forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_cloud_config.go
55 lines (43 loc) · 1.35 KB
/
update_cloud_config.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type UpdateCloudConfigCmd struct {
ui boshui.UI
director boshdir.Director
}
func NewUpdateCloudConfigCmd(ui boshui.UI, director boshdir.Director) UpdateCloudConfigCmd {
return UpdateCloudConfigCmd{ui: ui, director: director}
}
func (c UpdateCloudConfigCmd) Run(opts UpdateCloudConfigOpts) error {
tpl := boshtpl.NewTemplate(opts.Args.CloudConfig.Bytes)
bytes, err := tpl.Evaluate(opts.VarFlags.AsVariables(), opts.OpsFlags.AsOp(), boshtpl.EvaluateOpts{})
if err != nil {
return bosherr.WrapErrorf(err, "Evaluating cloud config")
}
cloudConfigDiff, err := c.director.DiffCloudConfig(bytes)
if err != nil {
return err
}
c.printManifestDiff(cloudConfigDiff)
err = c.ui.AskForConfirmation()
if err != nil {
return err
}
return c.director.UpdateCloudConfig(bytes)
}
func (c UpdateCloudConfigCmd) printManifestDiff(diff boshdir.CloudConfigDiff) {
for _, line := range diff.Diff {
lineMod, _ := line[1].(string)
if lineMod == "added" {
c.ui.BeginLinef("+ %s\n", line[0])
} else if lineMod == "removed" {
c.ui.BeginLinef("- %s\n", line[0])
} else {
c.ui.BeginLinef(" %s\n", line[0])
}
}
}