mirror of
https://git.sr.ht/~sircmpwn/core-go
synced 2024-09-09 11:42:36 +02:00
config: allow spreading config over multiple files
This is the Go-equivalent to https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/47657 This commit enables spreading the config in /etc/sr.ht - and, crucially, _only_ in /etc/sr.ht - over multiple .ini files. If a file config.ini is found (either in current or parent directory, or /etc/sr.ht) it (and only it) is loaded and any other ini files are ignored. To utilize multiple configs, they must be in /etc/sr.ht, and none of them must be called config.ini. Spreading the config over multiple files will make it much easier to create containerized versions, where e.g. different secrets can be made available in different files, but rendering it all into one big file would require some preprocessing.
This commit is contained in:
parent
bd40a71a93
commit
3c1346e6bb
1 changed files with 18 additions and 2 deletions
|
@ -3,6 +3,7 @@ package config
|
|||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.sr.ht/~sircmpwn/getopt"
|
||||
"github.com/vaughan0/go-ini"
|
||||
|
@ -37,13 +38,28 @@ func LoadConfig(defaultAddr string) ini.File {
|
|||
}
|
||||
}
|
||||
|
||||
// Only loads from one of these locations
|
||||
for _, path := range []string{
|
||||
"config.ini",
|
||||
"../config.ini",
|
||||
"/etc/sr.ht/config.ini",
|
||||
"/etc/sr.ht/*.ini",
|
||||
} {
|
||||
config, err = ini.LoadFile(path)
|
||||
if err == nil {
|
||||
matches, err_ := filepath.Glob(path)
|
||||
if err_ != nil {
|
||||
panic(err) // only happens on bad input
|
||||
}
|
||||
for _, f := range matches {
|
||||
if config == nil {
|
||||
config, err = ini.LoadFile(f)
|
||||
} else {
|
||||
err = config.LoadFile(f)
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(matches) > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue