Skip to content

Commit 6f1b4d4

Browse files
Yhoornemanclaude
andcommitted
feat: add --version option
Add a top-level `--version` flag via an eager Typer root callback that reads the installed package metadata (importlib.metadata) and prints `datadog-slo-overrides <version>`, falling back to "unknown" if the distribution metadata isn't available. Covered by a CLI test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 53e3bf8 commit 6f1b4d4

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
[![Changelog](https://img.shields.io/badge/changelog-Keep%20a%20Changelog%201.1.0-orange)](https://keepachangelog.com/en/1.1.0/)
1818
[![Documentation: Diátaxis](https://img.shields.io/badge/docs-Di%C3%A1taxis-009485?logo=readthedocs&logoColor=white)](https://diataxis.fr/)
1919
[![Build](https://img.shields.io/badge/build-unknown-lightgrey)](https://github.com/features/actions)
20-
[![Coverage](https://img.shields.io/badge/coverage-61%25-orange)](https://coverage.readthedocs.io/)
20+
[![Coverage](https://img.shields.io/badge/coverage-62%25-orange)](https://coverage.readthedocs.io/)
2121
[![pyscn quality](https://img.shields.io/badge/pyscn-not%20rated-lightgrey)](https://pyscn.ludo-tech.org)
2222

2323
CLI to set overrides idempotently for multiple SLO's

src/datadog_slo_overrides_cli/datadog_slo_overrides_cli.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import tomllib
2727
from dataclasses import dataclass
2828
from datetime import datetime
29+
from importlib.metadata import PackageNotFoundError
30+
from importlib.metadata import version as package_version
2931
from pathlib import Path
3032
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
3133

@@ -69,6 +71,7 @@
6971
APP_KEY_ENV = 'DD_APP_KEY'
7072

7173
APP_NAME = 'datadog-slo-overrides'
74+
PACKAGE_NAME = 'datadog_slo_overrides_cli'
7275

7376

7477
def config_dir() -> Path:
@@ -739,6 +742,34 @@ def execute(cfg: RunConfig) -> int:
739742
)
740743

741744

745+
def _version_callback(value: bool) -> None:
746+
"""Print the package version and exit when ``--version`` is passed."""
747+
if not value:
748+
return
749+
try:
750+
installed = package_version(PACKAGE_NAME)
751+
except PackageNotFoundError:
752+
installed = 'unknown'
753+
typer.echo(f'{APP_NAME} {installed}')
754+
raise typer.Exit
755+
756+
757+
@app.callback()
758+
def _root(
759+
_version: bool = typer.Option(
760+
False,
761+
'--version',
762+
callback=_version_callback,
763+
is_eager=True,
764+
help='Show the version and exit.',
765+
),
766+
) -> None:
767+
"""Set Datadog SLO corrections on multiple SLOs, selected by tag.
768+
769+
Dry-run by default; --apply to write. See README.md.
770+
"""
771+
772+
742773
@app.command(no_args_is_help=True)
743774
def run(
744775
tag: list[str] = typer.Option(

tests/test_datadog_slo_overrides_cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ def test_init_config_writes_template_and_guards_overwrite(tmp_path: Path) -> Non
190190
assert second.exit_code != 0
191191

192192

193+
def test_version_option() -> None:
194+
"""--version prints the app name and version, then exits 0."""
195+
result = runner.invoke(app, ['--version'])
196+
assert result.exit_code == 0
197+
assert 'datadog-slo-overrides' in result.output
198+
199+
193200
def test_load_direnv_env_success(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
194201
"""A successful ``direnv export json`` is parsed into the exported variables."""
195202
(tmp_path / '.envrc').write_text('export DD_API_KEY=k\n')

0 commit comments

Comments
 (0)