Files
sunhpc-go/internal/cli/init/cfg.go

47 lines
1.1 KiB
Go
Raw Normal View History

2026-02-14 05:36:00 +08:00
package initcmd
import (
2026-02-20 18:44:43 +08:00
"sunhpc/internal/middler/auth"
"sunhpc/pkg/logger"
2026-02-14 05:36:00 +08:00
"github.com/spf13/cobra"
2026-02-20 18:44:43 +08:00
"go.uber.org/zap"
2026-02-14 05:36:00 +08:00
)
2026-02-15 07:18:14 +08:00
// NewConfigCmd 创建 "init config" 命令
2026-02-20 18:44:43 +08:00
func NewInitCfgCmd() *cobra.Command {
2026-02-15 07:18:14 +08:00
var (
force bool
path string
verbose bool
)
cmd := &cobra.Command{
Use: "config",
Short: "生成默认配置文件",
Long: `
在指定路径生成 SunHPC 默认配置文件 (sunhpc.yaml)
示例:
sunhpc init config # 生成默认配置文件
sunhpc init config -f # 强制覆盖已有配置文件
sunhpc init config -p /etc/sunhpc/sunhpc.yaml # 指定路径
`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := auth.RequireRoot(); err != nil {
return err
}
2026-02-14 05:36:00 +08:00
2026-02-20 18:44:43 +08:00
logger.Info("✅ 配置文件已生成", zap.String("path", path))
2026-02-15 07:18:14 +08:00
return nil
},
}
2026-02-14 05:36:00 +08:00
2026-02-15 07:18:14 +08:00
// 定义局部 flags
cmd.Flags().BoolVarP(&force, "force", "f", false, "强制覆盖已有配置文件")
cmd.Flags().StringVarP(&path, "path", "p", "", "指定配置文件路径")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "显示详细日志")
2026-02-14 05:36:00 +08:00
2026-02-15 07:18:14 +08:00
return cmd
2026-02-14 05:36:00 +08:00
}