ok-1
This commit is contained in:
49
cmd/init/system.go
Normal file
49
cmd/init/system.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package initcmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sunhpc/internal/auth"
|
||||
"sunhpc/internal/config"
|
||||
"sunhpc/internal/system"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
dryRun bool // --dry-run -n: 仅模拟执行,不实际应用
|
||||
verbose bool // --verbose -v: 启用详细日志输出
|
||||
)
|
||||
|
||||
var systemCmd = &cobra.Command{
|
||||
Use: "system [flags]",
|
||||
Short: "根据配置文件初始化系统",
|
||||
Long: `读取 /etc/sunhpc/sunhpc.yaml 中的系统配置项并应用到当前节点。
|
||||
示例:
|
||||
sunhpc init system # 应用所有配置项
|
||||
sunhpc init system --dry-run # 仅模拟执行,不实际应用
|
||||
sunhpc init system --verbose # 启用详细日志输出
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// 权限检查:必须以 root 或 sudo 运行
|
||||
if err := auth.RequireRoot(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 加载主配置
|
||||
cfg, err := config.LoadSunHPC()
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载 sunhpc.yaml 失败: %v", err)
|
||||
}
|
||||
|
||||
// 统一应用所有配置
|
||||
return system.ApplyAll(cfg)
|
||||
},
|
||||
}
|
||||
|
||||
// init 初始化 systemCmd 的标志,添加长参数和段参数.
|
||||
func init() {
|
||||
// 注册长参数, 布尔参数
|
||||
systemCmd.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "仅模拟执行,不实际应用")
|
||||
systemCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "启用详细日志输出")
|
||||
}
|
||||
Reference in New Issue
Block a user