Skip to content

Commit 3b9f386

Browse files
Merge pull request #83 from baloise/feat/version-command
Add version command
2 parents 4be6a5f + ff57579 commit 3b9f386

10 files changed

Lines changed: 105 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Added
9+
- `version` command
810

911
## [3.0.1] - 2020-04-09
1012

docs/commands/version.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# version
2+
3+
The `version` command shows the GitOps CLI version information.
4+
5+
## Example
6+
7+
```bash
8+
gitopscli version
9+
```
10+
11+
## Usage
12+
```
13+
usage: gitopscli version [-h]
14+
15+
optional arguments:
16+
-h, --help show this help message and exit
17+
```

gitopscli/__main__.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import sys
33

44
from gitopscli.cliparser import create_cli
5-
from gitopscli.commands.add_pr_comment import pr_comment_command
6-
from gitopscli.commands.create_preview import create_preview_command
7-
from gitopscli.commands.delete_preview import delete_preview_command
8-
from gitopscli.commands.deploy import deploy_command
9-
from gitopscli.commands.sync_apps import sync_apps_command
5+
from gitopscli.commands import (
6+
pr_comment_command,
7+
create_preview_command,
8+
delete_preview_command,
9+
deploy_command,
10+
sync_apps_command,
11+
version_command,
12+
)
1013
from gitopscli.gitops_exception import GitOpsException
1114

1215

@@ -24,9 +27,14 @@ def main():
2427
command = create_preview_command
2528
elif args.command == "delete-preview":
2629
command = delete_preview_command
27-
28-
verbose = args.verbose
29-
del args.verbose
30+
elif args.command == "version":
31+
command = version_command
32+
33+
if "verbose" in args:
34+
verbose = args.verbose
35+
del args.verbose
36+
else:
37+
verbose = False
3038

3139
try:
3240
command(**vars(args))

gitopscli/cliparser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def create_cli(args):
1111
__add_pr_comment_command_parser(subparsers)
1212
__add_create_preview_command_parser(subparsers)
1313
__add_delete_preview_command_parser(subparsers)
14+
__add_version_command_parser(subparsers)
1415

1516
if len(args) == 0:
1617
parser.print_help(sys.stderr)
@@ -84,6 +85,10 @@ def __add_delete_preview_command_parser(subparsers):
8485
__add_verbose_parser(add_delete_preview_p)
8586

8687

88+
def __add_version_command_parser(subparsers):
89+
subparsers.add_parser("version", help="Show the GitOps CLI version information")
90+
91+
8792
def __add_git_parser_args(deploy_p, api_only=False):
8893
deploy_p.add_argument("--username", help="Git username", required=True)
8994
deploy_p.add_argument("--password", help="Git password or token", required=True)

gitopscli/commands/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .add_pr_comment import pr_comment_command
2+
from .create_preview import create_preview_command
3+
from .delete_preview import delete_preview_command
4+
from .deploy import deploy_command
5+
from .sync_apps import sync_apps_command
6+
from .version import version_command

gitopscli/commands/version.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pkg_resources
2+
3+
4+
def version_command(command):
5+
assert command == "version"
6+
version = pkg_resources.require("gitopscli")[0].version
7+
print(f"GitOps CLI version {version}")

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ nav:
1818
- delete-preview: commands/delete-preview.md
1919
- deploy: commands/deploy.md
2020
- sync-apps: commands/sync-apps.md
21+
- version: commands/version.md
2122
- Changelog: changelog.md
2223
- Contributing: contributing.md
2324
- License: license.md

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="gitopscli",
5-
version="3.0.0",
5+
version="3.0.1",
66
packages=find_packages(),
77
entry_points={"console_scripts": ["gitopscli = gitopscli.__main__:main"]},
88
setup_requires=["wheel"],

tests/commands/test_version.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import re
2+
import sys
3+
import unittest
4+
from contextlib import contextmanager
5+
from io import StringIO
6+
import pytest
7+
8+
from gitopscli.commands.version import version_command
9+
10+
11+
@contextmanager
12+
def captured_output():
13+
new_out = StringIO()
14+
old_out = sys.stdout
15+
try:
16+
sys.stdout = new_out
17+
yield sys.stdout
18+
finally:
19+
sys.stdout = old_out
20+
21+
22+
class VersionCommandTest(unittest.TestCase):
23+
def test_output(self):
24+
with captured_output() as stdout:
25+
version_command("version")
26+
assert re.match(r"^GitOps CLI version \d+\.\d+\.\d+\n$", stdout.getvalue())
27+
28+
def test_wrong_command_arg(self):
29+
with pytest.raises(AssertionError):
30+
version_command("foo")

tests/test_cliparser.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
EXPECTED_GITOPSCLI_HELP = """\
1010
usage: gitopscli [-h]
11-
{deploy,sync-apps,add-pr-comment,create-preview,delete-preview}
11+
{deploy,sync-apps,add-pr-comment,create-preview,delete-preview,version}
1212
...
1313
1414
GitOps CLI
@@ -17,13 +17,14 @@
1717
-h, --help show this help message and exit
1818
1919
commands:
20-
{deploy,sync-apps,add-pr-comment,create-preview,delete-preview}
20+
{deploy,sync-apps,add-pr-comment,create-preview,delete-preview,version}
2121
deploy Trigger a new deployment by changing YAML values
2222
sync-apps Synchronize applications (= every directory) from apps
2323
config repository to apps root config
2424
add-pr-comment Create a comment on the pull request
2525
create-preview Create a preview environment
2626
delete-preview Delete a preview environment
27+
version Show the GitOps CLI version information
2728
"""
2829

2930
EXPECTED_ADD_PR_COMMENT_NO_ARGS_ERROR = """\
@@ -261,6 +262,13 @@
261262
Root config repository name
262263
"""
263264

265+
EXPECTED_VERSION_HELP = """\
266+
usage: gitopscli version [-h]
267+
268+
optional arguments:
269+
-h, --help show this help message and exit
270+
"""
271+
264272

265273
@contextmanager
266274
def captured_output():
@@ -795,3 +803,13 @@ def test_sync_apps_all_args(self):
795803
self.assertEqual(cli.git_provider, "GIT_PROVIDER")
796804
self.assertEqual(cli.git_provider_url, "GIT_PROVIDER_URL")
797805
self.assertTrue(cli.verbose)
806+
807+
def test_version_args(self):
808+
cli = create_cli(["version"])
809+
self.assertEqual(cli.command, "version")
810+
811+
def test_version_help(self):
812+
exit_code, stdout, stderr = self._capture_create_cli(["version", "--help"])
813+
self.assertEqual(exit_code, 0)
814+
self.assertEqual(EXPECTED_VERSION_HELP, stdout)
815+
self.assertEqual("", stderr)

0 commit comments

Comments
 (0)