forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
97 lines (72 loc) · 1.87 KB
/
environment.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
package cmd
import (
cmdconf "github.com/cloudfoundry/bosh-cli/cmd/config"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type EnvironmentCmd struct {
sessionFactory func(cmdconf.Config) Session
config cmdconf.Config
ui boshui.UI
}
func NewEnvironmentCmd(
sessionFactory func(cmdconf.Config) Session,
config cmdconf.Config,
ui boshui.UI,
) EnvironmentCmd {
return EnvironmentCmd{sessionFactory: sessionFactory, config: config, ui: ui}
}
func (c EnvironmentCmd) Run(opts EnvironmentOpts) error {
args := opts.Args
if len(args.URL) == 0 {
return c.show()
}
updatedConfig := c.config.SetEnvironment(args.URL, args.Alias, opts.CACert)
err := c.set(updatedConfig)
if err != nil {
// If CA cert is specified, fail immediately
if len(opts.CACert) > 0 {
return err
}
// Otherwise try existing CA cert if user is just switching between environments
existingCACert := c.config.CACert(c.config.ResolveEnvironment(args.URL))
updatedConfig = c.config.SetEnvironment(args.URL, args.Alias, existingCACert)
altErr := c.set(updatedConfig)
if altErr != nil {
// Return original error without CA cert
return err
}
}
return nil
}
func (c EnvironmentCmd) show() error {
sess := c.sessionFactory(c.config)
c.ui.PrintLinef("Current environment is '%s'", sess.Environment())
director, err := sess.Director()
if err != nil {
return err
}
info, err := director.Info()
if err != nil {
return err
}
InfoTable{info, c.ui}.Print()
return nil
}
func (c EnvironmentCmd) set(updatedConfig cmdconf.Config) error {
sess := c.sessionFactory(updatedConfig)
director, err := sess.Director()
if err != nil {
return err
}
info, err := director.Info()
if err != nil {
return err
}
err = updatedConfig.Save()
if err != nil {
return err
}
c.ui.PrintLinef("Environment set to '%s'", sess.Environment())
InfoTable{info, c.ui}.Print()
return nil
}