Skip to content

Commit

Permalink
v0.4.0 - changed slice presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-szabo committed Aug 7, 2019
1 parent da1eb0e commit 87b9a0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
25 changes: 24 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"sort"
"strings"
)

Expand Down Expand Up @@ -40,7 +41,29 @@ func RunRoot(cmd *cobra.Command, args []string) (output string, err error) {
}
}

output = strings.Join(viper.GetStringSlice(key), " ")
switch viper.Get(key).(type) {
// If a section was requested, return the keys from that section
case map[string]interface{}:
var r []string
for k, _ := range viper.GetStringMapString(key) {
r = append(r,k)
}
sort.Strings(r)
output = strings.Join(r, " ")
// Return list of strings as result
default:
// Return all section names and root section keys if "." is provided
if key == "." {
var r []string
for k, _ := range viper.AllSettings() {
r = append(r,k)
}
sort.Strings(r)
output = strings.Join(r, " ")
} else {
output = strings.Join(viper.GetStringSlice(key), " ")
}
}
return
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ func TestRunRoot(t *testing.T) {
assert.Equal(t, "The answer is 42", result, "unexpected result")
assert.Nil(t, err, "unexpected error")

// reading keys of a section
result, err = RunRoot(cmd, []string{"../test.ini", "slices"})
assert.Equal(t, "numbers strings", result, "unexpected result")
assert.Nil(t, err, "unexpected error")

// reading section names (keys of the root section)
result, err = RunRoot(cmd, []string{"../test.ini", "."})
assert.Equal(t, "blur district9 master_of_the_universe slices", result, "unexpected result")
assert.Nil(t, err, "unexpected error")

}
2 changes: 1 addition & 1 deletion defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package defaults

// Application version
const Version = "0.3.0"
const Version = "0.4.0"

0 comments on commit 87b9a0d

Please sign in to comment.