2023-11-05 20:23:31 +01:00
package config
2024-01-13 23:26:04 +01:00
import (
"bytes"
"encoding/json"
2024-01-14 04:20:03 +01:00
"flag"
"fmt"
2024-01-13 23:26:04 +01:00
"log"
"os"
"slices"
2024-01-14 04:20:03 +01:00
"strings"
2024-01-14 01:02:30 +01:00
2024-01-31 17:11:15 +01:00
"git.hamburg.ccc.de/ccchh/spaceapid/types"
2024-01-13 23:26:04 +01:00
)
const (
2024-08-03 21:56:26 +02:00
envConfigPath = "SPACEAPID_CONFIG_PATH"
2024-01-13 23:26:04 +01:00
)
2024-01-14 04:20:03 +01:00
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
2024-01-13 23:26:04 +01:00
configPath , ok := os . LookupEnv ( envConfigPath )
2024-01-14 04:20:03 +01:00
if ok {
if configPath == "" {
log . Fatalln ( "Env variable" , envConfigPath , "is present but empty." )
}
_ = flagConfigPaths . Set ( configPath )
2024-01-13 23:26:04 +01:00
}
2024-01-14 04:20:03 +01:00
for _ , path := range flagConfigPaths {
_ , err := os . Stat ( path )
if err != nil {
log . Fatalln ( "Config file" , path , "doesn't exist or is not accessible, error:" , err )
}
paths = append ( paths , path )
}
// If paths is still empty we are missing something
if len ( paths ) == 0 {
2024-08-03 21:58:51 +02:00
log . Fatalln ( "Error: Neither env: " , envConfigPath , "nor cli flag was specified, unable to start without config." )
2024-01-13 23:26:04 +01:00
}
2024-01-14 04:20:03 +01:00
return
2024-01-13 23:26:04 +01:00
}
// ParseConfiguration returns the config from file
func ParseConfiguration ( ) ( conf SpaceapidConfig ) {
2024-01-14 04:20:03 +01:00
log . Println ( "Parsing configuration files" )
2024-01-13 23:26:04 +01:00
2024-01-14 04:20:03 +01:00
paths := getConfigPaths ( )
log . Println ( paths )
for _ , path := range paths {
// Read file
file , err := os . ReadFile ( path )
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 )
}
2024-01-13 23:26:04 +01:00
}
// Check if compatible with v14
if ! slices . Contains ( conf . Response . APICompatibility , "14" ) {
log . Fatalln ( "Provided file doesn't specify compatibility with API version 14" )
}
2024-08-03 21:58:51 +02:00
// Initialize fields for environment sensors
2024-01-14 01:02:30 +01:00
conf . Response . Sensors = make ( map [ string ] [ ] types . EnvironmentSensor )
for key , sensorConfigs := range conf . Dynamic . Sensors {
conf . Response . Sensors [ key ] = make ( [ ] types . EnvironmentSensor , len ( sensorConfigs ) )
for i , sensorConfig := range sensorConfigs {
conf . Response . Sensors [ key ] [ i ] = sensorConfig . SensorData
}
}
2024-01-13 23:26:04 +01:00
return
2023-11-05 20:23:31 +01:00
}