-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_set.go
51 lines (42 loc) · 1.02 KB
/
context_set.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
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
yaml "sigs.k8s.io/yaml/goyaml.v2"
)
type contextSetCmdOptions struct {
current string
}
var contextSetOptions contextSetCmdOptions
var contextSetCmd = &cobra.Command{
Use: "set",
Short: "Set the current context.",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
isModified := false
if contextSetOptions.current != "" {
if config.CurrentContext != contextSetOptions.current {
config.CurrentContext = contextSetOptions.current
isModified = true
}
}
if !isModified {
return nil
}
f, err := os.Create(configFile)
if err != nil {
return fmt.Errorf("create config file: %w", err)
}
defer f.Close()
if err := yaml.NewEncoder(f).Encode(config); err != nil {
return fmt.Errorf("encode config: %w", err)
}
return nil
},
}
func init() {
contextCmd.AddCommand(contextSetCmd)
contextSetCmd.Flags().
StringVar(&contextSetOptions.current, "current", "", "name of the current context")
}