Skip to content

Commit 6a76139

Browse files
committed
Replace individual host options with --hosts
1 parent 95028f1 commit 6a76139

File tree

3 files changed

+36
-59
lines changed

3 files changed

+36
-59
lines changed

.github/workflows/update-gin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
2828
- name: Run script
2929
# No space between `-e` and `run` or tox gets confused
30-
run: tox -erun -- -l DEBUG --gin --hub-datalad-org --atris
30+
run: tox -erun -- -l DEBUG --hosts gin,hub.datalad.org,atris
3131
env:
3232
GIN_TOKEN: ${{ secrets.GIN_TOKEN }}
3333
HUB_DATALAD_ORG_TOKEN: ${{ secrets.HUB_DATALAD_ORG_TOKEN }}

.github/workflows/update.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
2828
- name: Run script
2929
# No space between `-e` and `run` or tox gets confused
30-
run: tox -erun -- -l DEBUG --github --osf
30+
run: tox -erun -- -l DEBUG --hosts github,osf
3131
env:
3232
GITHUB_TOKEN: ${{ secrets.SEARCH_GITHUB_TOKEN }}
3333

src/find_datalad_repos/__main__.py

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import os
5+
import re
56
import click
67
from click_loglevel import LogLevel
78
from ghtoken import get_ghtoken
@@ -12,72 +13,50 @@
1213
from .util import commit, runcmd
1314

1415

15-
def set_mode(
16-
ctx: click.Context, _param: click.Parameter, value: RepoHost | None
17-
) -> RepoHost | None:
18-
if value is not None:
19-
ctx.params.setdefault("mode", set()).add(value)
20-
return value
16+
class RepoHostSet(click.ParamType):
17+
name = "hostset"
18+
19+
def convert(
20+
self,
21+
value: str | set[RepoHost],
22+
param: click.Parameter | None,
23+
ctx: click.Context | None,
24+
) -> set[RepoHost]:
25+
if not isinstance(value, str):
26+
return value
27+
selected = set()
28+
for v in re.split(r"\s*,\s*", value):
29+
try:
30+
selected.add(RepoHost(v))
31+
except ValueError:
32+
self.fail(f"{value!r}: invalid item {v!r}", param, ctx)
33+
return selected
34+
35+
def get_metavar(self, _param: click.Parameter) -> str:
36+
return "[all," + ",".join(v.value for v in RepoHost) + "]"
2137

2238

2339
@click.command()
40+
@click.option(
41+
"--hosts",
42+
type=RepoHostSet(),
43+
default="all",
44+
help="Set which repository hosts to query",
45+
)
2446
@click.option(
2547
"-l",
2648
"--log-level",
2749
type=LogLevel(),
2850
default=logging.INFO,
2951
help="Set logging level [default: INFO]",
3052
)
31-
@click.option(
32-
"--atris",
33-
flag_value=RepoHost.ATRIS,
34-
type=click.UNPROCESSED,
35-
callback=set_mode,
36-
expose_value=False,
37-
help="Update ATRIS data",
38-
)
39-
@click.option(
40-
"--gin",
41-
flag_value=RepoHost.GIN,
42-
type=click.UNPROCESSED,
43-
callback=set_mode,
44-
expose_value=False,
45-
help="Update GIN data",
46-
)
47-
@click.option(
48-
"--github",
49-
flag_value=RepoHost.GITHUB,
50-
type=click.UNPROCESSED,
51-
callback=set_mode,
52-
expose_value=False,
53-
help="Update GitHub data",
54-
)
55-
@click.option(
56-
"--hub-datalad-org",
57-
flag_value=RepoHost.HUB_DATALAD_ORG,
58-
type=click.UNPROCESSED,
59-
callback=set_mode,
60-
expose_value=False,
61-
help="Update hub.datalad.org data",
62-
)
63-
@click.option(
64-
"--osf",
65-
flag_value=RepoHost.OSF,
66-
type=click.UNPROCESSED,
67-
callback=set_mode,
68-
expose_value=False,
69-
help="Update OSF data",
70-
)
7153
@click.option(
7254
"-R",
7355
"--regen-readme",
7456
is_flag=True,
7557
help="Regenerate the README from the JSON file without querying",
7658
)
77-
def main(log_level: int, regen_readme: bool, mode: set[RepoHost] | None = None) -> None:
78-
if regen_readme and mode:
79-
raise click.UsageError("--regen-readme is mutually exclusive with mode options")
80-
59+
def main(log_level: int, regen_readme: bool, hosts: set[RepoHost]) -> None:
8160
logging.basicConfig(
8261
format="%(asctime)s [%(levelname)-8s] %(name)s %(message)s",
8362
datefmt="%Y-%m-%dT%H:%M:%S%z",
@@ -91,20 +70,18 @@ def main(log_level: int, regen_readme: bool, mode: set[RepoHost] | None = None)
9170
record = RepoRecord()
9271

9372
reports: list[str] = []
94-
if mode is None:
95-
mode = set(RepoHost)
9673
if not regen_readme:
97-
if RepoHost.GITHUB in mode:
74+
if RepoHost.GITHUB in hosts:
9875
reports.extend(record.update_github(get_ghtoken()))
99-
if RepoHost.OSF in mode:
76+
if RepoHost.OSF in hosts:
10077
reports.extend(record.update_osf())
101-
if RepoHost.GIN in mode:
78+
if RepoHost.GIN in hosts:
10279
reports.extend(record.update_gin(os.environ["GIN_TOKEN"]))
103-
if RepoHost.HUB_DATALAD_ORG in mode:
80+
if RepoHost.HUB_DATALAD_ORG in hosts:
10481
reports.extend(
10582
record.update_hub_datalad_org(os.environ["HUB_DATALAD_ORG_TOKEN"])
10683
)
107-
if RepoHost.ATRIS in mode:
84+
if RepoHost.ATRIS in hosts:
10885
reports.extend(record.update_atris())
10986
with open(RECORD_FILE, "w") as fp:
11087
print(record.model_dump_json(indent=4), file=fp)

0 commit comments

Comments
 (0)