Add -c flag to specify list of config files
- Environment variable also accepts comma-separated list - Overwrites cli arguments
This commit is contained in:
parent
0241a506d4
commit
9166a78fb7
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -9,3 +9,8 @@ spaceapid
|
||||||
|
|
||||||
# Saved state
|
# Saved state
|
||||||
spaceapid-state.json
|
spaceapid-state.json
|
||||||
|
|
||||||
|
# Config shards
|
||||||
|
credentials.json
|
||||||
|
dynamic.json
|
||||||
|
response.json
|
||||||
|
|
|
@ -3,10 +3,12 @@ package config
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"slices"
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
||||||
)
|
)
|
||||||
|
@ -15,36 +17,73 @@ const (
|
||||||
envConfigPath = "CONFIG_PATH"
|
envConfigPath = "CONFIG_PATH"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getConfigPath gets the spaceapid configuration from the respective environment variables
|
type pathsT []string
|
||||||
func getConfigPath() string {
|
|
||||||
// JSON template path
|
func (p *pathsT) String() string {
|
||||||
|
return fmt.Sprint(*p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *pathsT) Set(s string) error {
|
||||||
|
*p = make(pathsT, 0)
|
||||||
|
for _, path := range strings.Split(s, ",") {
|
||||||
|
*p = append(*p, path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
flagConfigPaths pathsT
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.Var(&flagConfigPaths, "c", "Comma-separated list of config file paths. Parsed in order of appearance. Values get overwritten by files parsed later.")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigPaths() (paths pathsT) {
|
||||||
|
// Get paths from env variable and if env var present override cli flags
|
||||||
configPath, ok := os.LookupEnv(envConfigPath)
|
configPath, ok := os.LookupEnv(envConfigPath)
|
||||||
if !ok || configPath == "" {
|
if ok {
|
||||||
log.Fatalln("Could not retrieve", envConfigPath, "env variable or variable is empty")
|
if configPath == "" {
|
||||||
|
log.Fatalln("Env variable", envConfigPath, "is present but empty.")
|
||||||
|
}
|
||||||
|
_ = flagConfigPaths.Set(configPath)
|
||||||
}
|
}
|
||||||
// Save as absolute path
|
|
||||||
absConfigPath, err := filepath.Abs(configPath)
|
for _, path := range flagConfigPaths {
|
||||||
if err != nil {
|
_, err := os.Stat(path)
|
||||||
log.Fatalln("Failed converting", configPath, "to absolute path:", err)
|
if err != nil {
|
||||||
|
log.Fatalln("Config file", path, "doesn't exist or is not accessible, error:", err)
|
||||||
|
}
|
||||||
|
paths = append(paths, path)
|
||||||
}
|
}
|
||||||
return absConfigPath
|
|
||||||
|
// If paths is still empty we are missing something
|
||||||
|
if len(paths) == 0 {
|
||||||
|
log.Fatalln("Neither the env variable nor cli flag was specified, we are missing a config file.")
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseConfiguration returns the config from file
|
// ParseConfiguration returns the config from file
|
||||||
func ParseConfiguration() (conf SpaceapidConfig) {
|
func ParseConfiguration() (conf SpaceapidConfig) {
|
||||||
log.Println("Parsing configuration file")
|
log.Println("Parsing configuration files")
|
||||||
// Read file
|
|
||||||
file, err := os.ReadFile(getConfigPath())
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Failed reading file:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse JSON
|
paths := getConfigPaths()
|
||||||
dec := json.NewDecoder(bytes.NewReader(file))
|
log.Println(paths)
|
||||||
dec.DisallowUnknownFields()
|
for _, path := range paths {
|
||||||
err = dec.Decode(&conf)
|
// Read file
|
||||||
if err != nil {
|
file, err := os.ReadFile(path)
|
||||||
log.Fatalln("Could not parse spaceapid config file:", err)
|
if err != nil {
|
||||||
|
log.Fatalln("Failed reading file:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse JSON
|
||||||
|
dec := json.NewDecoder(bytes.NewReader(file))
|
||||||
|
dec.DisallowUnknownFields()
|
||||||
|
err = dec.Decode(&conf)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Could not parse spaceapid config file:", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if compatible with v14
|
// Check if compatible with v14
|
||||||
|
|
16
main.go
16
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -15,7 +16,22 @@ import (
|
||||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
flagHelp bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.BoolVar(&flagHelp, "h", false, "Print help output")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if flagHelp {
|
||||||
|
flag.PrintDefaults()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
// Get spaceapid configuration
|
// Get spaceapid configuration
|
||||||
conf := config.ParseConfiguration()
|
conf := config.ParseConfiguration()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue