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,26 +17,62 @@ 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.")
|
||||||
}
|
}
|
||||||
// Save as absolute path
|
_ = flagConfigPaths.Set(configPath)
|
||||||
absConfigPath, err := filepath.Abs(configPath)
|
}
|
||||||
|
|
||||||
|
for _, path := range flagConfigPaths {
|
||||||
|
_, err := os.Stat(path)
|
||||||
if err != nil {
|
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
|
// ParseConfiguration returns the config from file
|
||||||
func ParseConfiguration() (conf SpaceapidConfig) {
|
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
|
// Read file
|
||||||
file, err := os.ReadFile(getConfigPath())
|
file, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed reading file:", err)
|
log.Fatalln("Failed reading file:", err)
|
||||||
}
|
}
|
||||||
|
@ -46,6 +84,7 @@ func ParseConfiguration() (conf SpaceapidConfig) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Could not parse spaceapid config file:", err)
|
log.Fatalln("Could not parse spaceapid config file:", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if compatible with v14
|
// Check if compatible with v14
|
||||||
if !slices.Contains(conf.Response.APICompatibility, "14") {
|
if !slices.Contains(conf.Response.APICompatibility, "14") {
|
||||||
|
|
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