/
switch.go
64 lines (50 loc) · 1.18 KB
/
switch.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
package cmd
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type switchCmdState struct {
Clear bool
}
func newSwitchCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "switch CLUSTER_NAME",
Short: "Switches the current cluster",
SilenceUsage: true,
}
state := &switchCmdState{}
cmd.PersistentFlags().BoolVarP(&state.Clear, "clear", "c", false, "Clears the current cluster")
cmd.RunE = state.runE
return cmd
}
func (s *switchCmdState) runE(cmd *cobra.Command, args []string) error {
clusterRepository, err := newClusterRepository()
if err != nil {
return err
}
if s.Clear {
return clusterRepository.SetCurrent("")
}
if len(args) != 1 {
return errors.New("invalid command arguments")
}
clusterName := args[0]
{
current, err := clusterRepository.Current()
if err != nil {
return err
}
if current != nil && current.Name == clusterName {
// is already the current cluster
return nil
}
}
cluster, err := clusterRepository.Load(clusterName)
if err != nil {
return err
}
if cluster == nil {
return errors.Errorf("could not find cluster '%s'", clusterName)
}
return clusterRepository.SetCurrent(cluster.Name)
}