-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
183 lines (156 loc) · 5.83 KB
/
Copy pathcli.py
File metadata and controls
183 lines (156 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""DevForge unified CLI entry point."""
import builtins as _builtins
import subprocess
import sys
import typer
from devforge import TOOLS, __version__
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
app = typer.Typer(
name="devforge",
help="DevForge — unified CLI for developer tools.",
rich_markup_mode="rich",
)
console = Console()
def _show_version(value: bool) -> None:
if value:
console.print(f"[bold]devforge[/] v{__version__} — DevForge CLI")
raise typer.Exit()
@app.callback()
def main_callback(
version: bool = typer.Option(
False,
"--version",
"-V",
help="Show version and exit.",
callback=_show_version,
is_eager=True,
),
):
pass
@app.command(name="tools")
def list_tools(
name: str | None = typer.Argument(None, help="Show details for a specific tool."),
):
"""List available DevForge CLI tools."""
if name:
tool = TOOLS.get(name)
if not tool:
console.print(f"[red]Unknown tool: {name}[/red]")
raise typer.Exit(code=1)
console.print(
Panel.fit(
f"[bold]{tool['icon']} {name}[/bold]\n"
f"Package: [cyan]{tool['package']}[/cyan]\n"
f"Description: {tool['description']}\n"
f"Pricing: [yellow]{tool['pricing']}[/yellow]\n"
f"Status: [green]{tool['status']}[/green]\n"
f"URL: {tool['url']}",
title=f"devforge {name}",
)
)
else:
table = Table(title="DevForge CLI Tools")
table.add_column("Command", style="cyan")
table.add_column("Package", style="green")
table.add_column("Description")
table.add_column("Pricing", style="yellow")
table.add_column("Status", style="bold")
for cmd, tool in TOOLS.items():
table.add_row(
tool["icon"] + " " + cmd,
tool["package"],
tool["description"],
tool["pricing"],
"✅" if tool["status"] == "ready" else tool["status"],
)
console.print(table)
console.print("\n[dim]Install individually:[/dim] [green]pip install devforge[guard][/green]")
console.print("[dim]Install all:[/dim] [green]pip install devforge[all][/green]")
@app.command()
def install(
tool: str = typer.Argument(..., help="Tool to install: " + ", ".join(TOOLS.keys()) + ", or 'all'"),
):
"""Install a DevForge tool."""
if tool == "all":
targets = _builtins.list(TOOLS.keys())
extras = ",".join(TOOLS.keys())
elif tool in TOOLS:
targets = [tool]
extras = tool
else:
console.print(f"[red]Unknown tool: {tool}[/red]")
console.print(f"Available: {', '.join(TOOLS.keys())}, 'all'")
raise typer.Exit(code=1)
pkg = f"devforge[{extras}]"
console.print(f"[yellow]Installing {pkg}...[/yellow]")
try:
result = subprocess.run([sys.executable, "-m", "pip", "install", pkg], capture_output=True, text=True)
if result.returncode == 0:
console.print(f"[green]Successfully installed:[/green] {', '.join(targets)}")
else:
console.print(f"[red]Installation failed:[/red] {result.stderr[:500]}")
raise typer.Exit(code=1)
except Exception as e:
console.print(f"[red]Error: {e}[/red]")
raise typer.Exit(code=1) from e
@app.command(name="versions")
def show_versions(
tool: str | None = typer.Argument(None, help="Check version of a specific tool."),
):
"""Show installed tool versions."""
if tool is not None and tool not in TOOLS:
console.print(f"[red]Unknown tool: {tool}[/red]")
console.print(f"Available: {', '.join(TOOLS.keys())}")
raise typer.Exit(code=1)
targets = [tool] if tool else _builtins.list(TOOLS.keys())
for t in targets:
info = TOOLS[t]
try:
result = subprocess.run(
[sys.executable, "-m", "pip", "show", info["package"]], capture_output=True, text=True
)
if result.returncode == 0:
for line in result.stdout.splitlines():
if line.startswith("Version:"):
ver = line.split(":", 1)[1].strip()
console.print(f"[cyan]{t:8}[/cyan] v{ver}")
break
else:
console.print(f"[dim]{t:8}[/dim] [red]not installed[/red]")
except Exception:
console.print(f"[dim]{t:8}[/dim] [red]error checking[/red]")
# Dynamically add subcommands for each tool
def _make_dispatch(tool_name: str):
"""Create a typer command that dispatches to the underlying tool CLI."""
pkg = TOOLS[tool_name]["package"]
def dispatch(
ctx: typer.Context,
args: list[str] = typer.Argument(None, help="Arguments to pass to the tool."), # noqa: B008
):
info = TOOLS.get(tool_name)
if not info:
console.print(f"[red]Unknown tool: {tool_name}[/red]")
raise typer.Exit(code=1)
try:
result = subprocess.run(
[sys.executable, "-m", info["package"].replace("-", "_")] + (args or []),
capture_output=False,
)
sys.exit(result.returncode)
except FileNotFoundError:
console.print(
f"[red]Tool '{tool_name}' not installed.[/red]\n"
f"Install with: [green]pip install devforge[{tool_name}][/green]"
)
raise typer.Exit(code=1) from None
dispatch.__name__ = tool_name
dispatch.__doc__ = f"Run `{pkg}` commands via the {tool_name} subcommand."
return dispatch
for cmd_name in TOOLS:
app.command(name=cmd_name)(_make_dispatch(cmd_name))
def main():
app()
if __name__ == "__main__":
main()