diff --git a/mindcli/cli.py b/mindcli/cli.py index e11cbc7..9be3065 100644 --- a/mindcli/cli.py +++ b/mindcli/cli.py @@ -217,16 +217,56 @@ def record_status(): click.echo("⏹️ Health Server 未运行") -def _get_vendor_commit() -> str: - """读取 _vendor/HERMES_COMMIT 文件获取 vendor 版本。""" - commit_file = os.path.join(mindcli._VENDOR_DIR, "HERMES_COMMIT") +@main.command() +def update(): + """检查并升级到最新版本。""" + import subprocess + + click.echo(f"当前版本: v{mindcli.__version__}") + click.echo("正在检查最新版本...") + + remoteVersion = _check_remote_version() + if remoteVersion and remoteVersion != mindcli.__version__: + click.echo(f"⬆️ 发现新版本 v{remoteVersion}") + click.echo("正在升级...") + installCmd = "pip install --upgrade git+https://git.brainwork.club/lidf/MindOS_CLI.git" + result = subprocess.run(installCmd.split(), capture_output=True, text=True) + if result.returncode == 0: + click.echo(f"✅ 已升级到 v{remoteVersion}") + click.echo(" 请重启 Mind CLI(mind start)以生效") + else: + click.echo(f"❌ 升级失败: {result.stderr[:200]}") + elif remoteVersion: + click.echo(f"✅ 已是最新版本 v{mindcli.__version__}") + else: + click.echo("⚠️ 无法检查远端版本(网络问题?)") + + +def _check_remote_version(): + """从 versions.json 获取远端 CLI 最新版本号。""" + import urllib.request + versionsUrl = "https://dl.brainwork.club/mindos-next/versions.json" try: - with open(commit_file) as f: - return f.read().strip() + req = urllib.request.Request(versionsUrl) + with urllib.request.urlopen(req, timeout=5) as resp: + data = json.loads(resp.read()) + return data.get("cli", {}).get("version") + except Exception: + return None + + +def _get_vendor_commit() -> str: + """读取 _vendor/VENDOR_COMMIT 文件获取 vendor 版本。""" + commitFile = os.path.join(mindcli._VENDOR_DIR, "VENDOR_COMMIT") + try: + with open(commitFile) as f: + for line in f: + if line.startswith("commit:"): + return line.split(":", 1)[1].strip() + return "unknown" except FileNotFoundError: return "unknown" if __name__ == "__main__": main() -