Skip to content

Commit 8acd57d

Browse files
committed
feat(update): 增强更新命令,获取可执行文件真实路径并解析符号链接
- 新增获取当前可执行文件真实路径的功能,确保更新命令的准确性 - 解析符号链接以获取真实文件系统路径,提升错误处理能力 - 优化更新检查逻辑,确保用户获得最新版本信息
1 parent e5547ce commit 8acd57d

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

cmd/update.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"path/filepath"
89
"time"
910

1011
"github.com/https-cert/deploy/internal/updater"
@@ -49,16 +50,27 @@ func CreateUpdateCmd() *cobra.Command {
4950
RunE: func(cmd *cobra.Command, args []string) error {
5051
ctx := context.Background()
5152

53+
// 获取当前可执行文件的真实路径
54+
execPath, err := os.Executable()
55+
if err != nil {
56+
return fmt.Errorf("获取可执行文件路径失败: %w", err)
57+
}
58+
// 解析符号链接,获取真实文件系统路径
59+
execPath, err = filepath.EvalSymlinks(execPath)
60+
if err != nil {
61+
return fmt.Errorf("解析可执行文件路径失败: %w", err)
62+
}
63+
5264
fmt.Println("正在检查更新...")
5365
info, err := updater.CheckUpdate(ctx)
5466
if err != nil {
5567
return fmt.Errorf("检查更新失败: %w", err)
5668
}
5769

58-
if !info.HasUpdate {
59-
fmt.Println("当前已是最新版本")
60-
return nil
61-
}
70+
if !info.HasUpdate {
71+
fmt.Println("当前已是最新版本")
72+
return nil
73+
}
6274

6375
fmt.Printf("发现新版本: %s -> %s\n", info.CurrentVersion, info.LatestVersion)
6476

@@ -80,11 +92,6 @@ func CreateUpdateCmd() *cobra.Command {
8092
if wasRunning {
8193
fmt.Println("正在重启守护进程...")
8294

83-
execPath, err := os.Executable()
84-
if err != nil {
85-
return fmt.Errorf("获取可执行文件路径失败,请手动启动: anssl daemon: %w", err)
86-
}
87-
8895
restartCmd := exec.Command(execPath, "daemon", "-c", ConfigFile)
8996
if err := restartCmd.Start(); err != nil {
9097
return fmt.Errorf("守护进程启动失败,请手动启动: anssl daemon: %w", err)

0 commit comments

Comments
 (0)