Ensure lexical ordering for config files

pull/19/head
Armon Dadgar 11 years ago
parent 2cc64f3291
commit f193ed5a88

@ -9,6 +9,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
@ -75,6 +76,8 @@ type Config struct {
ConsulConfig *consul.Config ConsulConfig *consul.Config
} }
type dirEnts []os.FileInfo
// DefaultConfig is used to return a sane default configuration // DefaultConfig is used to return a sane default configuration
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
@ -205,6 +208,9 @@ func ReadConfigPaths(paths []string) (*Config, error) {
return nil, fmt.Errorf("Error reading '%s': %s", path, err) return nil, fmt.Errorf("Error reading '%s': %s", path, err)
} }
// Sort the contents, ensures lexical order
sort.Sort(dirEnts(contents))
for _, fi := range contents { for _, fi := range contents {
// Don't recursively read contents // Don't recursively read contents
if fi.IsDir() { if fi.IsDir() {
@ -235,3 +241,16 @@ func ReadConfigPaths(paths []string) (*Config, error) {
return result, nil return result, nil
} }
// Implement the sort interface for dirEnts
func (d dirEnts) Len() int {
return len(d)
}
func (d dirEnts) Less(i, j int) bool {
return d[i].Name() < d[j].Name()
}
func (d dirEnts) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}

Loading…
Cancel
Save