summaryrefslogtreecommitdiffstats
path: root/cli/config.py
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-11-06 13:19:26 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-11-21 17:05:26 +0100
commitc4eb5d165a00fbe7e5ec2e9a1e7f2123ce09bcd5 (patch)
tree7a143466afd4c5d9ced5a4da0ef0fd6616dd6dc9 /cli/config.py
parent10a3897f92ee058155987cea23eec28b28377d4b (diff)
ogcli: move configuration data into a different file
Move the configuration into its own file so it is easier to access from multiple files.
Diffstat (limited to 'cli/config.py')
-rw-r--r--cli/config.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/cli/config.py b/cli/config.py
new file mode 100644
index 0000000..5a79698
--- /dev/null
+++ b/cli/config.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2020-2024 Soleta Networks <opengnsys@soleta.eu>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the
+# Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+import json
+
+OG_CLI_CFG_PATH = "/opt/opengnsys/etc/ogcli.json"
+
+def load_config():
+ data = None
+ try:
+ with open(OG_CLI_CFG_PATH, 'r') as json_file:
+ data = json.load(json_file)
+ except json.JSONDecodeError:
+ sys.exit(f'ERROR: Failed parse malformed JSON file {OG_CLI_CFG_PATH}')
+ except Exception as e:
+ sys.exit(f'ERROR: cannot open {OG_CLI_CFG_PATH}: {e}')
+
+ required_cfg_params = {'api_token', 'ip', 'port'}
+ difference_cfg_params = required_cfg_params - data.keys()
+ if len(difference_cfg_params) > 0:
+ sys.exit(f'Missing {difference_cfg_params} key in '
+ f'json config file')
+ return data
+
+cfg = load_config()