feat: persist config from uvx bootstrap
This commit is contained in:
+41
-1
@@ -8,6 +8,7 @@ import importlib.metadata
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
@@ -16,7 +17,7 @@ from typing import Any
|
||||
|
||||
|
||||
APP_NAME = "shusub2"
|
||||
FALLBACK_VERSION = "0.1.6"
|
||||
FALLBACK_VERSION = "0.1.7"
|
||||
DEFAULT_API_URL = "http://127.0.0.1:18318/api/tui/accounts"
|
||||
DEFAULT_CONFIG_FILE = "~/.config/shusub2/api-url"
|
||||
DEFAULT_STATUS_CONFIG_FILE = "~/.config/shusub2/status-url"
|
||||
@@ -28,6 +29,7 @@ MONITOR_OK_STATUSES = {"operational", "ok", "success"}
|
||||
MONITOR_FAILED_STATUSES = {"error", "failed", "failure"}
|
||||
MONITOR_STOPWORDS = {"response", "responses", "monitor"}
|
||||
INSTALL_COMMAND = "uv tool install --force git+https://gitea.shujk.top/shujakuin/shusub2.git"
|
||||
INSTALL_COMMAND_ARGS = ["uv", "tool", "install", "--force", "git+https://gitea.shujk.top/shujakuin/shusub2.git"]
|
||||
|
||||
|
||||
def env_int(name: str, default: int, *, minimum: int = 1) -> int:
|
||||
@@ -68,6 +70,36 @@ def default_status_url() -> str:
|
||||
)
|
||||
|
||||
|
||||
def config_file_path() -> Path:
|
||||
return Path(os.environ.get("SHUSUB2_API_URL_FILE", DEFAULT_CONFIG_FILE)).expanduser()
|
||||
|
||||
|
||||
def write_api_url_config(api_url: str) -> Path:
|
||||
value = str(api_url or "").strip()
|
||||
if not value:
|
||||
raise ValueError("api url is empty")
|
||||
path = config_file_path()
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
path.parent.chmod(0o700)
|
||||
except OSError:
|
||||
pass
|
||||
path.write_text(value + "\n", encoding="utf-8")
|
||||
try:
|
||||
path.chmod(0o600)
|
||||
except OSError:
|
||||
pass
|
||||
return path
|
||||
|
||||
|
||||
def run_install_command() -> int:
|
||||
try:
|
||||
return subprocess.run(INSTALL_COMMAND_ARGS, check=False).returncode
|
||||
except FileNotFoundError:
|
||||
print("uv command not found; install uv first, then run:", INSTALL_COMMAND, file=sys.stderr)
|
||||
return 127
|
||||
|
||||
|
||||
def inferred_status_url(api_url: str) -> str:
|
||||
parsed = urllib.parse.urlparse(str(api_url or "").strip())
|
||||
if not parsed.scheme or not parsed.netloc:
|
||||
@@ -624,6 +656,8 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="Sub2API quota and daily usage TUI")
|
||||
parser.add_argument("--api-url", default=default_api_url())
|
||||
parser.add_argument("--status-url", default=default_status_url(), help="optional sub2api-status /api/status URL for channel monitor health")
|
||||
parser.add_argument("--save-config", action="store_true", help="persist --api-url to ~/.config/shusub2/api-url before running")
|
||||
parser.add_argument("--install", action="store_true", help="persist --api-url, install shusub2 as a uv tool, then exit")
|
||||
parser.add_argument("--version-check-url", default=os.environ.get("SHUSUB2_VERSION_CHECK_URL", DEFAULT_VERSION_CHECK_URL))
|
||||
parser.add_argument(
|
||||
"--version-check-timeout",
|
||||
@@ -645,6 +679,12 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
args = build_parser().parse_args(argv)
|
||||
status_url = str(args.status_url or "").strip() or inferred_status_url(args.api_url)
|
||||
if args.save_config or args.install:
|
||||
config_path = write_api_url_config(args.api_url)
|
||||
print(f"saved api url to {config_path}")
|
||||
if args.install:
|
||||
print("installing shusub2 with uv tool...")
|
||||
return run_install_command()
|
||||
version_message = check_version_update(
|
||||
args.version_check_url,
|
||||
max(1, args.version_check_timeout),
|
||||
|
||||
Reference in New Issue
Block a user