Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f176df4bc4 | |||
| e94151cec5 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
bin
|
bin
|
||||||
config.*
|
config.*
|
||||||
.idea
|
.idea
|
||||||
|
nul
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -4,7 +4,7 @@ OUTPUT := ./bin/$(APP_NAME).exe
|
|||||||
GO := go
|
GO := go
|
||||||
|
|
||||||
# Version info
|
# 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)
|
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo none)
|
||||||
BUILD_TIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
|
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
|
# Detect Zig
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
ZIG_BIN := $(shell where zig 2>nul)
|
ZIG_BIN := $(shell where zig 2>/dev/null)
|
||||||
else
|
else
|
||||||
ZIG_BIN := $(shell which zig 2>/dev/null)
|
ZIG_BIN := $(shell which zig 2>/dev/null)
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -23,9 +23,15 @@ var exportCmd = &cobra.Command{
|
|||||||
filePath, _ := cmd.Flags().GetString("file")
|
filePath, _ := cmd.Flags().GetString("file")
|
||||||
format, _ := cmd.Flags().GetString("format")
|
format, _ := cmd.Flags().GetString("format")
|
||||||
|
|
||||||
// 默认文件名和格式
|
// 默认文件路径和格式
|
||||||
if filePath == "" {
|
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 == "" {
|
if format == "" {
|
||||||
format = "yaml"
|
format = "yaml"
|
||||||
@@ -42,9 +48,23 @@ var exportCmd = &cobra.Command{
|
|||||||
filePath = strings.TrimSuffix(filePath, ext) + "." + format
|
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()
|
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)
|
f, err := os.Create(filePath)
|
||||||
if err != nil {
|
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)
|
fmt.Printf("默认配置已导出到: %s\n", absPath)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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().StringP("format", "t", "yaml", "导出格式: yaml, toml, json (默认 yaml)")
|
||||||
|
exportCmd.Flags().BoolP("overwrite", "o", false, "强制覆盖已存在的文件")
|
||||||
|
|
||||||
rootCmd.AddCommand(exportCmd)
|
rootCmd.AddCommand(exportCmd)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user