Skip to content

Commit 18dd868

Browse files
committed
fix(client-go,pkg): sort config and pretty-printed keys
1 parent 54bb728 commit 18dd868

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

client-go/cmd/config.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"regexp"
9+
"sort"
910
"strings"
1011

1112
"github.com/deis/deis/pkg/prettyprint"
@@ -28,9 +29,15 @@ func ConfigList(appID string, oneLine bool) error {
2829
return err
2930
}
3031

32+
var keys []string
33+
for k := range config.Values {
34+
keys = append(keys, k)
35+
}
36+
sort.Strings(keys)
37+
3138
if oneLine {
32-
for key, value := range config.Values {
33-
fmt.Printf("%s=%s ", key, value)
39+
for _, key := range keys {
40+
fmt.Printf("%s=%s ", key, config.Values[key])
3441
}
3542
fmt.Println()
3643
} else {
@@ -39,11 +46,11 @@ func ConfigList(appID string, oneLine bool) error {
3946
configMap := make(map[string]string)
4047

4148
// config.Values is type interface, so it needs to be converted to a string
42-
for key, value := range config.Values {
43-
configMap[key] = value.(string)
49+
for _, key := range keys {
50+
configMap[key] = config.Values[key].(string)
4451
}
4552

46-
fmt.Print(prettyprint.PrettyTabs(configMap, 5))
53+
fmt.Print(prettyprint.PrettyTabs(configMap, 6))
4754
}
4855

4956
return nil

pkg/prettyprint/colorizer.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package prettyprint
44
import (
55
"bytes"
66
"fmt"
7+
"sort"
78
"strings"
89
"text/template"
910
)
@@ -174,24 +175,28 @@ func Overwritef(msg string, args ...interface{}) string {
174175
//
175176
// This will return a formatted string.
176177
// The previous example would return:
177-
// test testing
178178
// foo bar
179+
// test testing
179180
func PrettyTabs(msg map[string]string, spaces int) string {
181+
// find the longest key so we know how much padding to use
180182
max := 0
181-
182183
for key := range msg {
183184
if len(key) > max {
184185
max = len(key)
185186
}
186187
}
187-
188188
max += spaces
189189

190-
var output string
191-
192-
for key, value := range msg {
193-
output += fmt.Sprintf("%s%s%s\n", key, strings.Repeat(" ", max-len(key)), value)
190+
// sort the map keys so we can print them alphabetically
191+
var keys []string
192+
for k := range msg {
193+
keys = append(keys, k)
194194
}
195+
sort.Strings(keys)
195196

197+
var output string
198+
for _, k := range keys {
199+
output += fmt.Sprintf("%s%s%s\n", k, strings.Repeat(" ", max-len(k)), msg[k])
200+
}
196201
return output
197202
}

pkg/prettyprint/colorizer_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func ExampleColorize() {
3737
out := Colorize("{{.Red}}Hello {{.Default}}World{{.UnderGreen}}!{{.Default}}")
3838
fmt.Println(out)
3939
}
40+
4041
func ExampleColorizeVars() {
4142
vars := map[string]string{"Who": "World"}
4243
tpl := "{{.C.Red}}Hello {{.C.Default}}{{.V.Who}}!"
@@ -67,16 +68,13 @@ func TestPrettyTabs(t *testing.T) {
6768
"foo": "bar",
6869
}
6970

70-
expected := `test testing
71-
foo bar
72-
`
73-
otherExpected := `foo bar
71+
expected := `foo bar
7472
test testing
7573
`
7674

7775
output := PrettyTabs(test, 1)
7876

79-
if !(output == expected || output == otherExpected) {
77+
if output != expected {
8078
t.Errorf("Expected '%s', Got '%s'", expected, output)
8179
}
8280
}

0 commit comments

Comments
 (0)