edq.config.cmd.set

Update configuration options.

The file at the specified config location will be created if it doesn't exist.

 1"""
 2Update configuration options.
 3
 4The file at the specified config location will be created if it doesn't exist.
 5"""
 6
 7import argparse
 8import os
 9import typing
10
11import edq.config.argparser
12import edq.config.constants
13import edq.config.load
14import edq.config.settings
15import edq.config.source
16import edq.config.util
17
18def run(args: argparse.Namespace) -> int:
19    """ Run the target command and return the suggested exit status. """
20
21    path = _get_target_path(args)
22    if (path is None):
23        print("Unable to determine where to write the config to. Consider supplying a file on the command-line.")
24        return 1
25
26    path = os.path.abspath(path)
27
28    config_to_set: typing.Dict[str, str] = {}
29    for config_option in args.config_to_set:
30        (key, value) = edq.config.util.parse_string_config_option(config_option)
31        config_to_set[key] = value
32
33    edq.config.util.update_options_in_config_file(path, config_to_set)
34    print(f"Wrote {len(config_to_set)} config option(s) to: '{path}'.")
35
36    return 0
37
38def modify_parser(parser: argparse.ArgumentParser) -> None:
39    """ Add this CLI's flags to the given parser. """
40
41    parser.add_argument('config_to_set', metavar = "<KEY>=<VALUE>",
42        action = 'store', nargs = '+', type = str,
43        help = "Configuration option to be set. Expected config format is <key>=<value>.",
44    )
45
46    edq.config.argparser.add_config_location_argument_group(parser)
47
48def _get_target_path(args: argparse.Namespace) -> typing.Union[str, None]:
49    """
50    Decide where to write the given config to.
51    If no source is explicitly specified, write to the first local or project config.
52    If no config location can be determined, return None.
53    """
54
55    if (args.scope_file is not None):
56        return str(args.scope_file)
57
58    # If no explicit scope is provided, use either local or project scopes (whichever appears first).
59    no_explicit_scope = ((not args.scope_local) and (not args.scope_project) and (not args.scope_global))
60
61    for spec in edq.config.settings.get_load_order():
62        # Skip specs that do not have a path.
63        if (not isinstance(spec, edq.config.source.AbstractPathSpec)):
64            continue
65
66        if ((args.scope_local or no_explicit_scope) and isinstance(spec, edq.config.source.LocalSpec)):
67            return spec.resolve_path()
68
69        if ((args.scope_project or no_explicit_scope) and isinstance(spec, edq.config.source.ProjectSpec)):
70            return spec.resolve_path()
71
72        if (args.scope_global and isinstance(spec, edq.config.source.GlobalSpec)):
73            return spec.resolve_path(getattr(args, edq.config.constants.GLOBAL_CONFIG_KEY, None))
74
75    return None
def run(args: argparse.Namespace) -> int:
19def run(args: argparse.Namespace) -> int:
20    """ Run the target command and return the suggested exit status. """
21
22    path = _get_target_path(args)
23    if (path is None):
24        print("Unable to determine where to write the config to. Consider supplying a file on the command-line.")
25        return 1
26
27    path = os.path.abspath(path)
28
29    config_to_set: typing.Dict[str, str] = {}
30    for config_option in args.config_to_set:
31        (key, value) = edq.config.util.parse_string_config_option(config_option)
32        config_to_set[key] = value
33
34    edq.config.util.update_options_in_config_file(path, config_to_set)
35    print(f"Wrote {len(config_to_set)} config option(s) to: '{path}'.")
36
37    return 0

Run the target command and return the suggested exit status.

def modify_parser(parser: argparse.ArgumentParser) -> None:
39def modify_parser(parser: argparse.ArgumentParser) -> None:
40    """ Add this CLI's flags to the given parser. """
41
42    parser.add_argument('config_to_set', metavar = "<KEY>=<VALUE>",
43        action = 'store', nargs = '+', type = str,
44        help = "Configuration option to be set. Expected config format is <key>=<value>.",
45    )
46
47    edq.config.argparser.add_config_location_argument_group(parser)

Add this CLI's flags to the given parser.