-
Notifications
You must be signed in to change notification settings - Fork 151
/
cmd.go
124 lines (100 loc) · 2.94 KB
/
cmd.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package templates
import (
"errors"
"fmt"
"os"
"strings"
"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
"github.com/weaveworks/weave-gitops/pkg/adapters"
"github.com/weaveworks/weave-gitops/pkg/capi"
"k8s.io/cli-runtime/pkg/printers"
)
type templateCommandFlags struct {
ListTemplateParameters bool
ListTemplateProfiles bool
Provider string
}
var flags templateCommandFlags
var providers = []string{
"aws",
"azure",
"digitalocean",
"docker",
"openstack",
"packet",
"vsphere",
}
func TemplateCommand(endpoint *string, client *resty.Client) *cobra.Command {
cmd := &cobra.Command{
Use: "template",
Aliases: []string{"templates"},
Short: "Display one or many CAPI templates",
Example: `
# Get all CAPI templates
gitops get templates
# Get all AWS CAPI templates
gitops get templates --provider aws
# Show the parameters of a CAPI template
gitops get template <template-name> --list-parameters
`,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: getTemplateCmdPreRunE(endpoint, client),
RunE: getTemplateCmdRunE(endpoint, client),
Args: cobra.MaximumNArgs(1),
}
cmd.Flags().BoolVar(&flags.ListTemplateParameters, "list-parameters", false, "Show parameters of CAPI template")
cmd.Flags().BoolVar(&flags.ListTemplateProfiles, "list-profiles", false, "Show profiles of CAPI template")
cmd.Flags().StringVar(&flags.Provider, "provider", "", fmt.Sprintf("Filter templates by provider. Supported providers: %s", strings.Join(providers, " ")))
return cmd
}
func getTemplateCmdPreRunE(endpoint *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(c *cobra.Command, args []string) error {
if c.Flag("provider").Changed && !contains(providers, c.Flag("provider").Value.String()) {
return fmt.Errorf("provider %q is not valid", c.Flag("provider").Value.String())
}
if *endpoint == "" {
return cmderrors.ErrNoWGEEndpoint
}
return nil
}
}
func getTemplateCmdRunE(endpoint *string, client *resty.Client) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
r, err := adapters.NewHttpClient(*endpoint, client, os.Stdout)
if err != nil {
return err
}
w := printers.GetNewTabWriter(os.Stdout)
defer w.Flush()
if flags.ListTemplateParameters {
if len(args) == 0 {
return errors.New("template name is required")
}
return capi.GetTemplateParameters(args[0], r, w)
}
if flags.ListTemplateProfiles {
if len(args) == 0 {
return errors.New("template name is required")
}
return capi.GetTemplateProfiles(args[0], r, w)
}
if len(args) == 0 {
if flags.Provider != "" {
return capi.GetTemplatesByProvider(flags.Provider, r, w)
}
return capi.GetTemplates(r, w)
}
return nil
}
}
func contains(ss []string, str string) bool {
for _, s := range ss {
if s == str {
return true
}
}
return false
}