-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroot.go
More file actions
90 lines (72 loc) · 2.32 KB
/
root.go
File metadata and controls
90 lines (72 loc) · 2.32 KB
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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"fmt"
"log"
"os"
"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/spf13/cobra"
)
type GlobalOptions struct {
OmsPortalApiKey string
}
// GetRootCmd adds all child commands to the root command and sets flags appropriately.
func GetRootCmd() *cobra.Command {
opts := &GlobalOptions{}
rootCmd := &cobra.Command{
Use: "oms",
Short: "Codesphere Operations Management System (OMS)",
Long: io.Long(`Codesphere Operations Management System (OMS)
This command can be used to run common tasks related to managing codesphere installations,
like downloading new versions.`),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
apiKey := os.Getenv("OMS_PORTAL_API_KEY")
if len(apiKey) == 25 {
fmt.Fprintf(os.Stderr, "Warning: You used an old API key format.\n")
fmt.Fprintf(os.Stderr, "Attempting to upgrade to the new format...\n\n")
portalClient := portal.NewPortalClient()
keyId, err := portalClient.GetApiKeyId(apiKey)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to upgrade old API key: %v\n", err)
return
}
newApiKey := keyId + apiKey
if err := os.Setenv("OMS_PORTAL_API_KEY", newApiKey); err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to set environment variable: %v\n", err)
return
}
opts.OmsPortalApiKey = newApiKey
fmt.Fprintf(os.Stderr, "Please update your environment variable:\n\n")
fmt.Fprintf(os.Stderr, " export OMS_PORTAL_API_KEY='%s'\n\n", newApiKey)
}
},
}
// General commands
AddVersionCmd(rootCmd)
AddBetaCmd(rootCmd, opts)
AddUpdateCmd(rootCmd, opts)
// Package commands
AddListCmd(rootCmd, opts)
AddDownloadCmd(rootCmd, opts)
AddInstallCmd(rootCmd, opts)
AddInitCmd(rootCmd, opts)
AddBuildCmd(rootCmd, opts)
AddLicensesCmd(rootCmd)
// OMS API key management commands
AddRegisterCmd(rootCmd, opts)
AddRevokeCmd(rootCmd, opts)
// Smoke test commands
AddSmoketestCmd(rootCmd, opts)
return rootCmd
}
// Execute executes the root command. This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
//Disable printing timestamps on log lines
log.SetFlags(0)
err := GetRootCmd().Execute()
if err != nil {
os.Exit(1)
}
}