-
Notifications
You must be signed in to change notification settings - Fork 153
/
cmd.go
80 lines (63 loc) · 2.12 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
package terraform
import (
"errors"
"os"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/printers"
"github.com/weaveworks/weave-gitops/cmd/gitops/cmderrors"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/adapters"
"github.com/weaveworks/weave-gitops/pkg/templates"
)
type templateCommandFlags struct {
ListTemplateParameters bool
}
var flags templateCommandFlags
func TerraformCommand(opts *config.Options, client *adapters.HTTPClient) *cobra.Command {
cmd := &cobra.Command{
Use: "terraform",
Aliases: []string{"terraform"},
Short: "Display one or many Terraform templates",
Example: `
# Get all terraform templates
gitops get template terraform
# Show the parameters of a Terraform template
gitops get template terraform <template-name> --list-parameters
`,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: getTerraformTemplateCmdPreRunE(&opts.Endpoint),
RunE: getTerraformTemplateCmdRunE(opts, client),
Args: cobra.MaximumNArgs(1),
}
cmd.Flags().BoolVar(&flags.ListTemplateParameters, "list-parameters", false, "Show parameters of Terraform template")
return cmd
}
func getTerraformTemplateCmdPreRunE(endpoint *string) func(*cobra.Command, []string) error {
return func(c *cobra.Command, args []string) error {
if *endpoint == "" {
return cmderrors.ErrNoWGEEndpoint
}
return nil
}
}
func getTerraformTemplateCmdRunE(opts *config.Options, client *adapters.HTTPClient) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
err := client.ConfigureClientWithOptions(opts, 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("terraform template name is required")
}
return templates.GetTemplateParameters(templates.GitOpsTemplateKind, args[0], client, w)
}
if len(args) == 0 {
return templates.GetTemplates(templates.GitOpsTemplateKind, client, w)
}
return templates.GetTemplate(args[0], templates.GitOpsTemplateKind, client, w)
}
}