|
1 | 1 | import configparser |
2 | | -import sys |
3 | | -from pathlib import Path |
4 | | -from typing import NoReturn, Union |
5 | 2 |
|
6 | 3 | import click |
7 | 4 |
|
| 5 | +from repo_man.commands.add import add |
| 6 | +from repo_man.commands.edit import edit |
| 7 | +from repo_man.commands.flavors import flavors |
| 8 | +from repo_man.commands.implode import implode |
| 9 | +from repo_man.commands.init import init |
| 10 | +from repo_man.commands.list_repos import list_repos |
| 11 | +from repo_man.commands.sniff import sniff |
| 12 | +from repo_man.consts import REPO_TYPES_CFG |
8 | 13 |
|
9 | | -FAIL = "\033[91m" |
10 | | -ENDC = "\033[0m" |
11 | | -REPO_TYPES_CFG = "repo-man.cfg" |
12 | 14 |
|
13 | | - |
14 | | -def check_if_allowed(path: Path) -> Union[bool, NoReturn]: |
15 | | - if REPO_TYPES_CFG not in (str(item) for item in path.iterdir()): |
16 | | - print(f"{FAIL}The current directory is not configured for repository management{ENDC}") |
17 | | - sys.exit(1) |
18 | | - |
19 | | - return True |
20 | | - |
21 | | - |
22 | | -def parse_repo_types(config: configparser.ConfigParser) -> dict[str, set[str]]: |
23 | | - repo_types: dict[str, set[str]] = {"all": set()} |
24 | | - for section in config.sections(): |
25 | | - repos = {repo for repo in config[section]["known"].split("\n") if repo} |
26 | | - repo_types[section] = repos |
27 | | - if section != "ignore": |
28 | | - repo_types["all"].update(repos) |
29 | | - |
30 | | - return repo_types |
31 | | - |
32 | | - |
33 | | -def check_missing_repos(path: Path, repo_types: dict[str, set[str]]) -> None: |
34 | | - missing = set() |
35 | | - directories = {str(directory) for directory in path.iterdir()} |
36 | | - |
37 | | - for repo in sorted(repo_types["all"]): |
38 | | - if repo not in directories: |
39 | | - missing.add(repo) |
40 | | - |
41 | | - if missing: |
42 | | - print(f"{FAIL}The following repositories are configured but do not exist:") |
43 | | - for repo in missing: |
44 | | - print(f"\t{repo}") |
45 | | - sys.exit(1) |
46 | | - |
47 | | - return None |
48 | | - |
49 | | - |
50 | | -def get_valid_repo_types(): |
51 | | - config = configparser.ConfigParser() |
52 | | - config.read(REPO_TYPES_CFG) |
53 | | - valid_repo_types = parse_repo_types(config) |
54 | | - return sorted(set(valid_repo_types.keys())) |
55 | | - |
56 | | - |
57 | | -def main(): |
58 | | - path = Path(".") |
59 | | - check_if_allowed(path) |
60 | | - |
61 | | - config = configparser.ConfigParser() |
62 | | - config.read(REPO_TYPES_CFG) |
63 | | - valid_repo_types = parse_repo_types(config) |
64 | | - check_missing_repos(path, valid_repo_types) |
65 | | - |
66 | | - cli() |
67 | | - |
68 | | - |
69 | | -@click.group(invoke_without_command=True, context_settings={"help_option_names": ["-h", "--help"]}) |
| 15 | +@click.group(context_settings={"help_option_names": ["-h", "--help"]}) |
70 | 16 | @click.version_option(package_name="repo-man") |
71 | | -@click.option("-k", "--known", is_flag=True, help="List known repository types") |
72 | | -@click.option("-u", "--unconfigured", is_flag=True, help="List repositories without a configured type") |
73 | | -@click.option("-d", "--duplicates", is_flag=True, help="List repositories with more than one configured type") |
74 | | -# @click.option("-v", "--verbose", "verbosity", count=True) |
75 | | -def cli(known: bool, unconfigured: bool, duplicates: bool): |
| 17 | +@click.pass_context |
| 18 | +def cli(context): |
76 | 19 | """Manage repositories of different types""" |
77 | 20 |
|
78 | | - path = Path(".") |
79 | | - config = configparser.ConfigParser() |
80 | | - config.read(REPO_TYPES_CFG) |
81 | | - valid_repo_types = parse_repo_types(config) |
82 | | - |
83 | | - if known: |
84 | | - known_repo_types = sorted( |
85 | | - [repo_type for repo_type in valid_repo_types if repo_type != "all" and repo_type != "ignore"] |
86 | | - ) |
87 | | - for repo_type in known_repo_types: |
88 | | - print(repo_type) |
89 | | - |
90 | | - if unconfigured: |
91 | | - for directory in sorted(path.iterdir()): |
92 | | - if ( |
93 | | - directory.is_dir() |
94 | | - and str(directory) not in valid_repo_types["all"] |
95 | | - and str(directory) not in valid_repo_types.get("ignore", []) |
96 | | - ): |
97 | | - print(directory) |
98 | | - |
99 | | - if duplicates: |
100 | | - seen = set() |
101 | | - for repo_type in valid_repo_types: |
102 | | - if repo_type != "all" and repo_type != "ignore": |
103 | | - for repo in valid_repo_types[repo_type]: |
104 | | - if repo in seen: |
105 | | - print(repo) |
106 | | - seen.add(repo) |
107 | | - |
108 | | - |
109 | | -@cli.command(name="list", help="The type of repository to manage") |
110 | | -@click.option("-t", "--type", "repo_type", type=click.Choice(get_valid_repo_types()), show_choices=False, required=True) |
111 | | -def list_repos(repo_type: str): |
112 | | - """List matching repositories""" |
113 | | - |
114 | 21 | config = configparser.ConfigParser() |
115 | 22 | config.read(REPO_TYPES_CFG) |
116 | | - valid_repo_types = parse_repo_types(config) |
| 23 | + context.obj = config |
117 | 24 |
|
118 | | - for repo in valid_repo_types[repo_type]: |
119 | | - print(repo) |
120 | | - |
121 | | - return None |
122 | | - |
123 | | - |
124 | | -@cli.command |
125 | | -@click.argument("repo", type=click.Path(exists=True, file_okay=False)) |
126 | | -def flavors(repo: str): |
127 | | - """List the configured types for a repository""" |
128 | | - |
129 | | - config = configparser.ConfigParser() |
130 | | - config.read(REPO_TYPES_CFG) |
131 | 25 |
|
132 | | - found = set() |
133 | | - |
134 | | - for section in config.sections(): |
135 | | - if section == "ignore": |
136 | | - continue |
137 | | - if repo in config[section]["known"].split("\n"): |
138 | | - found.add(section) |
139 | | - |
140 | | - for repository in sorted(found): |
141 | | - print(repository) |
142 | | - |
143 | | - |
144 | | -@cli.command |
145 | | -@click.option("-t", "--type", "repo_types", multiple=True, help="The type of the repository", required=True) |
146 | | -@click.argument("repo", type=click.Path(exists=True, file_okay=False)) |
147 | | -def add(repo: str, repo_types: list): |
148 | | - """Add a new repository""" |
149 | | - |
150 | | - config = configparser.ConfigParser() |
151 | | - config.read(REPO_TYPES_CFG) |
152 | | - |
153 | | - for repo_type in repo_types: |
154 | | - if repo_type in config: |
155 | | - original_config = config[repo_type]["known"] |
156 | | - else: |
157 | | - original_config = "" |
158 | | - config.add_section(repo_type) |
159 | | - |
160 | | - if "known" not in config[repo_type] or repo not in config[repo_type]["known"].split("\n"): |
161 | | - config.set(repo_type, "known", f"{original_config}\n{repo}") |
162 | | - |
163 | | - with open(REPO_TYPES_CFG, "w") as config_file: |
164 | | - config.write(config_file) |
| 26 | +def main(): |
| 27 | + cli.add_command(add) |
| 28 | + cli.add_command(edit) |
| 29 | + cli.add_command(flavors) |
| 30 | + cli.add_command(implode) |
| 31 | + cli.add_command(init) |
| 32 | + cli.add_command(list_repos) |
| 33 | + cli.add_command(sniff) |
| 34 | + cli() |
0 commit comments