Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions regtests/client/python/cli/polaris_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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:
Expand All @@ -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()
Copy link
Member

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

if not config:
return

for attr in ['client_id', 'client_secret', 'host', 'port']:
Copy link
Member

@RussellSpitzer RussellSpitzer Aug 26, 2024

Choose a reason for hiding this comment

The 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])
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only support the current location, need to support the home dir(~) as well.

Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand Down