Coverage for src/somesy/commands/init_config.py: 100%
27 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-10 14:33 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-08-10 14:33 +0000
1"""CLI command to initialize somesy configuration file."""
2import logging
3from pathlib import Path
5import tomlkit
7from somesy.core.core import get_input_content
8from somesy.core.models import SomesyInput
10logger = logging.getLogger("somesy")
13def init_config(input_path: Path, options: dict) -> None:
14 """Initialize somesy configuration file.
16 Args:
17 input_path (Path): Path to somesy file (will be created/overwritten).
18 options (dict): CLI options.
19 """
20 logger.info(f"Updating input file ({input_path}) with CLI configurations...")
22 content = get_input_content(input_path, no_unwrap=True)
24 is_somesy = SomesyInput.is_somesy_file_path(input_path)
25 input_file_type = "somesy" if is_somesy else "pyproject"
26 msg = f"Found input file with {input_file_type} format."
27 logger.verbose(msg)
29 logger.debug(f"Input file content: {options}")
31 if "input_file" in options:
32 del options["input_file"]
33 if is_somesy:
34 content["config"] = options
35 else:
36 if "tool" not in content:
37 content["tool"] = {}
38 if "somesy" not in content["tool"]:
39 content["tool"]["somesy"] = {}
40 content["tool"]["somesy"]["config"] = options
42 with open(input_path, "w") as f:
43 tomlkit.dump(content, f)
45 logger.info(f"Input file ({input_path}) updated.")
46 logger.debug(f"Input file content: {content}")