forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_and_validate_settings_and_manifest.go
127 lines (105 loc) · 3.23 KB
/
merge_and_validate_settings_and_manifest.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
package pushaction
import (
"fmt"
"os"
"code.cloudfoundry.org/cli/actor/pushaction/manifest"
log "github.com/sirupsen/logrus"
)
type MissingNameError struct{}
func (MissingNameError) Error() string {
return "name not specified for app"
}
type NonexistentAppPathError struct {
Path string
}
func (e NonexistentAppPathError) Error() string {
return fmt.Sprint("app path not found:", e.Path)
}
type CommandLineOptionsWithMultipleAppsError struct{}
func (CommandLineOptionsWithMultipleAppsError) Error() string {
return "cannot use command line flag with multiple apps"
}
type AppNotFoundInManifestError struct {
Name string
}
func (e AppNotFoundInManifestError) Error() string {
return fmt.Sprintf("specfied app: %s not found in manifest", e.Name)
}
func (actor Actor) MergeAndValidateSettingsAndManifests(settings CommandLineSettings, apps []manifest.Application) ([]manifest.Application, error) {
var mergedApps []manifest.Application
if len(apps) == 0 {
log.Info("no manifest, generating one from command line settings")
mergedApps = append(mergedApps, settings.OverrideManifestSettings(manifest.Application{}))
} else {
if settings.Name != "" && len(apps) > 1 {
var err error
apps, err = actor.selectApp(settings.Name, apps)
if err != nil {
return nil, err
}
}
err := actor.validatePremergedSettings(settings, apps)
if err != nil {
return nil, err
}
for _, app := range apps {
mergedApps = append(mergedApps, settings.OverrideManifestSettings(app))
}
}
mergedApps = actor.setSaneDefaults(mergedApps)
log.Debugf("merged app settings: %#v", mergedApps)
return mergedApps, actor.validateMergedSettings(mergedApps)
}
func (Actor) selectApp(appName string, apps []manifest.Application) ([]manifest.Application, error) {
var returnedApps []manifest.Application
for _, app := range apps {
if app.Name == appName {
returnedApps = append(returnedApps, app)
}
}
if len(returnedApps) == 0 {
return nil, AppNotFoundInManifestError{Name: appName}
}
return returnedApps, nil
}
func (Actor) setSaneDefaults(apps []manifest.Application) []manifest.Application {
for i, app := range apps {
if app.HealthCheckType == "http" && app.HealthCheckHTTPEndpoint == "" {
apps[i].HealthCheckHTTPEndpoint = "/"
}
}
return apps
}
func (Actor) validatePremergedSettings(settings CommandLineSettings, apps []manifest.Application) error {
if len(apps) > 1 {
switch {
case settings.BuildpackName != "",
settings.Command != "",
settings.DiskQuota != 0,
settings.DockerImage != "",
settings.HealthCheckTimeout != 0,
settings.HealthCheckType != "",
settings.Instances != 0,
settings.Memory != 0,
settings.ProvidedAppPath != "",
settings.StackName != "":
log.Error("cannot use some parameters with multiple apps")
return CommandLineOptionsWithMultipleAppsError{}
}
}
return nil
}
func (Actor) validateMergedSettings(apps []manifest.Application) error {
for i, app := range apps {
if app.Name == "" {
log.WithField("index", i).Error("does not contain an app name")
return MissingNameError{}
}
_, err := os.Stat(app.Path)
if os.IsNotExist(err) {
log.WithField("path", app.Path).Error("app path does not exist")
return NonexistentAppPathError{Path: app.Path}
}
}
return nil
}