diff --git a/capycli/config/handle_config.py b/capycli/config/handle_config.py new file mode 100644 index 0000000..6e1ce8f --- /dev/null +++ b/capycli/config/handle_config.py @@ -0,0 +1,47 @@ +# ------------------------------------------------------------------------------- +# Copyright (c) 2024 Siemens +# All Rights Reserved. +# Author: thomas.graf@siemens.com +# +# SPDX-License-Identifier: MIT +# ------------------------------------------------------------------------------- + +import sys +from typing import Any + +import capycli.config.list +import capycli.mapping.mapping_to_xlsx +from capycli.common.print import print_red +from capycli.main.result_codes import ResultCode + + +def run_config_command(args: Any) -> None: + command = args.command[0].lower() + if command != "config": + return + + if len(args.command) < 2: + print_red("No subcommand specified!") + print() + + # display `mapping` related help + print("config - mapping sub-commands") + print(" list List the configuration settings") + print(" ToXlsx create an Excel sheet showing the mapping result") + return + + subcommand = args.command[1].lower() + if subcommand == "list": + """List the configuration settings.""" + app = capycli.config.list.ConfigList() + app.run(args) + return + + if subcommand == "toxlsx": + """Create an Excel sheet showing the mapping result.""" + app2 = capycli.mapping.mapping_to_xlsx.MappingToExcelXlsx() + app2.run(args) + return + + print_red("Unknown sub-command: " + subcommand) + sys.exit(ResultCode.RESULT_COMMAND_ERROR) diff --git a/capycli/config/list.py b/capycli/config/list.py new file mode 100644 index 0000000..caf1bb1 --- /dev/null +++ b/capycli/config/list.py @@ -0,0 +1,81 @@ +# ------------------------------------------------------------------------------- +# Copyright (c) 2024 Siemens +# All Rights Reserved. +# Author: thomas.graf@siemens.com +# +# SPDX-License-Identifier: MIT +# ------------------------------------------------------------------------------- + +import os +from typing import Any, Dict + +import capycli.common.html_support +import capycli.common.json_support +import capycli.common.script_base +from capycli import get_logger +from capycli.common.print import print_green, print_text, print_yellow +from capycli.main.options import CommandlineSupport + +LOG = get_logger(__name__) + + +class ConfigList(capycli.common.script_base.ScriptBase): + """List the configuration settings""" + + def print_config(self, config: Dict[str, Any]) -> None: + for key in config: + print(" ", key, "=", config[key]) + + def run(self, args: Any) -> None: + """Main method()""" + if args.debug: + global LOG + LOG = capycli.get_logger(__name__) + + print_text( + "\n" + capycli.APP_NAME + ", " + capycli.get_app_version() + + " - List the configuration settings\n") + + if args.help: + print("usage: CaPyCli config list") + print("") + return + + cmd_support = CommandlineSupport() + + config: Dict[str, Any] = {} + env_url = os.environ["SW360ServerUrl"] + if (env_url): + print_green("Environment: SW360ServerUrl =", env_url) + config["url"] = env_url + + env_token = os.environ["SW360ProductionToken"] + if (env_token): + print_green("Environment: SW360ServerUrl =", env_token) + config["token"] = env_token + + print() + + home = os.path.expanduser("~") + user_cfg = os.path.join(home, cmd_support.CONFIG_FILE_NAME) + if os.path.isfile(user_cfg): + print_green("User .capycli.cfg found") + config_user = cmd_support.read_config(user_cfg) + self.print_config(config_user) + config = cmd_support.update_config(config, config_user) + else: + print_yellow("No user .capycli.cfg found!") + + if os.path.isfile(cmd_support.CONFIG_FILE_NAME): + print_green("\nLocal .capycli.cfg found") + config_local = cmd_support.read_config() + self.print_config(config_local) + config = cmd_support.update_config(config, config_local) + else: + print_yellow("\nNo local .capycli.cfg found!") + + print() + + print_text("Resulting configuration:") + self.print_config(config) + print() diff --git a/capycli/main/application.py b/capycli/main/application.py index 38e0af2..a4b2274 100644 --- a/capycli/main/application.py +++ b/capycli/main/application.py @@ -15,6 +15,7 @@ import capycli from capycli.bom import handle_bom from capycli.common.print import print_red +from capycli.config import handle_config from capycli.dependencies import handle_dependencies from capycli.main import options from capycli.main.result_codes import ResultCode @@ -144,6 +145,8 @@ def _run(self, argv: List[str]) -> None: handle_moverview.run_moverview_command(self.options) elif command == "project": handle_project.run_project_command(self.options) + elif command == "config": + handle_config.run_config_command(self.options) else: print_red("Unknown command: " + command) sys.exit(ResultCode.RESULT_COMMAND_ERROR) diff --git a/capycli/main/options.py b/capycli/main/options.py index f433354..2ed29e4 100644 --- a/capycli/main/options.py +++ b/capycli/main/options.py @@ -438,10 +438,22 @@ def read_config(self, filename: str = "", config_string: str = "") -> Dict[str, return {} + def update_config(self, config_current: Dict[str, Any], config_new: Dict[str, Any]) -> Dict[str, Any]: + """Merge newer config settings into an existing config.""" + for key in config_new: + config_current[key] = config_new[key] + + return config_current + def process_commandline(self, argv: Any) -> Any: """Reads the command line arguments""" args = self.parser.parse_args(argv) - cfg = self.read_config() + home = os.path.expanduser("~") + user_cfg_path = os.path.join(home, self.CONFIG_FILE_NAME) + cfg = self.read_config(user_cfg_path) + + cfg_local = self.read_config() + cfg = self.update_config(cfg, cfg_local) if cfg: for key in cfg: