-
Notifications
You must be signed in to change notification settings - Fork 23
/
root.go
366 lines (302 loc) · 12 KB
/
root.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package cmd
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/credentials"
"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/types"
"github.com/replicatedhq/replicated/pkg/version"
"github.com/replicatedhq/replicated/client"
"github.com/replicatedhq/replicated/pkg/platformclient"
"github.com/spf13/cobra"
)
// table output settings
const (
minWidth = 0
tabWidth = 8
padding = 4
padChar = ' '
)
var appSlugOrID string
var apiToken string
var enterprisePrivateKeyPath = filepath.Join(homeDir(), ".replicated", "enterprise", "ecdsa")
var platformOrigin = "https://api.replicated.com/vendor"
var kurlDotSHOrigin = "https://kurl.sh"
var enterpriseOrigin = "https://api.replicated.com/enterprise"
func init() {
originFromEnv := os.Getenv("REPLICATED_API_ORIGIN")
if originFromEnv != "" {
platformOrigin = originFromEnv
}
enterpriseOriginFromEnv := os.Getenv("REPLICATED_ENTERPRISE_ORIGIN")
if enterpriseOriginFromEnv != "" {
enterpriseOrigin = enterpriseOriginFromEnv
}
}
// RootCmd represents the base command when called without any subcommands
func GetRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "replicated",
Short: "Manage channels, releases and collectors",
Long: `The replicated CLI allows vendors to manage their apps, channels, releases and collectors.`,
}
rootCmd.PersistentFlags().StringVar(&appSlugOrID, "app", "", "The app slug or app id to use in all calls")
rootCmd.PersistentFlags().StringVar(&apiToken, "token", "", "The API token to use to access your app in the Vendor API")
return rootCmd
}
// Almost the same as the default but don't print help subcommand
var rootCmdUsageTmpl = `
Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Example:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Available Commands:{{range .Commands}}{{if .IsAvailableCommand}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
w := tabwriter.NewWriter(stdout, minWidth, tabWidth, padding, padChar, tabwriter.TabIndent)
// get api client and app ID after flags are parsed
runCmds := &runners{
rootCmd: rootCmd,
stdin: stdin,
w: w,
}
if runCmds.rootCmd == nil {
runCmds.rootCmd = GetRootCmd()
}
if stderr != nil {
runCmds.rootCmd.SetErr(stderr)
}
if stdout != nil {
runCmds.rootCmd.SetOut(stdout)
}
channelCmd := &cobra.Command{
Use: "channel",
Short: "List channels",
Long: "List channels",
}
var releaseCmd = &cobra.Command{
Use: "release",
Short: "Manage app releases",
Long: `The release command allows vendors to create, display, and promote their releases.`,
}
runCmds.InitCompletionCommand(runCmds.rootCmd)
runCmds.rootCmd.AddCommand(channelCmd)
runCmds.InitChannelCreate(channelCmd)
runCmds.InitChannelInspect(channelCmd)
runCmds.InitChannelAdoption(channelCmd)
runCmds.InitChannelReleases(channelCmd)
runCmds.InitChannelCounts(channelCmd)
runCmds.InitChannelList(channelCmd)
runCmds.InitChannelRemove(channelCmd)
runCmds.InitChannelEnableSemanticVersioning(channelCmd)
runCmds.InitChannelDisableSemanticVersioning(channelCmd)
runCmds.rootCmd.AddCommand(releaseCmd)
err := runCmds.InitReleaseCreate(releaseCmd)
if err != nil {
return errors.Wrap(err, "initialize release create command")
}
runCmds.InitReleaseInspect(releaseCmd)
runCmds.InitReleaseDownload(releaseCmd)
runCmds.IniReleaseList(releaseCmd)
runCmds.InitReleaseUpdate(releaseCmd)
runCmds.InitReleasePromote(releaseCmd)
runCmds.InitReleaseLint(releaseCmd)
runCmds.InitReleaseTest(releaseCmd)
runCmds.InitReleaseCompatibility(releaseCmd)
collectorsCmd := runCmds.InitCollectorsCommand(runCmds.rootCmd)
runCmds.InitCollectorList(collectorsCmd)
runCmds.InitCollectorUpdate(collectorsCmd)
runCmds.InitCollectorPromote(collectorsCmd)
runCmds.InitCollectorCreate(collectorsCmd)
runCmds.InitCollectorInspect(collectorsCmd)
customersCmd := runCmds.InitCustomersCommand(runCmds.rootCmd)
runCmds.InitCustomersLSCommand(customersCmd)
runCmds.InitCustomersCreateCommand(customersCmd)
runCmds.InitCustomersDownloadLicenseCommand(customersCmd)
runCmds.InitCustomersArchiveCommand(customersCmd)
runCmds.InitCustomersInspectCommand(customersCmd)
runCmds.InitCustomerUpdateCommand(customersCmd)
instanceCmd := runCmds.InitInstanceCommand(runCmds.rootCmd)
runCmds.InitInstanceLSCommand(instanceCmd)
runCmds.InitInstanceInspectCommand(instanceCmd)
runCmds.InitInstanceTagCommand(instanceCmd)
installerCmd := runCmds.InitInstallerCommand(runCmds.rootCmd)
runCmds.InitInstallerCreate(installerCmd)
runCmds.InitInstallerList(installerCmd)
enterpriseCmd := runCmds.InitEnterpriseCommand(runCmds.rootCmd)
enterpriseAuthCmd := runCmds.InitEnterpriseAuth(enterpriseCmd)
runCmds.InitEnterpriseAuthInit(enterpriseAuthCmd)
runCmds.InitEnterpriseAuthApprove(enterpriseAuthCmd)
enterpriseChannelCmd := runCmds.InitEnterpriseChannel(enterpriseCmd)
runCmds.InitEnterpriseChannelLS(enterpriseChannelCmd)
runCmds.InitEnterpriseChannelCreate(enterpriseChannelCmd)
runCmds.InitEnterpriseChannelUpdate(enterpriseChannelCmd)
runCmds.InitEnterpriseChannelRM(enterpriseChannelCmd)
runCmds.InitEnterpriseChannelAssign(enterpriseChannelCmd)
enterprisePolicyCmd := runCmds.InitEnterprisePolicy(enterpriseCmd)
runCmds.InitEnterprisePolicyLS(enterprisePolicyCmd)
runCmds.InitEnterprisePolicyCreate(enterprisePolicyCmd)
runCmds.InitEnterprisePolicyUpdate(enterprisePolicyCmd)
runCmds.InitEnterprisePolicyRM(enterprisePolicyCmd)
runCmds.InitEnterprisePolicyAssign(enterprisePolicyCmd)
runCmds.InitEnterprisePolicyUnassign(enterprisePolicyCmd)
enterpriseInstallerCmd := runCmds.InitEnterpriseInstaller(enterpriseCmd)
runCmds.InitEnterpriseInstallerLS(enterpriseInstallerCmd)
runCmds.InitEnterpriseInstallerCreate(enterpriseInstallerCmd)
runCmds.InitEnterpriseInstallerUpdate(enterpriseInstallerCmd)
runCmds.InitEnterpriseInstallerRM(enterpriseInstallerCmd)
runCmds.InitEnterpriseInstallerAssign(enterpriseInstallerCmd)
appCmd := runCmds.InitAppCommand(runCmds.rootCmd)
runCmds.InitAppList(appCmd)
runCmds.InitAppCreate(appCmd)
runCmds.InitAppDelete(appCmd)
registryCmd := runCmds.InitRegistryCommand(runCmds.rootCmd)
runCmds.InitRegistryList(registryCmd)
runCmds.InitRegistryRemove(registryCmd)
runCmds.InitRegistryTest(registryCmd)
runCmds.InitRegistryLogs(registryCmd)
registryAddCmd := runCmds.InitRegistryAdd(registryCmd)
runCmds.InitRegistryAddDockerHub(registryAddCmd)
runCmds.InitRegistryAddECR(registryAddCmd)
runCmds.InitRegistryAddGAR(registryAddCmd)
runCmds.InitRegistryAddGCR(registryAddCmd)
runCmds.InitRegistryAddGHCR(registryAddCmd)
runCmds.InitRegistryAddQuay(registryAddCmd)
runCmds.InitRegistryAddOther(registryAddCmd)
clusterCmd := runCmds.InitClusterCommand(runCmds.rootCmd)
runCmds.InitClusterCreate(clusterCmd)
runCmds.InitClusterUpgrade(clusterCmd)
runCmds.InitClusterList(clusterCmd)
runCmds.InitClusterKubeconfig(clusterCmd)
runCmds.InitClusterRemove(clusterCmd)
runCmds.InitClusterVersions(clusterCmd)
runCmds.InitClusterShell(clusterCmd)
clusterNodeGroupCmd := runCmds.InitClusterNodeGroup(clusterCmd)
runCmds.InitClusterNodeGroupList(clusterNodeGroupCmd)
clusterAddonCmd := runCmds.InitClusterAddon(clusterCmd)
runCmds.InitClusterAddonLs(clusterAddonCmd)
runCmds.InitClusterAddonRm(clusterAddonCmd)
clusterAddonCreateCmd := runCmds.InitClusterAddonCreate(clusterAddonCmd)
runCmds.InitClusterAddonCreateObjectStore(clusterAddonCreateCmd)
runCmds.InitClusterAddonCreatePostgres(clusterAddonCreateCmd)
clusterPortCmd := runCmds.InitClusterPort(clusterCmd)
runCmds.InitClusterPortLs(clusterPortCmd)
runCmds.InitClusterPortExpose(clusterPortCmd)
runCmds.InitClusterPortRm(clusterPortCmd)
clusterPrepareCmd := runCmds.InitClusterPrepare(clusterCmd)
clusterUpdateCmd := runCmds.InitClusterUpdateCommand(clusterCmd)
runCmds.InitClusterUpdateTTL(clusterUpdateCmd)
runCmds.InitLoginCommand(runCmds.rootCmd)
runCmds.InitLogoutCommand(runCmds.rootCmd)
apiCmd := runCmds.InitAPICommand(runCmds.rootCmd)
runCmds.InitAPIGet(apiCmd)
runCmds.InitAPIPost(apiCmd)
runCmds.InitAPIPut(apiCmd)
runCmds.rootCmd.SetUsageTemplate(rootCmdUsageTmpl)
preRunSetupAPIs := func(_ *cobra.Command, _ []string) error {
if apiToken == "" {
creds, err := credentials.GetCurrentCredentials()
if err != nil {
if err == credentials.ErrCredentialsNotFound {
return errors.New("Please provide your API token or log in with `replicated login`")
}
return errors.Wrap(err, "get current credentials")
}
apiToken = creds.APIToken
}
// allow override
if os.Getenv("KURL_SH_ORIGIN") != "" {
kurlDotSHOrigin = os.Getenv("KURL_SH_ORIGIN")
}
platformAPI := platformclient.NewHTTPClient(platformOrigin, apiToken)
runCmds.platformAPI = platformAPI
httpClient := platformclient.NewHTTPClient(platformOrigin, apiToken)
kotsAPI := &kotsclient.VendorV3Client{HTTPClient: *httpClient}
runCmds.kotsAPI = kotsAPI
commonAPI := client.NewClient(platformOrigin, apiToken, kurlDotSHOrigin)
runCmds.api = commonAPI
version.PrintIfUpgradeAvailable()
return nil
}
prerunCommand := func(cmd *cobra.Command, args []string) (err error) {
if cmd.SilenceErrors { // when SilenceErrors is set, command wants to use custom error printer
defer func() {
printIfError(cmd, err)
}()
}
if err = preRunSetupAPIs(cmd, args); err != nil {
return errors.Wrap(err, "set up APIs")
}
if appSlugOrID == "" {
appSlugOrID = os.Getenv("REPLICATED_APP")
}
app, appType, err := runCmds.api.GetAppType(appSlugOrID)
if err != nil {
return errors.Wrap(err, "get app type")
}
runCmds.appType = appType
runCmds.appID = app.ID
runCmds.appSlug = app.Slug
return nil
}
channelCmd.PersistentPreRunE = prerunCommand
releaseCmd.PersistentPreRunE = prerunCommand
collectorsCmd.PersistentPreRunE = prerunCommand
installerCmd.PersistentPreRunE = prerunCommand
customersCmd.PersistentPreRunE = prerunCommand
instanceCmd.PersistentPreRunE = prerunCommand
clusterPrepareCmd.PersistentPreRunE = prerunCommand
appCmd.PersistentPreRunE = preRunSetupAPIs
registryCmd.PersistentPreRunE = preRunSetupAPIs
clusterCmd.PersistentPreRunE = preRunSetupAPIs
apiCmd.PersistentPreRunE = preRunSetupAPIs
runCmds.rootCmd.AddCommand(Version())
return runCmds.rootCmd.Execute()
}
func printIfError(cmd *cobra.Command, err error) {
if err == nil {
return
}
cmd.SilenceUsage = true
switch err := errors.Cause(err).(type) {
case platformclient.APIError:
fmt.Fprintln(os.Stderr, fmt.Sprintf("ERROR: %d", err.StatusCode))
fmt.Fprintln(os.Stderr, fmt.Sprintf("METHOD: %s", err.Method))
fmt.Fprintln(os.Stderr, fmt.Sprintf("ENDPOINT: %s", err.Endpoint))
fmt.Fprintln(os.Stderr, err.Message) // note that this can have multiple lines
default:
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err.Error())
}
}
func parseTags(tags []string) ([]types.Tag, error) {
parsedTags := []types.Tag{}
for _, tag := range tags {
tagParts := strings.SplitN(tag, "=", 2)
if len(tagParts) != 2 {
return nil, errors.Errorf("invalid tag format: %s", tag)
}
parsedTags = append(parsedTags, types.Tag{
Key: tagParts[0],
Value: tagParts[1],
})
}
return parsedTags, nil
}