2 Commits
main ... v1.0.2

Author SHA1 Message Date
f176df4bc4 fix: 构建文件的版本动态获取 2025-12-22 14:26:20 +08:00
e94151cec5 fix: 添加读取用户目录下的配置文件 2025-12-22 14:21:11 +08:00
3 changed files with 33 additions and 7 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
bin
config.*
.idea
.idea
nul

View File

@@ -4,7 +4,7 @@ OUTPUT := ./bin/$(APP_NAME).exe
GO := go
# Version info
VERSION := 1.0.1
VERSION := $(shell git describe --tags --abbrev=0)
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
@@ -14,7 +14,7 @@ BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
# Detect Zig
ifeq ($(OS),Windows_NT)
ZIG_BIN := $(shell where zig 2>nul)
ZIG_BIN := $(shell where zig 2>/dev/null)
else
ZIG_BIN := $(shell which zig 2>/dev/null)
endif

View File

@@ -23,9 +23,15 @@ var exportCmd = &cobra.Command{
filePath, _ := cmd.Flags().GetString("file")
format, _ := cmd.Flags().GetString("format")
// 默认文件和格式
// 默认文件路径和格式
if filePath == "" {
filePath = "config.yaml"
// 获取用户的 Home 目录
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("获取用户主目录失败: %w", err)
}
// 默认导出到 Home 目录下的 myapp_config.yaml
filePath = filepath.Join(homeDir, "netctl", "config.yaml")
}
if format == "" {
format = "yaml"
@@ -42,9 +48,23 @@ var exportCmd = &cobra.Command{
filePath = strings.TrimSuffix(filePath, ext) + "." + format
}
// 确保目录存在
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return fmt.Errorf("无法创建目录: %w", err)
}
// 获取默认配置
defaultConfig := config.DefaultConfig()
// 检查文件是否存在并提示覆盖
if _, err := os.Stat(filePath); err == nil {
overwrite, _ := cmd.Flags().GetBool("overwrite")
if !overwrite {
return fmt.Errorf("文件 %s 已存在,使用 --overwrite 强制覆盖", filePath)
}
}
// 创建文件
f, err := os.Create(filePath)
if err != nil {
@@ -76,14 +96,19 @@ var exportCmd = &cobra.Command{
}
}
absPath, _ := filepath.Abs(filePath)
absPath, err := filepath.Abs(filePath)
if err != nil {
return fmt.Errorf("获取绝对路径失败: %w", err)
}
fmt.Printf("默认配置已导出到: %s\n", absPath)
return nil
},
}
func init() {
exportCmd.Flags().StringP("file", "f", "", "导出文件路径 (默认 ./config.yaml)")
exportCmd.Flags().StringP("file", "f", "", "导出文件路径 (默认导出到用户 Home 目录下 netctl/config.yaml)")
exportCmd.Flags().StringP("format", "t", "yaml", "导出格式: yaml, toml, json (默认 yaml)")
exportCmd.Flags().BoolP("overwrite", "o", false, "强制覆盖已存在的文件")
rootCmd.AddCommand(exportCmd)
}