-
Notifications
You must be signed in to change notification settings - Fork 129
/
config.go
108 lines (92 loc) · 3.06 KB
/
config.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
package cmd
import (
"fmt"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"github.com/armadaproject/armada/internal/armadactl"
armadaslices "github.com/armadaproject/armada/internal/common/slices"
"github.com/armadaproject/armada/pkg/client"
)
func configCmd(a *armadactl.App) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Operations on armadactl config. Supported: use-context, get-contexts, current-context",
}
cmd.AddCommand(getContextsCmd(a))
cmd.AddCommand(useContextCmd(a))
cmd.AddCommand(currentContextCmd(a))
return cmd
}
func useContextCmd(a *armadactl.App) *cobra.Command {
cmd := &cobra.Command{
Use: "use-context <context>",
Short: "Sets the default context for future armadactl calls",
Long: `This command allows you to set the default context value for future armadactl calls.
It stores this state within the specified, or otherwise default ($HOME/.armadactl) configuration file.`,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return initParams(cmd, a.Params)
},
RunE: func(cmd *cobra.Command, args []string) error {
contextToUse := args[0]
if !slices.Contains(client.ExtractConfigurationContexts(), contextToUse) {
return fmt.Errorf("unable to use context %s. Context not defined in config.", contextToUse)
}
cfg, err := cmd.Flags().GetString("config")
if err != nil {
return err
} else if cfg != "" {
return client.ModifyCurrentContextInConfig(cfg, contextToUse)
}
homeDir, err := homedir.Dir()
if err != nil {
return fmt.Errorf("error getting home directory: %s", err)
}
err = client.ModifyCurrentContextInConfig(fmt.Sprintf("%s/.armadactl.yaml", homeDir), contextToUse)
if err == nil {
fmt.Printf("Current context set to %s\n", contextToUse)
}
return err
},
}
return cmd
}
func getContextsCmd(a *armadactl.App) *cobra.Command {
cmd := &cobra.Command{
Use: "get-contexts",
Short: "Retrieves all contexts from armadactl config",
PreRunE: func(cmd *cobra.Command, args []string) error {
return initParams(cmd, a.Params)
},
RunE: func(cmd *cobra.Command, args []string) error {
currentContext := viper.GetString("currentContext")
allContexts := armadaslices.Map(client.ExtractConfigurationContexts(), func(context string) string {
if context == currentContext {
return fmt.Sprintf("%s (current)", context)
}
return context
})
slices.Sort(allContexts)
fmt.Printf("Available contexts: %s\n", strings.Join(allContexts, ", "))
return nil
},
}
return cmd
}
func currentContextCmd(a *armadactl.App) *cobra.Command {
cmd := &cobra.Command{
Use: "current-context",
Short: "Returns the currently set Armada context, determined from armadactl config",
PreRunE: func(cmd *cobra.Command, args []string) error {
return initParams(cmd, a.Params)
},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Current context: %s\n", viper.GetString("currentContext"))
return nil
},
}
return cmd
}