-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
75 lines (63 loc) · 1.76 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
package cmd
import (
"fmt"
"os"
"path"
"runtime"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const version = "1.0"
const (
envPrefix = "METAL"
configFileWithoutExtension = "metal"
defaultConfigDir = ".config/equinix"
MetalTokenEnvVar = envPrefix + "AUTH_TOKEN"
MetalConfigFile = "~/" + defaultConfigDir + "/" + configFileWithoutExtension + ".yaml"
)
var (
rootCmd = &cobra.Command{
Use: "equinix-metal-k8s",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set the auth token
v := viper.New()
v.AddConfigPath(defaultConfigPath())
v.SetConfigName(configFileWithoutExtension)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
panic(fmt.Errorf("could not read config: %s", err))
}
}
v.SetEnvPrefix(envPrefix)
v.AutomaticEnv()
token = v.GetString("token")
},
}
project, token string
verbose bool
)
func init() {
rootCmd.AddCommand(createCmd)
createInit()
rootCmd.PersistentFlags().StringVar(&project, "project", "", "project ID")
rootCmd.PersistentFlags().StringVar(&token, "token", "", fmt.Sprintf("Equinix Metal token, overrides env var %s, which overrides %s", MetalTokenEnvVar, MetalConfigFile))
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "print lots of output to stderr")
}
// Execute primary function for cobra
func Execute() {
rootCmd.Execute()
}
func defaultConfigPath() string {
return path.Join(userHomeDir(), defaultConfigDir)
}
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}