-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
63 lines (55 loc) · 1.49 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
package cmd
import (
"fmt"
"github.com/radiusmethod/kxd/src/utils"
"github.com/spf13/cobra"
"log"
"os"
)
var rootCmd = &cobra.Command{
Use: "kxd",
Short: "kxd - switch between Kubeconfigs and contexts.",
Long: "Allows for switching kubeconfig files and contexts, as well as getting the current set ones.",
Run: func(cmd *cobra.Command, args []string) {
err := runConfigSwitcher()
if err != nil {
log.Fatal(err)
}
},
}
func Execute() {
if shouldRunDirectConfigSwitch() {
config := os.Args[1]
if err := directConfigSwitch(config); err != nil {
log.Fatal(err)
}
return
}
runRootCmd()
}
func directConfigSwitch(desiredConfig string) error {
configs := utils.GetConfigs()
if utils.Contains(configs, desiredConfig) {
fmt.Printf(utils.PromptColor, "Config ")
fmt.Printf(utils.CyanColor, desiredConfig)
fmt.Printf(utils.PromptColor, " set.\n")
if desiredConfig == "default" {
desiredConfig = "config"
}
utils.WriteFile(desiredConfig, utils.GetHomeDir())
return nil
}
fmt.Printf(utils.NoticeColor, "WARNING: Config ")
fmt.Printf(utils.CyanColor, desiredConfig)
fmt.Printf(utils.NoticeColor, " does not exist or is invalid.\n")
return nil
}
func runRootCmd() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
func shouldRunDirectConfigSwitch() bool {
invalidConfigs := []string{"f", "file", "ctx", "context", "ns", "namespace", "completion", "help", "--help", "v", "version"}
return len(os.Args) > 1 && !utils.Contains(invalidConfigs, os.Args[1])
}