Skip to content

Commit f812999

Browse files
add output format flag support to --version subcommand
1 parent 9840c84 commit f812999

3 files changed

Lines changed: 55 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
## [1.11.2] - 2026-01-22
11+
12+
## Added
13+
1014
- Migrate from CircleCI to GitHub Actions for testing and release workflows.
1115
- Remove CircleCI workflows.
1216
- Migrate from using `shiv` for zipapp generation to `pex` in order to support specific platform/arch and improve testing framework.
1317
- Add zizmor for GitHub Actions code scans, part of workflow and pre-commit
18+
- Support output format for `--version` in order to allow JSON parsing
1419

1520
## [1.10.3] - 2026-01-08
1621

cloudsmith_cli/cli/commands/main.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@
55
from ...core.api.version import get_version as get_api_version
66
from ...core.utils import get_github_website, get_help_website
77
from ...core.version import get_version as get_cli_version
8-
from .. import command, decorators
8+
from .. import command, decorators, utils
99

1010
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
1111

1212

13-
def print_version():
13+
def print_version(opts):
1414
"""Print the environment versions."""
15-
click.echo("Versions:")
16-
click.secho(
17-
"CLI Package Version: %(version)s"
18-
% {"version": click.style(get_cli_version(), bold=True)}
19-
)
20-
click.secho(
21-
"API Package Version: %(version)s"
22-
% {"version": click.style(get_api_version(), bold=True)}
23-
)
15+
cli_version = get_cli_version()
16+
api_version = get_api_version()
17+
18+
data = {
19+
"cli_version": cli_version,
20+
"api_version": api_version,
21+
}
22+
23+
if not utils.maybe_print_as_json(opts, data):
24+
click.echo("Versions:")
25+
click.secho(
26+
"CLI Package Version: %(version)s"
27+
% {"version": click.style(cli_version, bold=True)}
28+
)
29+
click.secho(
30+
"API Package Version: %(version)s"
31+
% {"version": click.style(api_version, bold=True)}
32+
)
2433

2534

2635
@click.group(
@@ -52,12 +61,13 @@ def print_version():
5261
is_eager=True,
5362
)
5463
@decorators.common_cli_config_options
64+
@decorators.common_cli_output_options
5565
@click.pass_context
5666
def main(ctx, opts, version):
5767
"""Handle entrypoint to CLI."""
5868
# pylint: disable=unused-argument
5969

6070
if version:
61-
print_version()
71+
print_version(opts)
6272
elif ctx.invoked_subcommand is None:
6373
click.echo(ctx.get_help())

cloudsmith_cli/cli/tests/commands/test_main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
13
import pytest
24

35
from ....core.api.version import get_version as get_api_version
@@ -17,6 +19,32 @@ def test_main_version(self, runner, option):
1719
"API Package Version: " + get_api_version() + "\n"
1820
)
1921

22+
@pytest.mark.parametrize("option", ["-V", "--version"])
23+
@pytest.mark.parametrize(
24+
"format_option,format_value",
25+
[("-F", "json"), ("--output-format", "json")],
26+
)
27+
def test_main_version_json(self, runner, option, format_option, format_value):
28+
"""Test the JSON output of `cloudsmith --version --output-format json`."""
29+
result = runner.invoke(main, [option, format_option, format_value])
30+
assert result.exit_code == 0
31+
output = json.loads(result.output)
32+
assert "data" in output
33+
assert output["data"]["cli_version"] == get_version()
34+
assert output["data"]["api_version"] == get_api_version()
35+
36+
@pytest.mark.parametrize("option", ["-V", "--version"])
37+
def test_main_version_pretty_json(self, runner, option):
38+
"""Test the pretty JSON output of `cloudsmith --version --output-format pretty_json`."""
39+
result = runner.invoke(main, [option, "--output-format", "pretty_json"])
40+
assert result.exit_code == 0
41+
output = json.loads(result.output)
42+
assert "data" in output
43+
assert output["data"]["cli_version"] == get_version()
44+
assert output["data"]["api_version"] == get_api_version()
45+
# Verify it's formatted with indentation
46+
assert " " in result.output
47+
2048
@pytest.mark.parametrize("option", ["-h", "--help"])
2149
def test_main_help(self, runner, option):
2250
"""Test the output of `cloudsmith --help`."""

0 commit comments

Comments
 (0)