Skip to content

Commit d020de0

Browse files
committed
feat(cmd): 重构命令执行,使用 RunE 进行错误处理,并在主程序中初始化日志记录器
1 parent 9f771b5 commit d020de0

File tree

6 files changed

+40
-50
lines changed

6 files changed

+40
-50
lines changed

cmd/daemon.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,30 @@ func CreateDaemonCmd() *cobra.Command {
2121
Use: "daemon",
2222
Short: "启动守护进程(后台运行)",
2323
Long: "在后台启动证书部署守护进程,进程崩溃或更新后将自动重启",
24-
Run: func(cmd *cobra.Command, args []string) {
24+
RunE: func(cmd *cobra.Command, args []string) error {
2525
// 检查是否已经在运行,如果是则先停止
2626
if IsRunning() {
2727
fmt.Println("守护进程已在运行,正在重启...")
2828
if err := StopDaemon(); err != nil {
29-
fmt.Printf("停止失败: %v\n", err)
30-
os.Exit(1)
29+
return fmt.Errorf("停止守护进程失败: %w", err)
3130
}
3231
time.Sleep(2 * time.Second)
3332
}
3433

3534
execPath, err := os.Executable()
3635
if err != nil {
37-
fmt.Printf("获取可执行文件路径失败: %v\n", err)
38-
os.Exit(1)
36+
return fmt.Errorf("获取可执行文件路径失败: %w", err)
3937
}
4038

4139
supervisorCmd := exec.Command(execPath, "_supervisor", "-c", ConfigFile)
4240
if err := supervisorCmd.Start(); err != nil {
43-
fmt.Printf("启动失败: %v\n", err)
44-
os.Exit(1)
41+
return fmt.Errorf("启动守护进程失败: %w", err)
4542
}
4643

4744
time.Sleep(500 * time.Millisecond)
4845

4946
if !IsRunning() {
50-
fmt.Println("启动失败")
51-
os.Exit(1)
47+
return fmt.Errorf("守护进程启动失败")
5248
}
5349

5450
fmt.Println("守护进程已启动")
@@ -67,6 +63,7 @@ func CreateDaemonCmd() *cobra.Command {
6763
}()
6864

6965
time.Sleep(100 * time.Millisecond)
66+
return nil
7067
},
7168
}
7269
}

cmd/restart.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,32 @@ func CreateRestartCmd() *cobra.Command {
1515
Use: "restart",
1616
Short: "重启守护进程",
1717
Long: "重启证书部署守护进程",
18-
Run: func(cmd *cobra.Command, args []string) {
18+
RunE: func(cmd *cobra.Command, args []string) error {
1919
if IsRunning() {
2020
if err := StopDaemon(); err != nil {
21-
fmt.Printf("停止失败: %v\n", err)
22-
os.Exit(1)
21+
return fmt.Errorf("停止守护进程失败: %w", err)
2322
}
2423
time.Sleep(2 * time.Second)
2524
}
2625

2726
execPath, err := os.Executable()
2827
if err != nil {
29-
fmt.Printf("获取可执行文件路径失败: %v\n", err)
30-
os.Exit(1)
28+
return fmt.Errorf("获取可执行文件路径失败: %w", err)
3129
}
3230

3331
supervisorCmd := exec.Command(execPath, "_supervisor", "-c", ConfigFile)
3432
if err := supervisorCmd.Start(); err != nil {
35-
fmt.Printf("启动失败: %v\n", err)
36-
os.Exit(1)
33+
return fmt.Errorf("启动守护进程失败: %w", err)
3734
}
3835

3936
time.Sleep(500 * time.Millisecond)
4037

4138
if !IsRunning() {
42-
fmt.Println("启动失败")
43-
os.Exit(1)
39+
return fmt.Errorf("守护进程启动失败")
4440
}
4541

4642
fmt.Println("守护进程已重启")
43+
return nil
4744
},
4845
}
4946
}

cmd/start.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"os/signal"
78
"path/filepath"
@@ -19,13 +20,13 @@ func CreateStartCmd() *cobra.Command {
1920
Use: "start",
2021
Short: "启动守护进程(前台运行)",
2122
Long: "在前台启动证书部署守护进程,用于调试",
22-
Run: func(cmd *cobra.Command, args []string) {
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
logger.Init()
25+
2326
if err := config.Init(ConfigFile); err != nil {
24-
logger.Fatal("初始化配置失败", "error", err)
27+
return fmt.Errorf("初始化配置失败: %w", err)
2528
}
2629

27-
logger.Init()
28-
2930
// 检查更新标记并清理(程序同级目录)
3031
execPath, _ := os.Executable()
3132
execDir := filepath.Dir(execPath)
@@ -49,6 +50,7 @@ func CreateStartCmd() *cobra.Command {
4950
logger.Info("停止中...")
5051
cancel()
5152
logger.Info("已停止")
53+
return nil
5254
},
5355
}
5456
}

cmd/stop.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/spf13/cobra"
87
)
@@ -13,18 +12,18 @@ func CreateStopCmd() *cobra.Command {
1312
Use: "stop",
1413
Short: "停止守护进程",
1514
Long: "停止正在运行的证书部署守护进程",
16-
Run: func(cmd *cobra.Command, args []string) {
15+
RunE: func(cmd *cobra.Command, args []string) error {
1716
if !IsRunning() {
1817
fmt.Println("守护进程未运行")
19-
return
18+
return nil
2019
}
2120

2221
if err := StopDaemon(); err != nil {
23-
fmt.Printf("停止失败: %v\n", err)
24-
os.Exit(1)
22+
return fmt.Errorf("停止守护进程失败: %w", err)
2523
}
2624

2725
fmt.Println("守护进程已停止")
26+
return nil
2827
},
2928
}
3029
}

cmd/update.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ func CreateCheckUpdateCmd() *cobra.Command {
1717
Use: "check-update",
1818
Short: "检查是否有新版本",
1919
Long: "检查 GitHub 是否有新版本可用",
20-
Run: func(cmd *cobra.Command, args []string) {
20+
RunE: func(cmd *cobra.Command, args []string) error {
2121
ctx := context.Background()
2222

2323
fmt.Println("正在检查更新...")
2424
info, err := updater.CheckUpdate(ctx)
2525
if err != nil {
26-
fmt.Printf("检查失败: %v\n", err)
27-
os.Exit(1)
26+
return fmt.Errorf("检查更新失败: %w", err)
2827
}
2928

3029
fmt.Printf("当前版本: %s\n", info.CurrentVersion)
@@ -36,6 +35,7 @@ func CreateCheckUpdateCmd() *cobra.Command {
3635
} else {
3736
fmt.Println("\n当前已是最新版本")
3837
}
38+
return nil
3939
},
4040
}
4141
}
@@ -46,37 +46,33 @@ func CreateUpdateCmd() *cobra.Command {
4646
Use: "update",
4747
Short: "更新到最新版本",
4848
Long: "从 GitHub Release 下载并更新到最新版本,如果守护进程正在运行则自动重启",
49-
Run: func(cmd *cobra.Command, args []string) {
49+
RunE: func(cmd *cobra.Command, args []string) error {
5050
ctx := context.Background()
5151

5252
fmt.Println("正在检查更新...")
5353
info, err := updater.CheckUpdate(ctx)
5454
if err != nil {
55-
fmt.Printf("检查失败: %v\n", err)
56-
os.Exit(1)
55+
return fmt.Errorf("检查更新失败: %w", err)
5756
}
5857

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

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

6665
wasRunning := IsRunning()
6766
if wasRunning {
6867
fmt.Println("正在停止守护进程...")
6968
if err := StopDaemon(); err != nil {
70-
fmt.Printf("停止失败: %v\n", err)
71-
fmt.Println("请手动停止后再更新")
72-
os.Exit(1)
69+
return fmt.Errorf("停止守护进程失败,请手动停止后再更新: %w", err)
7370
}
7471
time.Sleep(2 * time.Second)
7572
}
7673

7774
if err := updater.PerformUpdate(ctx, info); err != nil {
78-
fmt.Printf("更新失败: %v\n", err)
79-
os.Exit(1)
75+
return fmt.Errorf("更新失败: %w", err)
8076
}
8177

8278
fmt.Println("\n更新成功!")
@@ -86,26 +82,22 @@ func CreateUpdateCmd() *cobra.Command {
8682

8783
execPath, err := os.Executable()
8884
if err != nil {
89-
fmt.Printf("获取可执行文件路径失败: %v\n", err)
90-
fmt.Println("请手动启动: cert-deploy daemon")
91-
os.Exit(1)
85+
return fmt.Errorf("获取可执行文件路径失败,请手动启动: cert-deploy daemon: %w", err)
9286
}
9387

9488
restartCmd := exec.Command(execPath, "daemon", "-c", ConfigFile)
9589
if err := restartCmd.Start(); err != nil {
96-
fmt.Printf("启动失败: %v\n", err)
97-
fmt.Println("请手动启动: cert-deploy daemon")
98-
os.Exit(1)
90+
return fmt.Errorf("守护进程启动失败,请手动启动: cert-deploy daemon: %w", err)
9991
}
10092

10193
time.Sleep(1 * time.Second)
10294

10395
if !IsRunning() {
104-
fmt.Println("启动失败,请手动启动: cert-deploy daemon")
105-
os.Exit(1)
96+
return fmt.Errorf("守护进程启动失败,请手动启动: cert-deploy daemon")
10697
}
10798
fmt.Println("守护进程已重启")
10899
}
100+
return nil
109101
},
110102
}
111103
}

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"os"
66

77
"github.com/orange-juzipi/cert-deploy/cmd"
8+
"github.com/orange-juzipi/cert-deploy/pkg/logger"
89
)
910

1011
func main() {
12+
logger.Init()
13+
1114
rootCmd := cmd.CreateRootCmd()
1215

1316
if err := rootCmd.Execute(); err != nil {

0 commit comments

Comments
 (0)