Skip to content

Commit b67207b

Browse files
committed
fix(config): 默认使用安装目录配置
1 parent c0bbbc8 commit b67207b

4 files changed

Lines changed: 34 additions & 7 deletions

File tree

cmd/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func CreateDaemonCmd() *cobra.Command {
6161
info, err := updater.CheckUpdate(ctx)
6262
if err == nil && info.HasUpdate {
6363
fmt.Printf("\n发现新版本: %s -> %s\n", info.CurrentVersion, info.LatestVersion)
64-
fmt.Println("执行 './anssl update' 进行更新")
64+
fmt.Println("执行 'anssl update' 进行更新")
6565
}
6666
}()
6767

cmd/root.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ var (
1414
ConfigFile string
1515
)
1616

17+
const (
18+
localDefaultConfigFile = "config.yaml"
19+
installedDefaultConfigFile = "/opt/anssl/config.yaml"
20+
)
21+
1722
// CreateRootCmd 创建根命令
1823
func CreateRootCmd() *cobra.Command {
1924
rootCmd := &cobra.Command{
@@ -37,13 +42,24 @@ func CreateRootCmd() *cobra.Command {
3742
rootCmd.AddCommand(CreateVersionCmd())
3843

3944
// 全局标志
40-
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config", "c", "config.yaml", "配置文件路径")
45+
rootCmd.PersistentFlags().StringVarP(&ConfigFile, "config", "c", defaultConfigFile(), "配置文件路径")
4146

4247
return rootCmd
4348
}
4449

4550
// 辅助函数
4651

52+
// defaultConfigFile 返回默认配置文件路径,本地开发配置优先于安装目录配置。
53+
func defaultConfigFile() string {
54+
if _, err := os.Stat(localDefaultConfigFile); err == nil {
55+
return localDefaultConfigFile
56+
}
57+
if _, err := os.Stat(installedDefaultConfigFile); err == nil {
58+
return installedDefaultConfigFile
59+
}
60+
return localDefaultConfigFile
61+
}
62+
4763
// GetPIDFile 获取PID文件路径
4864
func GetPIDFile() string {
4965
homeDir, err := os.UserHomeDir()

cmd/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func CreateCheckUpdateCmd() *cobra.Command {
3737

3838
if info.HasUpdate {
3939
fmt.Println("\n发现新版本!")
40-
fmt.Println("执行 './anssl update' 进行更新")
40+
fmt.Println("执行 'anssl update' 进行更新")
4141
} else {
4242
fmt.Println("\n当前已是最新版本")
4343
}
@@ -103,13 +103,13 @@ func CreateUpdateCmd() *cobra.Command {
103103

104104
restartCmd := exec.Command(execPath, "daemon", "-c", ConfigFile)
105105
if err := restartCmd.Start(); err != nil {
106-
return fmt.Errorf("守护进程启动失败,请手动启动: anssl daemon: %w", err)
106+
return fmt.Errorf("守护进程启动失败,请手动启动: anssl daemon -c %s: %w", ConfigFile, err)
107107
}
108108

109109
time.Sleep(1 * time.Second)
110110

111111
if !IsRunning() {
112-
return fmt.Errorf("守护进程启动失败,请手动启动: anssl daemon")
112+
return fmt.Errorf("守护进程启动失败,请手动启动: anssl daemon -c %s", ConfigFile)
113113
}
114114
fmt.Println("守护进程已重启")
115115
}

scripts/install.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ print_success_banner() {
335335
EOF
336336
}
337337

338+
# print_next_step 输出首次启动命令,默认命令目录下省略绝对二进制路径。
339+
print_next_step() {
340+
if [ "$INSTALL_DIR" = "/usr/local/bin" ]; then
341+
command_bin="${BIN_NAME}"
342+
else
343+
command_bin="${INSTALL_DIR}/${BIN_NAME}"
344+
fi
345+
346+
printf '%s\n' "下一步:编辑 ${CONFIG_DIR}/config.yaml,填写 server.accessKey 后运行:"
347+
printf '%s\n' "${command_bin} daemon -c ${CONFIG_DIR}/config.yaml"
348+
}
349+
338350
# main 执行下载、校验、解压和安装流程。
339351
main() {
340352
parse_args "$@"
@@ -381,8 +393,7 @@ main() {
381393
install_config
382394

383395
print_success_banner
384-
printf '%s\n' "下一步:编辑 ${CONFIG_DIR}/config.yaml,填写 server.accessKey 后运行:"
385-
printf '%s\n' "${BIN_NAME} daemon -c ${CONFIG_DIR}/config.yaml"
396+
print_next_step
386397
}
387398

388399
main "$@"

0 commit comments

Comments
 (0)