Skip to content

Commit

Permalink
Merge pull request cloudfoundry#227 from XenoPhex/master
Browse files Browse the repository at this point in the history
By Grabthar's hammer, by the sons of Worvan, you shall be avenged. Also, sorting.
  • Loading branch information
Dan Lavine committed Aug 1, 2014
2 parents 7686c0d + f49a809 commit 3c6ae41
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
30 changes: 21 additions & 9 deletions cf/commands/application/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package application

import (
. "github.com/cloudfoundry/cli/cf/i18n"
"strings"
"sort"

"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
Expand Down Expand Up @@ -65,22 +65,34 @@ func (cmd *Env) Run(c *cli.Context) {
cmd.ui.Ok()
cmd.ui.Say("")

if len(vcapServices) > 0 {
cmd.ui.Say(T("System-Provided:"))
for _, line := range strings.Split(vcapServices, "\n") {
cmd.ui.Say(line)
}
} else {
cmd.displaySystemProvidedEnvironment(vcapServices)
cmd.ui.Say("")
cmd.displayUserProvidedEnvironment(envVars)
}

func (cmd *Env) displaySystemProvidedEnvironment(vcapServices string) {
if len(vcapServices) == 0 {
cmd.ui.Say(T("No system-provided env variables have been set"))
return
}
cmd.ui.Say(T("System-Provided:"))
cmd.ui.Say(vcapServices)
}

func (cmd *Env) displayUserProvidedEnvironment(envVars map[string]string) {
if len(envVars) == 0 {
cmd.ui.Say(T("No user-defined env variables have been set"))
return
}

keys := make([]string, 0, len(envVars))
for key, _ := range envVars {
keys = append(keys, key)
}
sort.Strings(keys)

cmd.ui.Say(T("User-Provided:"))
for key, value := range envVars {
cmd.ui.Say("%s: %s", key, terminal.EntityNameColor(value))
for _, key := range keys {
cmd.ui.Say("%s: %s", key, terminal.EntityNameColor(envVars[key]))
}
}
11 changes: 7 additions & 4 deletions cf/commands/application/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ var _ = Describe("env command", func() {

appRepo.ReadReturns.App = app
appRepo.ReadEnvReturns.UserEnv = map[string]string{
"my-key": "my-value",
"my-key2": "my-value2",
"my-key": "my-value",
"my-key2": "my-value2",
"first-key": "Zer0",
}
appRepo.ReadEnvReturns.VcapServices = `{
"VCAP_SERVICES": {
Expand All @@ -85,7 +86,7 @@ var _ = Describe("env command", func() {
}`
})

It("lists those environment variables like it's supposed to", func() {
It("lists those environment variables, in sorted order for provided services", func() {
runCommand("my-app")
Expect(appRepo.ReadEnvArgs.AppGuid).To(Equal("the-app-guid"))
Expect(ui.Outputs).To(ContainSubstrings(
Expand All @@ -96,7 +97,9 @@ var _ = Describe("env command", func() {
[]string{"pump-yer-brakes", ":", "drive-slow"},
[]string{"}"},
[]string{"User-Provided:"},
[]string{"my-key", "my-value", "my-key2", "my-value2"},
[]string{"first-key", "Zer0"},
[]string{"my-key", "my-value"},
[]string{"my-key2", "my-value2"},
))
})
})
Expand Down

0 comments on commit 3c6ae41

Please sign in to comment.