add show-trace option that is disabled by default

This commit is contained in:
Jörg Thalheim
2023-09-22 16:03:45 +02:00
parent e3b2424d9d
commit fa9e8d757d

View File

@@ -96,15 +96,15 @@ def cast(value: Any, type: Type, opt_description: str) -> Any:
) )
def options_for_machine(machine_name: str) -> dict: def options_for_machine(machine_name: str, show_trace: bool = False) -> dict:
clan_dir = get_clan_flake_toplevel() clan_dir = get_clan_flake_toplevel()
# use nix eval to lib.evalModules .#clanModules.machine-{machine_name} flags = []
cmd = nix_eval( if show_trace:
flags=[ flags.append("--show-trace")
"--show-trace", flags.append(
f"{clan_dir}#nixosConfigurations.{machine_name}.config.clanCore.optionsNix", f"{clan_dir}#nixosConfigurations.{machine_name}.config.clanCore.optionsNix"
],
) )
cmd = nix_eval(flags=flags)
proc = subprocess.run( proc = subprocess.run(
cmd, cmd,
capture_output=True, capture_output=True,
@@ -117,7 +117,9 @@ def options_for_machine(machine_name: str) -> dict:
return json.loads(proc.stdout) return json.loads(proc.stdout)
def read_machine_option_value(machine_name: str, option: str) -> str: def read_machine_option_value(
machine_name: str, option: str, show_trace: bool = False
) -> str:
clan_dir = get_clan_flake_toplevel() clan_dir = get_clan_flake_toplevel()
# use nix eval to read from .#nixosConfigurations.default.config.{option} # use nix eval to read from .#nixosConfigurations.default.config.{option}
# this will give us the evaluated config with the options attribute # this will give us the evaluated config with the options attribute
@@ -148,11 +150,13 @@ def read_machine_option_value(machine_name: str, option: str) -> str:
def get_or_set_option(args: argparse.Namespace) -> None: def get_or_set_option(args: argparse.Namespace) -> None:
if args.value == []: if args.value == []:
print(read_machine_option_value(args.machine, args.option)) print(read_machine_option_value(args.machine, args.option, args.show_trace))
else: else:
# load options # load options
if args.options_file is None: if args.options_file is None:
options = options_for_machine(machine_name=args.machine) options = options_for_machine(
machine_name=args.machine, show_trace=args.show_trace
)
else: else:
with open(args.options_file) as f: with open(args.options_file) as f:
options = json.load(f) options = json.load(f)
@@ -169,6 +173,7 @@ def get_or_set_option(args: argparse.Namespace) -> None:
options=options, options=options,
settings_file=settings_file, settings_file=settings_file,
option_description=args.option, option_description=args.option,
show_trace=args.show_trace,
) )
if not args.quiet: if not args.quiet:
new_value = read_machine_option_value(args.machine, args.option) new_value = read_machine_option_value(args.machine, args.option)
@@ -182,6 +187,7 @@ def set_option(
options: dict, options: dict,
settings_file: Path, settings_file: Path,
option_description: str = "", option_description: str = "",
show_trace: bool = False,
) -> None: ) -> None:
option_path = option.split(".") option_path = option.split(".")
@@ -198,6 +204,7 @@ def set_option(
options=options, options=options,
settings_file=settings_file, settings_file=settings_file,
option_description=option, option_description=option,
show_trace=show_trace,
) )
target_type = map_type(options[option]["type"]) target_type = map_type(options[option]["type"])
@@ -242,7 +249,6 @@ def register_parser(
# inject callback function to process the input later # inject callback function to process the input later
parser.set_defaults(func=get_or_set_option) parser.set_defaults(func=get_or_set_option)
# add --machine argument
parser.add_argument( parser.add_argument(
"--machine", "--machine",
"-m", "-m",
@@ -251,39 +257,40 @@ def register_parser(
default="default", default="default",
) )
# add --options-file argument parser.add_argument(
"--show-trace",
help="Show nix trace on evaluation error",
action="store_true",
)
parser.add_argument( parser.add_argument(
"--options-file", "--options-file",
help="JSON file with options", help="JSON file with options",
type=Path, type=Path,
) )
# add --settings-file argument
parser.add_argument( parser.add_argument(
"--settings-file", "--settings-file",
help="JSON file with settings", help="JSON file with settings",
type=Path, type=Path,
) )
# add --quiet argument
parser.add_argument( parser.add_argument(
"--quiet", "--quiet",
help="Do not print the value", help="Do not print the value",
action="store_true", action="store_true",
) )
# add single positional argument for the option (e.g. "foo.bar")
parser.add_argument( parser.add_argument(
"option", "option",
help="Option to read or set", help="Option to read or set (e.g. foo.bar)",
type=str, type=str,
) )
# add a single optional argument for the value
parser.add_argument( parser.add_argument(
"value", "value",
# force this arg to be set # force this arg to be set
nargs="*", nargs="*",
help="Value to set", help="option value to set (if omitted, the current value is printed)",
) )