Skip to content

Commit db748c4

Browse files
authored
CM-25361 - Add version command that supports TEXT and JSON output formats (#142)
1 parent 9d91ff4 commit db748c4

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

cycode/cli/consts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
PROGRAM_NAME = 'cycode'
2+
13
PRE_COMMIT_COMMAND_SCAN_TYPE = 'pre_commit'
24
PRE_RECEIVE_COMMAND_SCAN_TYPE = 'pre_receive'
35
COMMIT_HISTORY_COMMAND_SCAN_TYPE = 'commit_history'

cycode/cli/main.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import sys
34
from typing import TYPE_CHECKING, List, Optional, Tuple
@@ -8,7 +9,7 @@
89
from cycode.cli import code_scanner
910
from cycode.cli.auth.auth_command import authenticate
1011
from cycode.cli.config import config
11-
from cycode.cli.consts import ISSUE_DETECTED_STATUS_CODE, NO_ISSUES_STATUS_CODE
12+
from cycode.cli.consts import ISSUE_DETECTED_STATUS_CODE, NO_ISSUES_STATUS_CODE, PROGRAM_NAME
1213
from cycode.cli.models import Severity
1314
from cycode.cli.user_settings.configuration_manager import ConfigurationManager
1415
from cycode.cli.user_settings.credentials_manager import CredentialsManager
@@ -180,8 +181,30 @@ def finalize(context: click.Context, *_, **__) -> None:
180181
sys.exit(exit_code)
181182

182183

184+
@click.command(short_help='Show the version and exit')
185+
@click.pass_context
186+
def version(context: click.Context) -> None:
187+
output = context.obj['output']
188+
189+
prog = PROGRAM_NAME
190+
ver = __version__
191+
192+
message = f'{prog}, version {ver}'
193+
if output == 'json':
194+
message = json.dumps({'name': prog, 'version': ver})
195+
196+
click.echo(message, color=context.color)
197+
context.exit()
198+
199+
183200
@click.group(
184-
commands={'scan': code_scan, 'configure': set_credentials, 'ignore': add_exclusions, 'auth': authenticate},
201+
commands={
202+
'scan': code_scan,
203+
'configure': set_credentials,
204+
'ignore': add_exclusions,
205+
'auth': authenticate,
206+
'version': version,
207+
},
185208
context_settings=CONTEXT,
186209
)
187210
@click.option(
@@ -210,7 +233,7 @@ def finalize(context: click.Context, *_, **__) -> None:
210233
help='Characteristic JSON object that lets servers identify the application',
211234
type=str,
212235
)
213-
@click.version_option(__version__, prog_name='cycode')
236+
@click.version_option(__version__, prog_name=PROGRAM_NAME)
214237
@click.pass_context
215238
def main_cli(
216239
context: click.Context, verbose: bool, no_progress_meter: bool, output: str, user_agent: Optional[str]

0 commit comments

Comments
 (0)