/
switch.go
128 lines (110 loc) · 3.23 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package main
import (
"errors"
"fmt"
"os"
"sort"
"launchpad.net/gnuflag"
"github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/envcmd"
"github.com/juju/juju/environs"
)
type SwitchCommand struct {
cmd.CommandBase
EnvName string
List bool
}
var switchDoc = `
Show or change the default juju environment name.
If no command line parameters are passed, switch will output the current
environment as defined by the file $JUJU_HOME/current-environment.
If a command line parameter is passed in, that value will is stored in the
current environment file if it represents a valid environment name as
specified in the environments.yaml file.
`
func (c *SwitchCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "switch",
Args: "[environment name]",
Purpose: "show or change the default juju environment name",
Doc: switchDoc,
Aliases: []string{"env"},
}
}
func (c *SwitchCommand) SetFlags(f *gnuflag.FlagSet) {
f.BoolVar(&c.List, "l", false, "list the environment names")
f.BoolVar(&c.List, "list", false, "")
}
func (c *SwitchCommand) Init(args []string) (err error) {
c.EnvName, err = cmd.ZeroOrOneArgs(args)
return
}
func validEnvironmentName(name string, names []string) bool {
for _, n := range names {
if name == n {
return true
}
}
return false
}
func (c *SwitchCommand) Run(ctx *cmd.Context) error {
// Switch is an alternative way of dealing with environments than using
// the JUJU_ENV environment setting, and as such, doesn't play too well.
// If JUJU_ENV is set we should report that as the current environment,
// and not allow switching when it is set.
// Passing through the empty string reads the default environments.yaml file.
environments, err := environs.ReadEnvirons("")
if err != nil {
return errors.New("couldn't read the environment")
}
names := environments.Names()
sort.Strings(names)
if c.List {
// List all environments.
if c.EnvName != "" {
return errors.New("cannot switch and list at the same time")
}
for _, name := range names {
fmt.Fprintf(ctx.Stdout, "%s\n", name)
}
return nil
}
jujuEnv := os.Getenv("JUJU_ENV")
if jujuEnv != "" {
if c.EnvName == "" {
fmt.Fprintf(ctx.Stdout, "%s\n", jujuEnv)
return nil
} else {
return fmt.Errorf("cannot switch when JUJU_ENV is overriding the environment (set to %q)", jujuEnv)
}
}
currentEnv := envcmd.ReadCurrentEnvironment()
if currentEnv == "" {
currentEnv = environments.Default
}
// Handle the different operation modes.
switch {
case c.EnvName == "" && currentEnv == "":
// Nothing specified and nothing to switch to.
return errors.New("no currently specified environment")
case c.EnvName == "":
// Simply print the current environment.
fmt.Fprintf(ctx.Stdout, "%s\n", currentEnv)
default:
// Switch the environment.
if !validEnvironmentName(c.EnvName, names) {
return fmt.Errorf("%q is not a name of an existing defined environment", c.EnvName)
}
if err := envcmd.WriteCurrentEnvironment(c.EnvName); err != nil {
return err
}
if currentEnv == "" {
fmt.Fprintf(ctx.Stdout, "-> %s\n", c.EnvName)
} else {
fmt.Fprintf(ctx.Stdout, "%s -> %s\n", currentEnv, c.EnvName)
}
}
return nil
}