3333from cyclonedx .parser .requirements import RequirementsParser
3434
3535
36+ class CycloneDxCmdException (Exception ):
37+ pass
38+
39+
40+ class CycloneDxCmdNoInputFileSupplied (CycloneDxCmdException ):
41+ pass
42+
43+
3644class CycloneDxCmd :
3745 # Whether debug output is enabled
3846 _DEBUG_ENABLED : bool = False
@@ -49,7 +57,14 @@ def __init__(self, args: argparse.Namespace):
4957 self ._debug_message ('Parsed Arguments: {}' .format (self ._arguments ))
5058
5159 def get_output (self ) -> BaseOutput :
52- parser = self ._get_input_parser ()
60+ try :
61+ parser = self ._get_input_parser ()
62+ except CycloneDxCmdNoInputFileSupplied as e :
63+ print (f'ERROR: { str (e )} ' )
64+ exit (1 )
65+ except CycloneDxCmdException as e :
66+ print (f'ERROR: { str (e )} ' )
67+ exit (1 )
5368
5469 if parser .has_warnings ():
5570 print ('' )
@@ -186,6 +201,29 @@ def _get_input_parser(self) -> BaseParser:
186201 return EnvironmentParser ()
187202
188203 # All other Parsers will require some input - grab it now!
204+ if not self ._arguments .input_source :
205+ # Nothing passed via STDIN, and no FILENAME supplied, let's assume a default by input type for ease
206+ current_directory = os .getcwd ()
207+ try :
208+ if self ._arguments .input_from_conda_explicit :
209+ raise CycloneDxCmdNoInputFileSupplied ('When using input from Conda Explicit, you need to pipe input'
210+ 'via STDIN' )
211+ elif self ._arguments .input_from_conda_json :
212+ raise CycloneDxCmdNoInputFileSupplied ('When using input from Conda JSON, you need to pipe input'
213+ 'via STDIN' )
214+ elif self ._arguments .input_from_pip :
215+ self ._arguments .input_source = open (os .path .join (current_directory , 'Pipfile.lock' ), 'r' )
216+ elif self ._arguments .input_from_poetry :
217+ self ._arguments .input_source = open (os .path .join (current_directory , 'poetry.lock' ), 'r' )
218+ elif self ._arguments .input_from_requirements :
219+ self ._arguments .input_source = open (os .path .join (current_directory , 'requirements.txt' ), 'r' )
220+ else :
221+ raise CycloneDxCmdException ('Parser type could not be determined.' )
222+ except FileNotFoundError as e :
223+ raise CycloneDxCmdNoInputFileSupplied (
224+ f'No input file was supplied and no input was provided on STDIN:\n { str (e )} '
225+ )
226+
189227 input_data_fh = self ._arguments .input_source
190228 with input_data_fh :
191229 input_data = input_data_fh .read ()
@@ -202,7 +240,7 @@ def _get_input_parser(self) -> BaseParser:
202240 elif self ._arguments .input_from_requirements :
203241 return RequirementsParser (requirements_content = input_data )
204242 else :
205- raise ValueError ('Parser type could not be determined.' )
243+ raise CycloneDxCmdException ('Parser type could not be determined.' )
206244
207245
208246def main ():
0 commit comments