-
Notifications
You must be signed in to change notification settings - Fork 7
/
antifreeze.go
212 lines (164 loc) · 4.62 KB
/
antifreeze.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"code.cloudfoundry.org/cli/plugin"
"gopkg.in/yaml.v2"
)
func main() {
plugin.Start(&AntifreezePlugin{})
}
type AntifreezePlugin struct{}
func (c *AntifreezePlugin) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] != "check-manifest" {
os.Exit(0)
}
fmt.Println("Running check-manifest...")
appName, manifestPath, err := ParseArgs(args)
fatalIf(err)
manifestEnv, manifestServices, err := ParseManifest(manifestPath, appName)
fatalIf(err)
appEnv, appServices, err := GetAppEnvAndServices(cliConnection, appName)
fatalIf(err)
missingEnv := MissingFromManifest(manifestEnv, appEnv)
missingServices := MissingFromManifest(manifestServices, appServices)
if len(missingEnv) == 0 && len(missingServices) == 0 {
os.Exit(0)
}
printMissingValues(appName, manifestPath, missingEnv, missingServices)
os.Exit(1)
}
func (c *AntifreezePlugin) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "antifreeze",
Version: plugin.VersionType{
Major: 0,
Minor: 4,
Build: 0,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 30,
Build: 0,
},
Commands: []plugin.Command{
plugin.Command{
Name: "check-manifest",
HelpText: "Check your manifest isn't missing any ENV vars or services currently in an app",
},
},
}
}
func ParseArgs(args []string) (string, string, error) {
flags := flag.NewFlagSet("check-manifest", flag.ContinueOnError)
manifestPath := flags.String("f", "", "path to an application manifest")
err := flags.Parse(args[2:])
if err != nil {
return "", "", err
}
if *manifestPath == "" {
return "", "", fmt.Errorf("Missing manifest argument")
}
appName := args[1]
return appName, *manifestPath, nil
}
func GetAppEnvAndServices(cliConnection plugin.CliConnection, appName string) (appEnv []string, appServices []string, err error) {
app, _ := cliConnection.GetApp(appName)
for k := range app.EnvironmentVars {
appEnv = append(appEnv, k)
}
for _, s := range app.Services {
appServices = append(appServices, s.Name)
}
return appEnv, appServices, err
}
type YManifest struct {
Applications []YApplication `yaml:"applications"`
}
type YApplication struct {
Name string `yaml:"name"`
Env map[string]interface{} `yaml:"env"`
Services []string `yaml:"services"`
}
func ParseManifest(manifestPath, appName string) (manifestEnv []string, manifestServices []string, err error) {
document, err := loadYAML(manifestPath)
if err != nil {
return manifestEnv, manifestServices, err
}
app, err := findApp(appName, document.Applications)
if err != nil {
return manifestEnv, manifestServices, err
}
for k := range app.Env {
manifestEnv = append(manifestEnv, k)
}
return manifestEnv, app.Services, nil
}
func MissingFromManifest(manifestList, appList []string) (missing []string) {
for _, appValue := range appList {
if !stringInSlice(appValue, manifestList) {
missing = append(missing, appValue)
}
}
return missing
}
func loadYAML(manifestPath string) (manifest YManifest, err error) {
b, err := ioutil.ReadFile(manifestPath)
if err != nil {
return YManifest{}, fmt.Errorf("Unable to read manifest file: %s", manifestPath)
}
var document YManifest
err = yaml.Unmarshal(b, &document)
if err != nil {
return YManifest{}, fmt.Errorf("Unable to parse manifest YAML")
}
return document, nil
}
func findApp(appName string, apps []YApplication) (app YApplication, err error) {
if len(apps) == 0 {
return YApplication{}, fmt.Errorf("No application found in manifest")
}
appIndex := notFoundIndex
for i := range apps {
if apps[i].Name == appName {
appIndex = i
break
}
}
if appIndex == notFoundIndex {
return YApplication{}, fmt.Errorf("Application '%s' not found in manifest", appName)
}
return apps[appIndex], nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if a == b {
return true
}
}
return false
}
func fatalIf(err error) {
if err != nil {
fmt.Fprintln(os.Stdout, "error:", err)
os.Exit(1)
}
}
func printMissingValues(appName string, manifestPath string, missingEnv []string, missingServices []string) {
if len(missingEnv) > 0 {
fmt.Printf("\nApp '%s' has unexpected ENV vars (missing from manifest %s):\n", appName, manifestPath)
printListAsBullets(missingEnv)
}
if len(missingServices) > 0 {
fmt.Printf("\nApp '%s' has unexpected services (missing from manifest %s):\n", appName, manifestPath)
printListAsBullets(missingServices)
}
}
func printListAsBullets(list []string) {
for _, v := range list {
fmt.Printf("- %s\n", v)
}
}
const notFoundIndex = -1