-
Notifications
You must be signed in to change notification settings - Fork 929
/
apply_manifest_command.go
116 lines (92 loc) · 3.42 KB
/
apply_manifest_command.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
package v7
import (
"os"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/cli/util/manifestparser"
"code.cloudfoundry.org/clock"
"github.com/cloudfoundry/bosh-cli/director/template"
)
//go:generate counterfeiter . ApplyManifestActor
type ApplyManifestActor interface {
SetSpaceManifest(spaceGUID string, rawManifest []byte) (v7action.Warnings, error)
}
type ApplyManifestCommand struct {
PathToManifest flag.ManifestPathWithExistenceCheck `short:"f" description:"Path to app manifest"`
Vars []template.VarKV `long:"var" description:"Variable key value pair for variable substitution, (e.g., name=app1); can specify multiple times"`
PathsToVarsFiles []flag.PathWithExistenceCheck `long:"vars-file" description:"Path to a variable substitution file for manifest; can specify multiple times"`
usage interface{} `usage:"CF_NAME apply-manifest -f APP_MANIFEST_PATH"`
relatedCommands interface{} `related_commands:"create-app, create-app-manifest, push"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor ApplyManifestActor
ManifestLocator ManifestLocator
ManifestParser ManifestParser
CWD string
}
func (cmd *ApplyManifestCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, clock.NewClock())
cmd.ManifestLocator = manifestparser.NewLocator()
cmd.ManifestParser = manifestparser.ManifestParser{}
currentDir, err := os.Getwd()
cmd.CWD = currentDir
return err
}
func (cmd ApplyManifestCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
readPath := cmd.CWD
if cmd.PathToManifest != "" {
readPath = string(cmd.PathToManifest)
}
pathToManifest, exists, err := cmd.ManifestLocator.Path(readPath)
if err != nil {
return err
}
if !exists {
return translatableerror.ManifestFileNotFoundInDirectoryError{PathToManifest: readPath}
}
cmd.UI.DisplayTextWithFlavor("Applying manifest {{.ManifestPath}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"ManifestPath": pathToManifest,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": user.Name,
})
var pathsToVarsFiles []string
for _, varFilePath := range cmd.PathsToVarsFiles {
pathsToVarsFiles = append(pathsToVarsFiles, string(varFilePath))
}
manifest, err := cmd.ManifestParser.InterpolateAndParse(pathToManifest, pathsToVarsFiles, cmd.Vars)
if err != nil {
return err
}
rawManifest, err := cmd.ManifestParser.MarshalManifest(manifest)
if err != nil {
return err
}
warnings, err := cmd.Actor.SetSpaceManifest(cmd.Config.TargetedSpace().GUID, rawManifest)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
return nil
}