Skip to content

Commit

Permalink
fix: if no input file is supplied and no input is provided on STDIN, …
Browse files Browse the repository at this point in the history
…we will now try to automatically locate (in the current working directory) a manifest with default name for the input type specified. This works for PIP (Pipfile.lock), Poetry (poetry.lock) and Requirements (requirements.txt)

Signed-off-by: Paul Horton <phorton@sonatype.com>
  • Loading branch information
madpah committed Nov 12, 2021
1 parent 4c4c8d8 commit 93f9e59
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions cyclonedx_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
from cyclonedx.parser.requirements import RequirementsParser


class CycloneDxCmdException(Exception):
pass


class CycloneDxCmdNoInputFileSupplied(CycloneDxCmdException):
pass


class CycloneDxCmd:
# Whether debug output is enabled
_DEBUG_ENABLED: bool = False
Expand All @@ -49,7 +57,14 @@ def __init__(self, args: argparse.Namespace):
self._debug_message('Parsed Arguments: {}'.format(self._arguments))

def get_output(self) -> BaseOutput:
parser = self._get_input_parser()
try:
parser = self._get_input_parser()
except CycloneDxCmdNoInputFileSupplied as e:
print(f'ERROR: {str(e)}')
exit(1)
except CycloneDxCmdException as e:
print(f'ERROR: {str(e)}')
exit(1)

if parser.has_warnings():
print('')
Expand Down Expand Up @@ -186,6 +201,29 @@ def _get_input_parser(self) -> BaseParser:
return EnvironmentParser()

# All other Parsers will require some input - grab it now!
if not self._arguments.input_source:
# Nothing passed via STDIN, and no FILENAME supplied, let's assume a default by input type for ease
current_directory = os.getcwd()
try:
if self._arguments.input_from_conda_explicit:
raise CycloneDxCmdNoInputFileSupplied('When using input from Conda Explicit, you need to pipe input'
'via STDIN')
elif self._arguments.input_from_conda_json:
raise CycloneDxCmdNoInputFileSupplied('When using input from Conda JSON, you need to pipe input'
'via STDIN')
elif self._arguments.input_from_pip:
self._arguments.input_source = open(os.path.join(current_directory, 'Pipefile.lock'), 'r')
elif self._arguments.input_from_poetry:
self._arguments.input_source = open(os.path.join(current_directory, 'poetry.lock'), 'r')
elif self._arguments.input_from_requirements:
self._arguments.input_source = open(os.path.join(current_directory, 'requirements.txt'), 'r')
else:
raise CycloneDxCmdException('Parser type could not be determined.')
except FileNotFoundError as e:
raise CycloneDxCmdNoInputFileSupplied(
f'No input file was supplied and no input was provided on STDIN:\n{str(e)}'
)

input_data_fh = self._arguments.input_source
with input_data_fh:
input_data = input_data_fh.read()
Expand All @@ -202,7 +240,7 @@ def _get_input_parser(self) -> BaseParser:
elif self._arguments.input_from_requirements:
return RequirementsParser(requirements_content=input_data)
else:
raise ValueError('Parser type could not be determined.')
raise CycloneDxCmdException('Parser type could not be determined.')


def main():
Expand Down

0 comments on commit 93f9e59

Please sign in to comment.