Files
sunhpc-go/internal/cli/tmpl/dump.go
2026-02-20 18:44:43 +08:00

59 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}