Skip to content

Commit 810af65

Browse files
authored
Merge pull request #529 from Robin-Van-de-Merghel/robin-refactor-cli
refactor: Refactored the config cli
2 parents b787d8d + d78d07d commit 810af65

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

diracx-cli/src/diracx/cli/internal/config.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@
2424
app = AsyncTyper()
2525

2626

27+
def get_repo_path(config_repo_str: str) -> Path:
28+
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo_str)
29+
if config_repo.scheme != "git+file" or config_repo.path is None:
30+
raise NotImplementedError("Only git+file:// URLs are supported")
31+
32+
repo_path = Path(config_repo.path)
33+
34+
return repo_path
35+
36+
37+
def get_config_from_repo_path(repo_path: Path) -> Config:
38+
return ConfigSource.create_from_url(backend_url=repo_path).read_config()
39+
40+
2741
@app.command()
2842
def generate_cs(config_repo: str):
2943
"""Generate a minimal DiracX configuration repository."""
3044
# TODO: The use of TypeAdapter should be moved in to typer itself
31-
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
32-
if config_repo.scheme != "git+file" or config_repo.path is None:
33-
raise NotImplementedError("Only git+file:// URLs are supported")
34-
repo_path = Path(config_repo.path)
45+
46+
repo_path = get_repo_path(config_repo)
47+
3548
if repo_path.exists() and list(repo_path.iterdir()):
3649
typer.echo(f"ERROR: Directory {repo_path} already exists", err=True)
3750
raise typer.Exit(1)
@@ -60,10 +73,8 @@ def add_vo(
6073
):
6174
"""Add a registry entry (vo) to an existing configuration repository."""
6275
# TODO: The use of TypeAdapter should be moved in to typer itself
63-
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
64-
if config_repo.scheme != "git+file" or config_repo.path is None:
65-
raise NotImplementedError("Only git+file:// URLs are supported")
66-
repo_path = Path(config_repo.path)
76+
repo_path = get_repo_path(config_repo)
77+
config = get_config_from_repo_path(repo_path)
6778

6879
# A VO should at least contain a default group
6980
new_registry = RegistryConfig(
@@ -77,8 +88,6 @@ def add_vo(
7788
},
7889
)
7990

80-
config = ConfigSource.create_from_url(backend_url=repo_path).read_config()
81-
8291
if vo in config.Registry:
8392
typer.echo(f"ERROR: VO {vo} already exists", err=True)
8493
raise typer.Exit(1)
@@ -103,15 +112,11 @@ def add_group(
103112
):
104113
"""Add a group to an existing vo in the configuration repository."""
105114
# TODO: The use of TypeAdapter should be moved in to typer itself
106-
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
107-
if config_repo.scheme != "git+file" or config_repo.path is None:
108-
raise NotImplementedError("Only git+file:// URLs are supported")
109-
repo_path = Path(config_repo.path)
115+
repo_path = get_repo_path(config_repo)
116+
config = get_config_from_repo_path(repo_path)
110117

111118
new_group = GroupConfig(Properties=set(properties), Quota=None, Users=set())
112119

113-
config = ConfigSource.create_from_url(backend_url=repo_path).read_config()
114-
115120
if vo not in config.Registry:
116121
typer.echo(f"ERROR: Virtual Organization {vo} does not exist", err=True)
117122
raise typer.Exit(1)
@@ -139,16 +144,11 @@ def add_user(
139144
):
140145
"""Add a user to an existing vo and group."""
141146
# TODO: The use of TypeAdapter should be moved in to typer itself
142-
config_repo = TypeAdapter(ConfigSourceUrl).validate_python(config_repo)
143-
if config_repo.scheme != "git+file" or config_repo.path is None:
144-
raise NotImplementedError("Only git+file:// URLs are supported")
145-
146-
repo_path = Path(config_repo.path)
147+
repo_path = get_repo_path(config_repo)
148+
config = get_config_from_repo_path(repo_path)
147149

148150
new_user = UserConfig(PreferedUsername=preferred_username)
149151

150-
config = ConfigSource.create_from_url(backend_url=repo_path).read_config()
151-
152152
if vo not in config.Registry:
153153
typer.echo(f"ERROR: Virtual Organization {vo} does not exist", err=True)
154154
raise typer.Exit(1)

0 commit comments

Comments
 (0)