Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

By Grabthar's hammer, by the sons of Worvan, you shall be avenged. Also, sorting. #227

Merged
1 commit merged into from
Aug 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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