diff --git a/harmony/cli.py b/harmony/cli.py index 505ac0b..d59e42f 100644 --- a/harmony/cli.py +++ b/harmony/cli.py @@ -65,6 +65,8 @@ def setup_cli(parser): parser.add_argument('--harmony-input', help=('the input data for the action provided by Harmony, required for ' '--harmony-action=invoke')) + parser.add_argument('--harmony-input-file', + help=('the optional path to the input data for the action provided by Harmony')) parser.add_argument('--harmony-sources', help=('file path that contains a STAC catalog with items and metadata to ' 'be processed by the service. Required for non-deprecated ' @@ -323,11 +325,16 @@ def run_cli(parser, args, AdapterClass, cfg=None): if args.harmony_wrap_stdout: setup_stdout_log_formatting(cfg) + # read in the operation file passed in with --harmony-input-file if any + if bool(args.harmony_input_file): + with open(args.harmony_input_file, 'r') as f: + args.harmony_input = f.read() + if args.harmony_action == 'invoke': start_time = datetime.datetime.now() if not bool(args.harmony_input): parser.error( - '--harmony-input must be provided for --harmony-action=invoke') + '--harmony-input or --harmony-input-file must be provided for --harmony-action=invoke') elif not bool(args.harmony_sources): successful = _invoke_deprecated(AdapterClass, args.harmony_input, cfg) if not successful: diff --git a/tests/test_cli.py b/tests/test_cli.py index 7d79297..3de52a1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,4 @@ +import os import unittest from unittest.mock import patch @@ -50,8 +51,11 @@ def test_when_passing_nothing_it_returns_false(self, parser): class TestCliInvokeAction(unittest.TestCase): def setUp(self): self.config = harmony.util.config(validate=False) + with open('/tmp/operation.json', 'w') as f: + f.write('{"test": "input"}') def tearDown(self): + os.remove('/tmp/operation.json') MockAdapter.messages = [] MockAdapter.errors = [] MockAdapter.cleaned_up = [] @@ -62,7 +66,7 @@ def test_when_harmony_input_is_not_provided_it_terminates_with_error(self, parse args = parser.parse_args() cli.run_cli(parser, args, MockAdapter, self.config) error_method.assert_called_once_with( - '--harmony-input must be provided for --harmony-action=invoke') + '--harmony-input or --harmony-input-file must be provided for --harmony-action=invoke') @cli_test('--harmony-action', 'invoke', '--harmony-input', '{"test": "input"}') def test_when_harmony_input_is_provided_it_creates_and_invokes_an_adapter(self, parser): @@ -70,6 +74,13 @@ def test_when_harmony_input_is_provided_it_creates_and_invokes_an_adapter(self, cli.run_cli(parser, args, MockAdapter, self.config) self.assertListEqual([{'test': 'input'}], MockAdapter.messages) + @cli_test('--harmony-action', 'invoke', '--harmony-input-file', '/tmp/operation.json') + def test_when_harmony_input_file_is_provided_it_creates_and_invokes_an_adapter(self, parser): + args = parser.parse_args() + + cli.run_cli(parser, args, MockAdapter, self.config) + self.assertListEqual([{'test': 'input'}], MockAdapter.messages) + @cli_test('--harmony-action', 'invoke', '--harmony-input', '{"test": "input"}') def test_when_the_backend_service_doesnt_respond_it_responds_with_an_error(self, parser): class MockImpl(MockAdapter):