-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathcmd.go
163 lines (137 loc) · 5.24 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package root
import (
"fmt"
"os"
"strings"
"github.com/weaveworks/weave-gitops/cmd/gitops/check"
"github.com/weaveworks/weave-gitops/cmd/gitops/update"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
wego "github.com/weaveworks/weave-gitops/api/v1alpha1"
"github.com/weaveworks/weave-gitops/cmd/gitops/add"
beta "github.com/weaveworks/weave-gitops/cmd/gitops/beta/cmd"
"github.com/weaveworks/weave-gitops/cmd/gitops/delete"
"github.com/weaveworks/weave-gitops/cmd/gitops/docs"
"github.com/weaveworks/weave-gitops/cmd/gitops/flux"
"github.com/weaveworks/weave-gitops/cmd/gitops/get"
"github.com/weaveworks/weave-gitops/cmd/gitops/install"
"github.com/weaveworks/weave-gitops/cmd/gitops/resume"
"github.com/weaveworks/weave-gitops/cmd/gitops/suspend"
"github.com/weaveworks/weave-gitops/cmd/gitops/ui"
"github.com/weaveworks/weave-gitops/cmd/gitops/uninstall"
"github.com/weaveworks/weave-gitops/cmd/gitops/upgrade"
"github.com/weaveworks/weave-gitops/cmd/gitops/version"
fluxBin "github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/kube"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/weaveworks/weave-gitops/pkg/utils"
"k8s.io/client-go/rest"
)
var options struct {
endpoint string
overrideInCluster bool
verbose bool
gitHostTypes map[string]string
}
// Only want AutomaticEnv to be called once!
func init() {
// Setup flag to env mapping:
// config-repo => GITOPS_CONFIG_REPO
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("GITOPS")
viper.AutomaticEnv()
}
func RootCmd(client *resty.Client) *cobra.Command {
cliRunner := &runner.CLIRunner{}
osysClient := osys.New()
fluxClient := fluxBin.New(osysClient, cliRunner)
fluxClient.SetupBin()
var rootCmd = &cobra.Command{
Use: "gitops",
SilenceUsage: true,
SilenceErrors: true,
Short: "Weave GitOps",
Long: "Command line utility for managing Kubernetes applications via GitOps.",
Example: fmt.Sprintf(`
# Get verbose output for any gitops command
gitops [command] -v, --verbose
# Get gitops app help
gitops help app
# Add application to gitops control from a local git repository
gitops add app . --name <myapp>
OR
gitops add app <myapp-directory>
# Add application to gitops control from a github repository
gitops add app \
--name <myapp> \
--url [email protected]:myorg/<myapp> \
--branch prod-<myapp>
# Get status of application under gitops control
gitops get app podinfo
# Get help for gitops add app command
gitops add app -h
gitops help add app
# Show manifests that would be installed by the gitops install command
gitops install --dry-run
# Install gitops in the %s namespace
gitops install
# Get the version of gitops along with commit, branch, and flux version
gitops version
To learn more, you can find our documentation at https://docs.gitops.weave.works/
`, wego.DefaultNamespace),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
configureLogger()
// Sync flag values and env vars.
err := viper.BindPFlags(cmd.Flags())
if err != nil {
log.Fatalf("Error binding viper to flags: %v", err)
}
ns, _ := cmd.Flags().GetString("namespace")
if ns == "" {
return
}
if nserr := utils.ValidateNamespace(ns); nserr != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", nserr)
os.Exit(1)
}
if options.overrideInCluster {
kube.InClusterConfig = func() (*rest.Config, error) { return nil, rest.ErrNotInCluster }
}
},
}
rootCmd.PersistentFlags().BoolVarP(&options.verbose, "verbose", "v", false, "Enable verbose output")
rootCmd.PersistentFlags().String("namespace", wego.DefaultNamespace, "The namespace scope for this operation")
rootCmd.PersistentFlags().StringVarP(&options.endpoint, "endpoint", "e", os.Getenv("WEAVE_GITOPS_ENTERPRISE_API_URL"), "The Weave GitOps Enterprise HTTP API endpoint")
rootCmd.PersistentFlags().BoolVar(&options.overrideInCluster, "override-in-cluster", false, "override running in cluster check")
rootCmd.PersistentFlags().StringToStringVar(&options.gitHostTypes, "git-host-types", map[string]string{}, "Specify which custom domains are running what (github or gitlab)")
cobra.CheckErr(rootCmd.PersistentFlags().MarkHidden("override-in-cluster"))
cobra.CheckErr(rootCmd.PersistentFlags().MarkHidden("git-host-types"))
rootCmd.AddCommand(install.Cmd)
rootCmd.AddCommand(beta.Cmd)
rootCmd.AddCommand(uninstall.Cmd)
rootCmd.AddCommand(version.Cmd)
rootCmd.AddCommand(flux.Cmd)
rootCmd.AddCommand(ui.NewCommand())
rootCmd.AddCommand(get.GetCommand(&options.endpoint, client))
rootCmd.AddCommand(add.GetCommand(&options.endpoint, client))
rootCmd.AddCommand(update.UpdateCommand(&options.endpoint, client))
rootCmd.AddCommand(delete.DeleteCommand(&options.endpoint, client))
rootCmd.AddCommand(resume.GetCommand())
rootCmd.AddCommand(suspend.GetCommand())
rootCmd.AddCommand(upgrade.Cmd)
rootCmd.AddCommand(docs.Cmd)
rootCmd.AddCommand(check.Cmd)
return rootCmd
}
func configureLogger() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
if options.verbose {
log.SetLevel(log.DebugLevel)
}
}