Add -c flag to specify list of config files

- Environment variable also accepts comma-separated list
  - Overwrites cli arguments
This commit is contained in:
Bendodroid 2024-01-14 04:20:03 +01:00
parent 0241a506d4
commit 9166a78fb7
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
3 changed files with 83 additions and 23 deletions

5
.gitignore vendored
View file

@ -9,3 +9,8 @@ spaceapid
# Saved state
spaceapid-state.json
# Config shards
credentials.json
dynamic.json
response.json

View file

@ -3,10 +3,12 @@ package config
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"slices"
"strings"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
)
@ -15,26 +17,62 @@ const (
envConfigPath = "CONFIG_PATH"
)
// getConfigPath gets the spaceapid configuration from the respective environment variables
func getConfigPath() string {
// JSON template path
type pathsT []string
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)
if !ok || configPath == "" {
log.Fatalln("Could not retrieve", envConfigPath, "env variable or variable is empty")
if ok {
if configPath == "" {
log.Fatalln("Env variable", envConfigPath, "is present but empty.")
}
// Save as absolute path
absConfigPath, err := filepath.Abs(configPath)
_ = flagConfigPaths.Set(configPath)
}
for _, path := range flagConfigPaths {
_, err := os.Stat(path)
if err != nil {
log.Fatalln("Failed converting", configPath, "to absolute path:", err)
log.Fatalln("Config file", path, "doesn't exist or is not accessible, error:", err)
}
return absConfigPath
paths = append(paths, path)
}
// 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
func ParseConfiguration() (conf SpaceapidConfig) {
log.Println("Parsing configuration file")
log.Println("Parsing configuration files")
paths := getConfigPaths()
log.Println(paths)
for _, path := range paths {
// Read file
file, err := os.ReadFile(getConfigPath())
file, err := os.ReadFile(path)
if err != nil {
log.Fatalln("Failed reading file:", err)
}
@ -46,6 +84,7 @@ func ParseConfiguration() (conf SpaceapidConfig) {
if err != nil {
log.Fatalln("Could not parse spaceapid config file:", err)
}
}
// Check if compatible with v14
if !slices.Contains(conf.Response.APICompatibility, "14") {

16
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"flag"
"fmt"
"log"
"net/http"
@ -15,7 +16,22 @@ import (
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
)
var (
flagHelp bool
)
func init() {
flag.BoolVar(&flagHelp, "h", false, "Print help output")
}
func main() {
flag.Parse()
if flagHelp {
flag.PrintDefaults()
os.Exit(0)
}
// Get spaceapid configuration
conf := config.ParseConfiguration()