重构架构
This commit is contained in:
46
internal/cli/init/cfg.go
Normal file
46
internal/cli/init/cfg.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package initcmd
|
||||
|
||||
import (
|
||||
"sunhpc/internal/middler/auth"
|
||||
"sunhpc/pkg/logger"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// NewConfigCmd 创建 "init config" 命令
|
||||
func NewInitCfgCmd() *cobra.Command {
|
||||
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
|
||||
}
|
||||
|
||||
logger.Info("✅ 配置文件已生成", zap.String("path", path))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// 定义局部 flags
|
||||
cmd.Flags().BoolVarP(&force, "force", "f", false, "强制覆盖已有配置文件")
|
||||
cmd.Flags().StringVarP(&path, "path", "p", "", "指定配置文件路径")
|
||||
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "显示详细日志")
|
||||
|
||||
return cmd
|
||||
}
|
||||
53
internal/cli/init/db.go
Normal file
53
internal/cli/init/db.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package initcmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sunhpc/internal/middler/auth"
|
||||
"sunhpc/pkg/config"
|
||||
"sunhpc/pkg/database"
|
||||
"sunhpc/pkg/logger"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewInitDBCmd() *cobra.Command {
|
||||
var force bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "db",
|
||||
Short: "初始化数据库",
|
||||
Long: `初始化SQLite数据库,创建所有表结构和默认数据。
|
||||
|
||||
示例:
|
||||
sunhpc init db # 初始化数据库
|
||||
sunhpc init db --force # 强制重新初始化`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := auth.RequireRoot(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Debug("执行数据库初始化...")
|
||||
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载配置失败: %w", err)
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
db, err := database.GetInstance(&cfg.Database, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("数据库连接失败: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if err := db.InitTables(force); err != nil {
|
||||
return fmt.Errorf("数据库初始化失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVarP(&force, "force", "f", false, "强制重新初始化")
|
||||
return cmd
|
||||
}
|
||||
19
internal/cli/init/init.go
Normal file
19
internal/cli/init/init.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package initcmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// 仅定义 Cmd, 注册子命令,只负责组装命令树,尽量不包含业务逻辑
|
||||
func NewInitCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "初始化集群配置",
|
||||
Long: "初始化 SunHPC 配置文件、数据库、系统参数及相关服务",
|
||||
}
|
||||
|
||||
cmd.AddCommand(NewInitDBCmd())
|
||||
cmd.AddCommand(NewInitCfgCmd())
|
||||
|
||||
return cmd
|
||||
}
|
||||
Reference in New Issue
Block a user