forked from MusicDin/kubitect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_export_preset.go
73 lines (56 loc) · 1.56 KB
/
cmd_export_preset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"os"
"github.com/MusicDin/kubitect/embed"
"github.com/spf13/cobra"
)
var (
exportPresetShort = "Export cluster configuration preset"
exportPresetLong = LongDesc(`
Command export preset outputs cluster configuration preset with a given name to
the standard output.`)
exportPresetExample = Example(`
To list available presets run:
> kubitect list presets
To export a preset to the specific file run:
> kubitect export preset --name minimal > cluster.yaml`)
)
type ExportPresetOptions struct {
PresetName string
}
func NewExportPresetCmd() *cobra.Command {
var o ExportPresetOptions
cmd := &cobra.Command{
Use: "preset",
GroupID: "main",
Short: exportPresetShort,
Long: exportPresetLong,
Example: exportPresetExample,
RunE: func(cmd *cobra.Command, args []string) error {
return o.Run()
},
}
cmd.PersistentFlags().StringVar(&o.PresetName, "name", "", "preset name")
cmd.MarkPersistentFlagRequired("name")
cmd.RegisterFlagCompletionFunc("name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var names []string
presets, err := embed.Presets()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
for _, p := range presets {
names = append(names, presetName(p.Name))
}
return names, cobra.ShellCompDirectiveNoFileComp
})
return cmd
}
func (o *ExportPresetOptions) Run() error {
p, err := embed.GetPreset(o.PresetName + ".yaml")
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, string(p.Content))
return nil
}