Skip to content

Commit

Permalink
feat: globalConfig file passed through --config accepts only JSON o…
Browse files Browse the repository at this point in the history
…r YAML formats (#1216)

**Issue number:** ADDON-71108

## Summary Config class should accept only json or yaml files

### Changes

> Argparser was modified not to allow anything with other extension than
.json or .yaml to be passed as config parameter (--config)

### User experience

> N/A

## Checklist

If your change doesn't seem to apply, please leave them unchecked.

* [x] I have performed a self-review of this change
* [x] Changes have been tested
* [x] Changes are documented
* [x] PR title follows [conventional commit
semantics](https://www.conventionalcommits.org/en/v1.0.0/)
  • Loading branch information
lplonka-splunk authored Jun 3, 2024
1 parent 21610e2 commit f661475
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ It takes the following parameters:
source. The default is `package`.
* `--config` - [optional] path to the configuration file. It defaults to
the globalConfig file in the parent directory of the source provided.
Only *.json* and *.yaml* files are accepted.
* `--ta-version` - [optional] override current version of TA. The default
version is version specified in `globalConfig.json` or `globalConfig.yaml`.
A Splunkbase compatible version of SEMVER will be used by default.
Expand Down
17 changes: 15 additions & 2 deletions splunk_add_on_ucc_framework/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.
#
import argparse
import re
import sys
from typing import Optional, Sequence
import logging
Expand All @@ -35,6 +36,8 @@
# subparser being specified. Example is `--version`, the default subparser will
# be added here as well. But this is not a big deal for now, we don't have
# global options anyway.


class DefaultSubcommandArgumentParser(argparse.ArgumentParser):
__default_subparser = None

Expand Down Expand Up @@ -78,9 +81,10 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
)
build_parser.add_argument(
"--config",
type=str,
type=config_file_type,
nargs="?",
help="path to configuration file, defaults to globalConfig file in parent directory of source provided",
help="path to configuration file, defaults to globalConfig file in parent directory of source provided. "
"Only .json and .yaml files are accepted.",
default=None,
)
build_parser.add_argument(
Expand Down Expand Up @@ -231,5 +235,14 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
return 0


def config_file_type(filename: str) -> str:
pattern = re.compile(r".*\.(json|yaml)$")
if not pattern.match(filename):
msg = f"Global config file should be a JSON or YAML file. Provided: {filename}"
logger.error(msg)
raise argparse.ArgumentTypeError(msg)
return filename


if __name__ == "__main__":
raise SystemExit(main())
30 changes: 30 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,33 @@ def test_package_command(mock_package, args, expected_parameters):
main.main(args)

mock_package.assert_called_with(**expected_parameters)


@pytest.mark.parametrize(
"config_path,should_pass",
(
["path/to/config.json", True],
["path/to/config.yaml", True],
["path/to/config_but_not_jsonnor_yaml.xyz", False],
["config.yml", False],
),
)
@mock.patch("splunk_add_on_ucc_framework.commands.build.generate")
def test_correct_config_argument(
mock_ucc_gen_generate, caplog, config_path, should_pass
):
args = ["build", "--config"]
args.append(config_path)

if should_pass:
main.main(args)

args, kwargs = mock_ucc_gen_generate.call_args
assert kwargs["config_path"] == config_path

else: # Failing scenario - config file is not .json nor .yaml
with pytest.raises(SystemExit):
main.main(args)

expected_msg = f" Global config file should be a JSON or YAML file. Provided: {config_path}"
assert expected_msg in caplog.text

0 comments on commit f661475

Please sign in to comment.