Skip to content

Commit

Permalink
Handle mulit-app manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
odlp committed May 19, 2016
1 parent 2bd42e7 commit 730139b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
69 changes: 52 additions & 17 deletions antifreeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *AntifreezePlugin) Run(cliConnection plugin.CliConnection, args []string
appName, manifestPath, err := ParseArgs(args)
fatalIf(err)

manifestEnv, manifestServices, err := ParseManifest(manifestPath)
manifestEnv, manifestServices, err := ParseManifest(manifestPath, appName)
fatalIf(err)

appEnv, appServices, err := GetAppEnvAndServices(cliConnection, appName)
Expand All @@ -50,8 +50,8 @@ func (c *AntifreezePlugin) GetMetadata() plugin.PluginMetadata {
Name: "antifreeze",
Version: plugin.VersionType{
Major: 0,
Minor: 2,
Build: 1,
Minor: 3,
Build: 0,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Expand Down Expand Up @@ -103,34 +103,29 @@ type YManifest struct {
}

type YApplication struct {
Name string `yaml:"name"`
Env map[string]interface{} `yaml:"env"`
Services []string `yaml:"services"`
}

func ParseManifest(manifestPath string) ([]string, []string, error) {
b, err := ioutil.ReadFile(manifestPath)
func ParseManifest(manifestPath, appName string) (manifestEnv []string, manifestServices []string, err error) {
document, err := loadYAML(manifestPath)

if err != nil {
return []string{}, []string{}, fmt.Errorf("Unable to read manifest file: %s", manifestPath)
return manifestEnv, manifestServices, err
}

var document YManifest
err = yaml.Unmarshal(b, &document)
app, err := findApp(appName, document.Applications)

if err != nil {
return []string{}, []string{}, fmt.Errorf("Unable to parse manifest YAML")
return manifestEnv, manifestServices, err
}

if len(document.Applications) == 0 {
return []string{}, []string{}, fmt.Errorf("No application found in manifest")
for k := range app.Env {
manifestEnv = append(manifestEnv, k)
}

envKeys := []string{}
for k := range document.Applications[0].Env {
envKeys = append(envKeys, k)
}

return envKeys, document.Applications[0].Services, nil
return manifestEnv, app.Services, nil
}

func MissingFromManifest(manifestList, appList []string) (missing []string) {
Expand All @@ -142,6 +137,44 @@ func MissingFromManifest(manifestList, appList []string) (missing []string) {
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 {
Expand Down Expand Up @@ -175,3 +208,5 @@ func printListAsBullets(list []string) {
fmt.Printf("- %s\n", v)
}
}

const notFoundIndex = -1
30 changes: 26 additions & 4 deletions antifreeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,53 @@ var _ = Describe("Flag Parsing", func() {

var _ = Describe("Parsing Manifest", func() {
It("parses the ENV keys", func() {
envKeys, _, err := ParseManifest("./fixtures/manifest.yml")
envKeys, _, err := ParseManifest("./fixtures/manifest.yml", "app-name")
Expect(err).ToNot(HaveOccurred())
Expect(envKeys).To(HaveLen(2))
Expect(envKeys).To(ContainElement("ENV_VAR_1"))
Expect(envKeys).To(ContainElement("ENV_VAR_2"))
})

It("parses the service names", func() {
_, serviceNames, err := ParseManifest("./fixtures/manifest.yml")
_, serviceNames, err := ParseManifest("./fixtures/manifest.yml", "app-name")
Expect(err).ToNot(HaveOccurred())
Expect(serviceNames).To(HaveLen(2))
Expect(serviceNames).To(ContainElement("service-1"))
Expect(serviceNames).To(ContainElement("service-2"))
})

Context("multi-app manifest", func() {
It("returns the values for the correct app", func() {
envKeys, serviceNames, err := ParseManifest("./fixtures/multi-manifest.yml", "app-2")
Expect(err).ToNot(HaveOccurred())

Expect(serviceNames).To(HaveLen(2))
Expect(serviceNames).To(ContainElement("service-3"))
Expect(serviceNames).To(ContainElement("service-4"))

Expect(envKeys).To(HaveLen(2))
Expect(envKeys).To(ContainElement("ENV_VAR_3"))
Expect(envKeys).To(ContainElement("ENV_VAR_4"))
})
})

Context("app doesn't exist in the manifest", func() {
It("returns an error", func() {
_, _, err := ParseManifest("./fixtures/multi-manifest.yml", "app-666")
Expect(err).To(MatchError("Application 'app-666' not found in manifest"))
})
})

Context("invalid manifest path", func() {
It("returns an error", func() {
_, _, err := ParseManifest("./pure-fiction")
_, _, err := ParseManifest("./pure-fiction", "fictional-app")
Expect(err).To(MatchError("Unable to read manifest file: ./pure-fiction"))
})
})

Context("invalid manifest", func() {
It("returns an error", func() {
_, _, err := ParseManifest("./fixtures/invalid-manifest.json")
_, _, err := ParseManifest("./fixtures/invalid-manifest.json", "app-name")
Expect(err).To(MatchError("No application found in manifest"))
})
})
Expand Down
20 changes: 20 additions & 0 deletions fixtures/multi-manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
applications:
- name: app-1
memory: 256M
instances: 1
services:
- service-1
- service-2
env:
ENV_VAR_1: 1800
ENV_VAR_2: https://pivotal.io
- name: app-2
memory: 256M
instances: 1
services:
- service-3
- service-4
env:
ENV_VAR_3: 3600
ENV_VAR_4: https://github.com

0 comments on commit 730139b

Please sign in to comment.