/
root.go
276 lines (243 loc) · 7.98 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
package cmd
import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/fatih/color"
metalgo "github.com/metal-stack/metal-go"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/viper"
)
const (
cfgFileType = "yaml"
// name of the application, used for help, config location and env config variable names.
programName = "metalctl"
)
var (
ctx Context
printer Printer
detailer Detailer
driverURL string
driver *metalgo.Driver
defaultSSHKeys = [...]string{"id_ed25519", "id_rsa", "id_dsa"}
// will bind all viper flags to subcommands and
// prevent overwrite of identical flag names from other commands
// see https://github.com/spf13/viper/issues/233#issuecomment-386791444
bindPFlags = func(cmd *cobra.Command, args []string) {
err := viper.BindPFlags(cmd.Flags())
if err != nil {
log.Fatal(err.Error())
}
}
rootCmd = &cobra.Command{
Use: programName,
Aliases: []string{"m"},
Short: "a cli to manage metal devices.",
Long: "",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initPrinter()
},
SilenceUsage: true,
}
markdownCmd = &cobra.Command{
Use: "markdown",
Short: "create markdown documentation",
RunE: func(cmd *cobra.Command, args []string) error {
err := doc.GenMarkdownTree(rootCmd, "./docs")
if err != nil {
return err
}
return nil
},
}
)
// Execute is the entrypoint of the metal-go application
func Execute() {
err := rootCmd.Execute()
if err != nil {
if viper.GetBool("debug") {
st := errors.WithStack(err)
fmt.Printf("%+v", st)
}
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringP("config", "c", "", `alternative config file path, (default is ~/.metalctl/config.yaml).
Example config.yaml:
---
apitoken: "alongtoken"
...
`)
rootCmd.PersistentFlags().StringP("url", "u", "", "api server address. Can be specified with METALCTL_URL environment variable.")
rootCmd.PersistentFlags().String("apitoken", "", "api token to authenticate. Can be specified with METALCTL_APITOKEN environment variable.")
rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kube-config to use for authentication and authorization. Is updated by login.")
rootCmd.PersistentFlags().StringP("order", "", "", "order by (comma separated) column(s), possible values: size|id|status|event|when|partition|project")
rootCmd.PersistentFlags().StringP("output-format", "o", "table", "output format (table|wide|markdown|json|yaml|template), wide is a table with more columns.")
rootCmd.PersistentFlags().StringP("template", "", "", `output template for template output-format, go template format.
For property names inspect the output of -o json or -o yaml for reference.
Example for machines:
metalctl machine list -o template --template "{{ .id }}:{{ .size.id }}"
`)
rootCmd.PersistentFlags().Bool("no-headers", false, "do not print headers of table output format (default print headers)")
rootCmd.PersistentFlags().Bool(forceFlag, false, "skips security prompts (which can be dangerous to set blindly because actions can lead to data loss or additional costs)")
rootCmd.PersistentFlags().Bool("debug", false, "debug output")
rootCmd.PersistentFlags().Bool("force-color", false, "force colored output even without tty")
err := rootCmd.RegisterFlagCompletionFunc("output-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return outputFormatListCompletion()
})
if err != nil {
log.Fatal(err.Error())
}
err = rootCmd.RegisterFlagCompletionFunc("order", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return outputOrderListCompletion()
})
if err != nil {
log.Fatal(err.Error())
}
rootCmd.AddCommand(firmwareCmd)
rootCmd.AddCommand(machineCmd)
rootCmd.AddCommand(firewallCmd)
rootCmd.AddCommand(projectCmd)
rootCmd.AddCommand(sizeCmd)
rootCmd.AddCommand(filesystemLayoutCmd)
rootCmd.AddCommand(imageCmd)
rootCmd.AddCommand(partitionCmd)
rootCmd.AddCommand(switchCmd)
rootCmd.AddCommand(networkCmd)
rootCmd.AddCommand(markdownCmd)
rootCmd.AddCommand(healthCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(whoamiCmd)
rootCmd.AddCommand(contextCmd)
rootCmd.AddCommand(updateCmd)
err = viper.BindPFlags(rootCmd.PersistentFlags())
if err != nil {
log.Fatalf("error setup root cmd:%v", err)
}
}
func initConfig() {
viper.SetEnvPrefix(strings.ToUpper(programName))
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
viper.SetConfigType(cfgFileType)
cfgFile := viper.GetString("config")
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("config file path set explicitly, but unreadable:%v", err)
}
} else {
viper.SetConfigName("config")
viper.AddConfigPath(fmt.Sprintf("/etc/%s", programName))
h, err := os.UserHomeDir()
if err != nil {
log.Printf("unable to figure out user home directory, skipping config lookup path: %v", err)
} else {
viper.AddConfigPath(fmt.Sprintf(h+"/.%s", programName))
}
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
usedCfg := viper.ConfigFileUsed()
if usedCfg != "" {
log.Fatalf("config %s file unreadable:%v", usedCfg, err)
}
}
}
ctx = mustDefaultContext()
driverURL = viper.GetString("url")
if driverURL == "" && ctx.ApiURL != "" {
driverURL = ctx.ApiURL
}
hmacKey := viper.GetString("hmac")
if hmacKey == "" && ctx.HMAC != nil {
hmacKey = *ctx.HMAC
}
apiToken := viper.GetString("apitoken")
// if there is no api token explicitly specified we try to pull it out of the kubeconfig context
if apiToken == "" {
authContext, err := getAuthContext(viper.GetString("kubeConfig"))
// if there is an error, no kubeconfig exists for us ... this is not really an error
// since metalctl can be used in scripting with an hmac-key
if err == nil {
apiToken = authContext.IDToken
}
}
var err error
driver, err = metalgo.NewDriver(driverURL, apiToken, hmacKey)
if err != nil {
log.Fatal(err)
}
}
func initPrinter() {
if viper.IsSet("force-color") {
enabled := viper.GetBool("force-color")
if enabled {
color.NoColor = false
} else {
color.NoColor = true
}
}
var err error
printer, err = NewPrinter(
viper.GetString("output-format"),
viper.GetString("order"),
viper.GetString("template"),
viper.GetBool("no-headers"),
)
if err != nil {
log.Fatalf("unable to initialize printer:%v", err)
}
detailer, err = NewDetailer(viper.GetString("output-format"))
if err != nil {
log.Fatalf("unable to initialize detailer:%v", err)
}
}
func searchSSHKey() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", fmt.Errorf("unable to determine current user for expanding userdata path:%w", err)
}
homeDir := currentUser.HomeDir
defaultDir := filepath.Join(homeDir, "/.ssh/")
var key string
for _, k := range defaultSSHKeys {
possibleKey := filepath.Join(defaultDir, k)
_, err := os.ReadFile(possibleKey)
if err == nil {
fmt.Printf("using SSH identity: %s. Another identity can be specified with --sshidentity/-p\n",
possibleKey)
key = possibleKey
break
}
}
if key == "" {
return "", fmt.Errorf("failure to locate a SSH identity in default location (%s). "+
"Another identity can be specified with --sshidentity/-p\n", defaultDir)
}
return key, nil
}
func readFromFile(filePath string) (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", fmt.Errorf("unable to determine current user for expanding userdata path:%w", err)
}
homeDir := currentUser.HomeDir
if filePath == "~" {
filePath = homeDir
} else if strings.HasPrefix(filePath, "~/") {
filePath = filepath.Join(homeDir, filePath[2:])
}
content, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("unable to read from given file %s error:%w", filePath, err)
}
return strings.TrimSpace(string(content)), nil
}