Skip to content

Commit

Permalink
Merge pull request #338 from CycloneDX/bugfix/json-format-default-file
Browse files Browse the repository at this point in the history
fix: cli default file name for json format
  • Loading branch information
madpah authored Apr 5, 2022
2 parents eb054b0 + 8747620 commit 929e26d
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions cyclonedx_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
# Copyright (c) OWASP Foundation. All Rights Reserved.

import argparse
import enum
import os
import sys
from datetime import datetime
from typing import Optional

from cyclonedx.model import Tool
from cyclonedx.model.bom import Bom
from cyclonedx.output import BaseOutput, OutputFormat, SchemaVersion, get_instance
from cyclonedx.output import BaseOutput, OutputFormat, SchemaVersion, get_instance as get_output_instance
from cyclonedx.parser import BaseParser

from .parser.conda import CondaListExplicitParser, CondaListJsonParser
Expand All @@ -44,6 +45,22 @@ class CycloneDxCmdNoInputFileSupplied(CycloneDxCmdException):
pass


@enum.unique
class _CLI_OUTPUT_FORMAT(enum.Enum):
XML = 'xml'
JSON = 'json'


_output_formats = {
_CLI_OUTPUT_FORMAT.XML: OutputFormat.XML,
_CLI_OUTPUT_FORMAT.JSON: OutputFormat.JSON,
}
_output_default_filenames = {
_CLI_OUTPUT_FORMAT.XML: 'cyclonedx.xml',
_CLI_OUTPUT_FORMAT.JSON: 'cyclonedx.json',
}


class CycloneDxCmd:
# Whether debug output is enabled
_DEBUG_ENABLED: bool = False
Expand All @@ -59,6 +76,9 @@ def __init__(self, args: argparse.Namespace) -> None:
self._debug_message('!!! DEBUG MODE ENABLED !!!')
self._debug_message('Parsed Arguments: {}'.format(self._arguments))

def _get_output_format(self) -> _CLI_OUTPUT_FORMAT:
return _CLI_OUTPUT_FORMAT(str(self._arguments.output_format).lower())

def get_output(self) -> BaseOutput:
try:
parser = self._get_input_parser()
Expand Down Expand Up @@ -94,17 +114,20 @@ def get_output(self) -> BaseOutput:
vendor='CycloneDX', name='cyclonedx-bom', version=md_version('cyclonedx-bom')
))

return get_instance(
return get_output_instance(
bom=bom,
output_format=OutputFormat[str(self._arguments.output_format).upper()],
output_format=_output_formats[self._get_output_format()],
schema_version=SchemaVersion['V{}'.format(
str(self._arguments.output_schema_version).replace('.', '_')
)]
)

def execute(self) -> None:
output_format = self._get_output_format()
self._debug_message(f'output_format: {output_format}')

# Quick check for JSON && SchemaVersion <= 1.1
if str(self._arguments.output_format).upper() == 'JSON' and \
if output_format == OutputFormat.JSON and \
str(self._arguments.output_schema_version) in ['1.0', '1.1']:
self._error_and_exit(
message='CycloneDX schema does not support JSON output in Schema Versions < 1.2',
Expand All @@ -118,7 +141,9 @@ def execute(self) -> None:
return

# Check directory writable
output_filename = os.path.realpath(self._arguments.output_file)
output_file = self._arguments.output_file
output_filename = os.path.realpath(
output_file if isinstance(output_file, str) else _output_default_filenames[output_format])
self._debug_message('Will be outputting SBOM to file at: {}'.format(output_filename))
output.output_to_file(filename=output_filename, allow_overwrite=self._arguments.output_file_overwrite)

Expand Down Expand Up @@ -177,7 +202,8 @@ def get_arg_parser(*, prog: Optional[str] = None) -> argparse.ArgumentParser:
description='Choose the output format and schema version'
)
output_group.add_argument(
'--format', action='store', choices=['json', 'xml'], default='xml',
'--format', action='store',
choices=[f.value for f in _CLI_OUTPUT_FORMAT], default=_CLI_OUTPUT_FORMAT.XML.value,
help='The output format for your SBOM (default: %(default)s)',
dest='output_format'
)
Expand All @@ -187,7 +213,8 @@ def get_arg_parser(*, prog: Optional[str] = None) -> argparse.ArgumentParser:
dest='output_schema_version'
)
output_group.add_argument(
'-o', '--o', '--output', action='store', metavar='FILE_PATH', default='cyclonedx.xml', required=False,
# string, None or True. True=autodetect(based-on-format)
'-o', '--o', '--output', action='store', metavar='FILE_PATH', default=True, required=False,
help='Output file path for your SBOM (set to \'-\' to output to STDOUT)', dest='output_file'
)
output_group.add_argument(
Expand Down

0 comments on commit 929e26d

Please sign in to comment.