feat(updater): 引入 24 小时静默更新检测与一键升级功能

- 新增 `updater` 模块实现 GitHub Release 版本异步检查与本地缓存
- 新增 `git air update` 与 `git air version` 命令支持一键升级及版本检查
- 优化 CLI 终端交互,在首页渲染时提供友好的新版本升级提示卡片
This commit is contained in:
unclesam committed 2026-08-22 17:07:02 +08:00
1 parent 10cbfc39a5
commit 7632652b7b
9 files changed
+398 -64

No files matched your search

+14 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/unclesam-ly/git-air/internal/llm"
"github.com/unclesam-ly/git-air/internal/reviewer"
"github.com/unclesam-ly/git-air/internal/ui"
"github.com/unclesam-ly/git-air/internal/updater"
)
var rootCmd = &cobra.Command{
@@ -21,6 +22,9 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
// 后台非阻塞异步检查新版本
updateCh := updater.CheckAsync()
// 1. 检查并获取 Diff
if flagCached, _ := cmd.Flags().GetBool("cached"); flagCached {
args = append([]string{"--cached"}, args...)
@@ -96,7 +100,16 @@ var rootCmd = &cobra.Command{
ui.PrintFooter()
// 8. 门禁阻断检测 (Pre-commit 拦截逻辑)
// 8. 检查新版本提醒(非阻塞)
select {
case info := <-updateCh:
if info != nil {
ui.PrintUpdateBanner(info.CurrentVersion, info.LatestVersion, info.ReleaseURL)
}
default:
}
// 9. 门禁阻断检测 (Pre-commit 拦截逻辑)
outText := reviewOutput.String()
hasBlocker := strings.Contains(outText, "[BLOCKER]") || strings.Contains(outText, "[CRITICAL]")
hasReject := strings.Contains(outText, "[REJECT]")
+35
View File
@@ -0,0 +1,35 @@
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/unclesam-ly/git-air/internal/ui"
"github.com/unclesam-ly/git-air/internal/updater"
)
var updateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"upgrade"},
Short: "一键升级 git-air 到最新版本",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("🔍 正在检查并拉取最新版本 (当前: %s)...\n", updater.CurrentVersion)
updateCmd := exec.Command("go", "install", "github.com/unclesam-ly/git-air@latest")
updateCmd.Stdout = os.Stdout
updateCmd.Stderr = os.Stderr
if err := updateCmd.Run(); err != nil {
ui.PrintError("升级失败: %v (提示: 请确保已安装 Go 并配置环境变量)", err)
return
}
ui.PrintSuccess("git-air 已成功升级至最新版本!🎉")
},
}
func init() {
rootCmd.AddCommand(updateCmd)
}
+38
View File
@@ -0,0 +1,38 @@
package cmd
import (
"fmt"
"runtime"
"github.com/spf13/cobra"
"github.com/unclesam-ly/git-air/internal/ui"
"github.com/unclesam-ly/git-air/internal/updater"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "查看当前 git-air 版本信息及更新检测",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("git-air %s (%s/%s, Go %s)\n",
updater.CurrentVersion,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
)
checkFlag, _ := cmd.Flags().GetBool("check")
if checkFlag {
info := updater.CheckUpdate(true)
if info != nil {
ui.PrintUpdateBanner(info.CurrentVersion, info.LatestVersion, info.ReleaseURL)
} else {
ui.PrintSuccess("当前已是最新版本!")
}
}
},
}
func init() {
versionCmd.Flags().BoolP("check", "c", true, "向 GitHub 检查是否有可用新版本")
rootCmd.AddCommand(versionCmd)
}