Ensure lexical ordering for config files

This commit is contained in:
Armon Dadgar 2013-12-27 14:49:35 -08:00
parent 2cc64f3291
commit f193ed5a88
1 changed files with 19 additions and 0 deletions

View File

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