重构架构
This commit is contained in:
58
internal/cli/tmpl/dump.go
Normal file
58
internal/cli/tmpl/dump.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package tmpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "sunhpc/pkg/logger"
|
||||
"sunhpc/pkg/templating"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newDumpCmd() *cobra.Command {
|
||||
var output string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "dump <template-name>",
|
||||
Short: "导出内置模板到文件",
|
||||
Long: `
|
||||
将内置的 YAML 模板导出为可编辑的文件。
|
||||
|
||||
示例:
|
||||
sunhpc tmpl dump autofs --output ./my-autofs.yaml`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name := args[0]
|
||||
|
||||
// 检查模板是否存在
|
||||
available, _ := templating.ListEmbeddedTemplates()
|
||||
found := false
|
||||
for _, n := range available {
|
||||
if n == name {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("内置模板 '%s' 不存在。可用模板: %v", name, available)
|
||||
}
|
||||
|
||||
outPath := output
|
||||
if outPath == "" {
|
||||
outPath = name + ".yaml"
|
||||
}
|
||||
|
||||
if err := templating.DumpEmbeddedTemplateToFile(name, outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("内置模板 '%s' 已导出到: %s", name, outPath)
|
||||
log.Infof("你可以编辑此文件,然后用以下命令使用它:")
|
||||
log.Infof(" sunhpc tmpl render %s -f %s [flags]", name, outPath)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&output, "output", "o", "", "输出文件路径(默认: <name>.yaml)")
|
||||
return cmd
|
||||
}
|
||||
Reference in New Issue
Block a user