-
Notifications
You must be signed in to change notification settings - Fork 332
Support configuration file for Polaris CLI #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,16 @@ | |
| from polaris.management import ApiClient, Configuration | ||
| from polaris.management import PolarisDefaultApi | ||
|
|
||
| """ | ||
| A CLI configuration file. Here is an example of the file content: | ||
| { | ||
| "client_id": "my_client_id", | ||
| "client_secret": "my_client_secret", | ||
| "host": "localhost", | ||
| "port": 8181 | ||
| } | ||
| """ | ||
| POLARIS_CLI_JSON = ".polaris_cli.json" | ||
|
|
||
| class PolarisCli: | ||
| """ | ||
|
|
@@ -44,6 +54,8 @@ class PolarisCli: | |
| @staticmethod | ||
| def execute(args=None): | ||
| options = Parser.parse(args) | ||
| PolarisCli._merge_with_config(options) | ||
|
|
||
| client_builder = PolarisCli._get_client_builder(options) | ||
| with client_builder() as api_client: | ||
| try: | ||
|
|
@@ -55,6 +67,27 @@ def execute(args=None): | |
| PolarisCli._try_print_exception(e) | ||
| sys.exit(1) | ||
|
|
||
| @staticmethod | ||
| def _merge_with_config(options): | ||
| config = PolarisCli._load_config() | ||
| if not config: | ||
| return | ||
|
|
||
| for attr in ['client_id', 'client_secret', 'host', 'port']: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason why we want to allow-list only these props? Seems like we could allow anything here for future proofing? I assume we could just have something like for attr in config
If attr not in options
options[attr] = config[attr] |
||
| if not getattr(options, attr) and config.get(attr): | ||
| setattr(options, attr, config[attr]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't overwrite the default values of host and port, will make the change to overwrite. |
||
|
|
||
| @staticmethod | ||
| def _load_config(): | ||
| try: | ||
| with open(POLARIS_CLI_JSON, "r") as file: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only support the current location, need to support the home dir(
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably want to allow a cli arg like -c configFile as well |
||
| return json.load(file) | ||
| except FileNotFoundError: | ||
| return None | ||
| except json.JSONDecodeError: | ||
| sys.stderr.write(f'Error decoding JSON from the file: {file_path}{os.linesep}') | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def _try_print_exception(e): | ||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we do a quick rename of this to file_config or something like that? Just to make it clear the difference between options and config. The lines below are a little complicated to me otherwise