diff --git a/cf/commands/application/env.go b/cf/commands/application/env.go index 322bd91a767..fa96dcb09f7 100644 --- a/cf/commands/application/env.go +++ b/cf/commands/application/env.go @@ -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" @@ -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])) } } diff --git a/cf/commands/application/env_test.go b/cf/commands/application/env_test.go index e5743f62ca0..c3c83c5bfed 100644 --- a/cf/commands/application/env_test.go +++ b/cf/commands/application/env_test.go @@ -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": { @@ -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( @@ -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"}, )) }) })