Skip to content

Commit c21b0b1

Browse files
committed
fix: Properly handle flags to UV subcommands by making it a subcommand of our CLI
1 parent 9f90b60 commit c21b0b1

File tree

1 file changed

+130
-46
lines changed

1 file changed

+130
-46
lines changed

bin/uv-operations.py

Lines changed: 130 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414

1515
from cyclopts import App, Parameter
1616

17-
app = App(
18-
help="Run uv commands across all code locations",
19-
default_parameter=Parameter(negative=""),
20-
)
17+
app = App(help="Run uv commands across all code locations")
2118

2219

2320
class Colors:
@@ -74,51 +71,21 @@ def run_uv_command(location: Path, uv_args: list[str], verbose: bool = False) ->
7471
sys.exit(1)
7572

7673

77-
@app.default
78-
def run_uv_operations( # noqa: C901
79-
*uv_command: Annotated[
80-
str,
81-
Parameter(
82-
show=False,
83-
help="The uv command and arguments to run (e.g., sync, lock --upgrade)",
84-
),
85-
],
86-
code_locations_dir: Annotated[
87-
Path,
88-
Parameter(
89-
help="Base directory containing code locations",
90-
),
91-
] = Path(__file__).parent.parent / "dg_projects",
92-
continue_on_error: Annotated[
93-
bool,
94-
Parameter(
95-
help="Continue running even if some locations fail",
96-
),
97-
] = False,
98-
verbose: Annotated[
99-
bool,
100-
Parameter(
101-
help="Print verbose output",
102-
),
103-
] = False,
74+
def run_across_locations(
75+
uv_args: list[str],
76+
code_locations_dir: Path,
77+
continue_on_error: bool,
78+
verbose: bool,
10479
) -> None:
10580
"""
106-
Run uv commands across all code locations.
81+
Run a uv command across all code locations.
10782
108-
Examples:
109-
uv-operations sync
110-
uv-operations lock --upgrade
111-
uv-operations build
112-
uv-operations pip list
83+
Args:
84+
uv_args: Arguments to pass to uv.
85+
code_locations_dir: Base directory containing code locations.
86+
continue_on_error: Whether to continue if a location fails.
87+
verbose: Whether to print verbose output.
11388
"""
114-
if not uv_command:
115-
print(f"{Colors.RED}Error: No uv command specified{Colors.NC}")
116-
print("\nUsage examples:")
117-
print(" uv-operations sync")
118-
print(" uv-operations lock --upgrade")
119-
print(" uv-operations build")
120-
sys.exit(1)
121-
12289
base_dir = code_locations_dir.resolve()
12390

12491
if not base_dir.exists():
@@ -149,7 +116,7 @@ def run_uv_operations( # noqa: C901
149116
print(f"{Colors.BLUE}Processing: {location.name}{Colors.NC}")
150117
print(f"{Colors.BLUE}{'=' * 40}{Colors.NC}")
151118

152-
success = run_uv_command(location, list(uv_command), verbose=verbose)
119+
success = run_uv_command(location, uv_args, verbose=verbose)
153120

154121
if success:
155122
print(f"{Colors.GREEN}✓ Success: {location.name}{Colors.NC}")
@@ -187,5 +154,122 @@ def run_uv_operations( # noqa: C901
187154
print(f"\n{Colors.GREEN}All operations completed successfully!{Colors.NC}")
188155

189156

157+
@app.command
158+
def sync(
159+
code_locations_dir: Annotated[
160+
Path,
161+
Parameter(
162+
help="Base directory containing code locations",
163+
),
164+
] = Path(__file__).parent.parent / "dg_projects",
165+
continue_on_error: Annotated[
166+
bool,
167+
Parameter(
168+
help="Continue running even if some locations fail",
169+
),
170+
] = False,
171+
verbose: Annotated[
172+
bool,
173+
Parameter(
174+
help="Print verbose output",
175+
),
176+
] = False,
177+
) -> None:
178+
"""Sync dependencies in all code locations."""
179+
run_across_locations(["sync"], code_locations_dir, continue_on_error, verbose)
180+
181+
182+
@app.command
183+
def lock(
184+
upgrade: Annotated[
185+
bool,
186+
Parameter(
187+
help="Upgrade all dependencies to latest versions",
188+
),
189+
] = False,
190+
code_locations_dir: Annotated[
191+
Path,
192+
Parameter(
193+
help="Base directory containing code locations",
194+
),
195+
] = Path(__file__).parent.parent / "dg_projects",
196+
continue_on_error: Annotated[
197+
bool,
198+
Parameter(
199+
help="Continue running even if some locations fail",
200+
),
201+
] = False,
202+
verbose: Annotated[
203+
bool,
204+
Parameter(
205+
help="Print verbose output",
206+
),
207+
] = False,
208+
) -> None:
209+
"""Lock dependencies in all code locations."""
210+
uv_args = ["lock"]
211+
if upgrade:
212+
uv_args.append("--upgrade")
213+
run_across_locations(uv_args, code_locations_dir, continue_on_error, verbose)
214+
215+
216+
@app.command
217+
def build(
218+
code_locations_dir: Annotated[
219+
Path,
220+
Parameter(
221+
help="Base directory containing code locations",
222+
),
223+
] = Path(__file__).parent.parent / "dg_projects",
224+
continue_on_error: Annotated[
225+
bool,
226+
Parameter(
227+
help="Continue running even if some locations fail",
228+
),
229+
] = False,
230+
verbose: Annotated[
231+
bool,
232+
Parameter(
233+
help="Print verbose output",
234+
),
235+
] = False,
236+
) -> None:
237+
"""Build packages in all code locations."""
238+
run_across_locations(["build"], code_locations_dir, continue_on_error, verbose)
239+
240+
241+
@app.command
242+
def pip(
243+
pip_command: Annotated[
244+
str,
245+
Parameter(
246+
help="The pip subcommand to run (e.g., list, freeze)",
247+
),
248+
],
249+
code_locations_dir: Annotated[
250+
Path,
251+
Parameter(
252+
help="Base directory containing code locations",
253+
),
254+
] = Path(__file__).parent.parent / "dg_projects",
255+
continue_on_error: Annotated[
256+
bool,
257+
Parameter(
258+
help="Continue running even if some locations fail",
259+
),
260+
] = False,
261+
verbose: Annotated[
262+
bool,
263+
Parameter(
264+
help="Print verbose output",
265+
),
266+
] = False,
267+
) -> None:
268+
"""Run pip commands in all code locations."""
269+
run_across_locations(
270+
["pip", pip_command], code_locations_dir, continue_on_error, verbose
271+
)
272+
273+
190274
if __name__ == "__main__":
191275
app()

0 commit comments

Comments
 (0)