From b0d2931ab611d131ed0bbbce18f2c19726196bbc Mon Sep 17 00:00:00 2001 From: ale Date: Sat, 2 Feb 2019 06:45:15 +0000 Subject: [PATCH] Load configuration files in sorted order Allows us to use rc.d-style setup where later files override contents from previous ones, useful in a configuration management context. --- server/config.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server/config.go b/server/config.go index 87eb280..0229f3c 100644 --- a/server/config.go +++ b/server/config.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "log" "path/filepath" + "sort" "git.autistici.org/ai3/go-common/clientutil" "gopkg.in/yaml.v2" @@ -81,12 +82,21 @@ func loadStandaloneBackendConfig(path string) (map[string]yaml.MapSlice, error) return out, nil } +// Sort a string slice. +type filesList []string + +func (l filesList) Len() int { return len(l) } +func (l filesList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } +func (l filesList) Less(i, j int) bool { return l[i] < l[j] } + // Apply a function to all files matching a glob pattern (errors are ignored). +// Files are visited in sorted order. func forAllFiles(pattern string, f func(string)) { files, err := filepath.Glob(pattern) if err != nil { return } + sort.Sort(filesList(files)) for _, file := range files { f(file) } -- 2.22.0