-
Notifications
You must be signed in to change notification settings - Fork 153
/
cmd.go
64 lines (53 loc) · 1.43 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
package terraform
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/weaveworks/tf-controller/tfctl"
"github.com/weaveworks/weave-gitops/cmd/gitops/config"
"github.com/weaveworks/weave-gitops/pkg/run"
"k8s.io/cli-runtime/pkg/genericclioptions"
"os"
)
var kubeConfigArgs *genericclioptions.ConfigFlags
func Command(opts *config.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "terraform",
Aliases: []string{"tf"},
Args: cobra.ExactArgs(1),
Short: "Trigger replan for a Terraform object",
Example: `
# Replan the Terraform plan of a Terraform object from the "flux-system" namespace
gitops replan terraform --namespace flux-system my-resource
`,
RunE: func(cmd *cobra.Command, args []string) error {
app := tfctl.New("", "")
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
context, err := cmd.Flags().GetString("context")
if err != nil {
return err
}
kubeConfigArgs.Namespace = &namespace
kubeConfigArgs.Context = &context
cfg, err := kubeConfigArgs.ToRESTConfig()
if err != nil {
return err
}
v := viper.New()
v.Set("namespace", namespace)
if err := app.Init(cfg, v); err != nil {
return err
}
return app.Replan(
os.Stdout,
args[0],
)
},
}
kubeConfigArgs = run.GetKubeConfigArgs()
kubeConfigArgs.AddFlags(cmd.Flags())
kubeConfigArgs.KubeConfig = &opts.Kubeconfig
return cmd
}