Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Existing `queries` configs remain valid; they are the fallback/global scope. Que
| `--output` | Save to file instead of printing; filename supports `{CURRENT_DATE}` etc. | `--output report_{CURRENT_DATE}` |
| `--list-connections` | Print JSON array of configured connection names and exit | `--list-connections` |
| `--list-queries` | Print configured query names and exit. Default shape is `{"global": [...], "connections": {...}}`; pass `--list-queries legacy` for the old flat global query array | `--list-queries` |
| `--version` / `-v` | Print the installed version (`sql2json <version>`) and exit | `--version` |

**Note:** Both `--list-connections` and `--list_connections` (underscore) are accepted by fire.

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- `--version` / `-v` flag prints the installed version (`sql2json <version>`) and exits 0, instead of trying to run a query named `version`.

### Changed
- The Docker image now uses an Alpine base with a multi-stage build, producing a
smaller image (~99 MB). It continues to run as a non-root user.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ sql2json [options] [--param value ...]
| `--output` | _(stdout)_ | Save to file; filename supports `{CURRENT_DATE}` etc. |
| `--list-connections` | — | Print JSON array of configured connection names and exit |
| `--list-queries` | — | Print configured query names and exit. Default shape is `{"global": [...], "connections": {...}}`; pass `--list-queries legacy` for the old flat global query list |
| `--version` / `-v` | — | Print the installed version (`sql2json <version>`) and exit |

Extra `--key value` flags become SQL bind parameters (`:key` in your query).

Expand Down
1 change: 1 addition & 0 deletions skills/sql2json/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ sql2json --list-connections
sql2json --list-queries
# → {"global": ["users"], "connections": {"mydb": ["table_sizes"]}}
sql2json --list-queries legacy # old flat global query list
sql2json --version # print installed version and exit (also -v)
```

When an agent receives a data request, discover connections and scoped queries before inventing SQL. Choose a connection, then prefer a query listed under that connection; fall back to a global query only if no scoped query matches.
Expand Down
4 changes: 4 additions & 0 deletions sql2json/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import fire

from . import __version__
from .parameter import parse_parameter
from .sql2json import (
_current_date,
Expand Down Expand Up @@ -160,6 +161,9 @@ def handle_run_query2json(


def main():
if any(arg in ("--version", "-v") for arg in sys.argv[1:]):
print(f"sql2json {__version__}")
sys.exit(0)
fire.Fire(handle_run_query2json)


Expand Down
42 changes: 42 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,48 @@ def test_invalid_timezone_stderr_is_json(self, tmp_path):
assert "error" in error


class TestVersion:
def test_long_flag_exits_zero(self):
result = run_cli("--version")
assert result.returncode == 0

def test_short_flag_exits_zero(self):
result = run_cli("-v")
assert result.returncode == 0

def test_long_flag_prints_version(self):
from sql2json import __version__

result = run_cli("--version")
assert result.stdout.strip() == f"sql2json {__version__}"

def test_short_flag_prints_version(self):
from sql2json import __version__

result = run_cli("-v")
assert result.stdout.strip() == f"sql2json {__version__}"

def test_does_not_run_a_query(self):
result = run_cli("--version")
assert result.stderr == ""

def test_short_circuits_before_config_is_read(self, tmp_path):
from sql2json import __version__

missing_cfg = str(tmp_path / "does_not_exist.json")
result = run_cli("--version", "--config", missing_cfg)
assert result.returncode == 0
assert result.stdout.strip() == f"sql2json {__version__}"
assert result.stderr == ""

def test_works_alongside_other_flags(self):
from sql2json import __version__

result = run_cli("--name", "default", "--query", "default", "--version")
assert result.returncode == 0
assert result.stdout.strip() == f"sql2json {__version__}"


class TestEntryPoint:
"""The `sql2json` console_scripts entry point (pyproject [project.scripts])."""

Expand Down
Loading