99 lines
3.0 KiB
Python
99 lines
3.0 KiB
Python
"""Git utilities including proxy management."""
|
|
|
|
import subprocess
|
|
|
|
import click
|
|
|
|
from mycli.config import config
|
|
|
|
|
|
def _get_proxies():
|
|
"""Get git proxies from config."""
|
|
return config.get_all_git_proxies()
|
|
|
|
|
|
@click.group(name="git")
|
|
def git_cmd():
|
|
"""Git utilities."""
|
|
pass
|
|
|
|
|
|
@git_cmd.command("proxy")
|
|
@click.argument("name", required=False)
|
|
@click.option("--unset", "-u", is_flag=True, help="Unset proxy")
|
|
@click.option("--status", "-s", is_flag=True, help="Show current proxy status")
|
|
def proxy_cmd(name, unset, status):
|
|
"""Manage git proxy settings.
|
|
|
|
Examples:
|
|
mycli git proxy fastgithub # Set fastgithub proxy
|
|
mycli git proxy pandafan # Set pandafan proxy
|
|
mycli git proxy -u # Unset proxy
|
|
mycli git proxy -s # Show status
|
|
"""
|
|
if status or (not name and not unset):
|
|
_show_status()
|
|
return
|
|
|
|
if unset:
|
|
subprocess.run(["git", "config", "--global", "--unset", "http.proxy"], capture_output=True)
|
|
subprocess.run(["git", "config", "--global", "--unset", "https.proxy"], capture_output=True)
|
|
click.secho("Git proxy unset", fg="green")
|
|
return
|
|
|
|
proxies = _get_proxies()
|
|
if name not in proxies:
|
|
click.echo(f"Unknown proxy: {name}", err=True)
|
|
click.echo(f"Available: {', '.join(proxies.keys())}", err=True)
|
|
raise click.Exit(1)
|
|
|
|
proxy_url = proxies[name]
|
|
subprocess.run(["git", "config", "--global", "http.proxy", proxy_url], check=True)
|
|
subprocess.run(["git", "config", "--global", "https.proxy", proxy_url], check=True)
|
|
click.secho(f"Git proxy set to {name}: {proxy_url}", fg="green")
|
|
|
|
|
|
def _show_status():
|
|
"""Show current git proxy status."""
|
|
result = subprocess.run(
|
|
["git", "config", "--global", "http.proxy"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
http_proxy = result.stdout.strip() if result.returncode == 0 else "(not set)"
|
|
|
|
result = subprocess.run(
|
|
["git", "config", "--global", "https.proxy"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
https_proxy = result.stdout.strip() if result.returncode == 0 else "(not set)"
|
|
|
|
# 检查当前设置的代理名称
|
|
proxies = _get_proxies()
|
|
current_name = None
|
|
for proxy_name, url in proxies.items():
|
|
if http_proxy == url:
|
|
current_name = proxy_name
|
|
break
|
|
|
|
click.secho("Git proxy status:", fg="cyan")
|
|
if current_name:
|
|
click.echo(f" Active: {current_name}")
|
|
click.echo(f" http.proxy: {http_proxy}")
|
|
click.echo(f" https.proxy: {https_proxy}")
|
|
|
|
click.secho("\nAvailable proxies:", fg="cyan")
|
|
for proxy_name, url in proxies.items():
|
|
marker = "*" if proxy_name == current_name else " "
|
|
click.echo(f" [{marker}] {proxy_name:12} {url}")
|
|
|
|
|
|
@git_cmd.command("proxy-list")
|
|
def proxy_list():
|
|
"""List available git proxies."""
|
|
proxies = _get_proxies()
|
|
click.secho("Available proxies:", fg="cyan")
|
|
for name, url in proxies.items():
|
|
click.echo(f" {name:12} {url}")
|