Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f176df4bc4 | |||
| e94151cec5 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
bin
|
||||
config.*
|
||||
.idea
|
||||
nul
|
||||
|
||||
4
Makefile
4
Makefile
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user