/
root-instance-switch.go
50 lines (45 loc) · 1.48 KB
/
root-instance-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
package cli
import (
color "github.com/mitchellh/colorstring"
"github.com/spf13/cobra"
)
func instanceSwitch(givenName string) {
conf.Set(joinKey(stateWord, selectorWord), givenName)
if err := writeConfig(false); err == nil {
currentInstance = givenName
zboth.Info().Msgf("Instance being managed switched to %s.", color.Color(toSprintf("[green]%s", currentInstance)))
} else {
zboth.Fatal().Err(err).Msgf("Failed to update the selected instance.")
}
}
var switchInstanceRootCmd = &cobra.Command{
Use: "switch",
Short: "Switch to an instance of " + nameProject,
Run: func(cmd *cobra.Command, _ []string) {
if len(allInstances()) == 1 {
zboth.Fatal().Err(toError("only one instance")).Msgf("You cannot switch because you only have one instance.")
}
if ownCall(cmd) {
if cmd.Flag("name").Changed {
givenName := cmd.Flag("name").Value.String()
if err := instanceValidate(givenName); err != nil {
zboth.Fatal().Err(err).Msgf(err.Error())
}
instanceSwitch(givenName)
} else {
isInteractive(true)
instanceSwitch(selectInstance("switch to"))
}
} else {
if isInteractive(false) {
instanceSwitch(selectInstance("switch to"))
} else {
zboth.Fatal().Err(toError("unexpected operation")).Msgf("Please repeat your actions with the `--debug` flag and report this error.")
}
}
},
}
func init() {
instanceRootCmd.AddCommand(switchInstanceRootCmd)
switchInstanceRootCmd.Flags().StringP("name", "n", "", "Name of instance to switch to.")
}