59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package tmpl
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"sunhpc/internal/log"
|
||
"sunhpc/internal/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
|
||
}
|