diff options
Diffstat (limited to 'src/runtime_config.go')
| -rw-r--r-- | src/runtime_config.go | 62 | 
1 files changed, 62 insertions, 0 deletions
diff --git a/src/runtime_config.go b/src/runtime_config.go new file mode 100644 index 0000000..07e4a7c --- /dev/null +++ b/src/runtime_config.go @@ -0,0 +1,62 @@ +// vim: shiftwidth=4 tabstop=4 noexpandtab + +package main + +import ( +	"log" +	"io/ioutil" +	"os" +	"flag" +	"encoding/json" +) + +type RuntimeConfig struct { +	Flag FlagConfig +	Web WebConfig `json:"webserver"` +	Transport TransportConfig `json:"file_transport"` +} + +func GetRuntimeConfig() RuntimeConfig { + +	retval := RuntimeConfig{} + +	// read CLI parameters +	retval.Flag.read() + +	// open the config file +	configFile, err := os.Open(retval.Flag.ConfigPath) +	defer configFile.Close() +	if err != nil { +		log.Fatalf("Could not open configuration file %s", retval.Flag.ConfigPath) +	} + +	// read byte content +	byteData, err := ioutil.ReadAll(configFile) +	if err != nil { +		log.Fatalf("Could not read configuration file %s", retval.Flag.ConfigPath) +	} + +	// parse content to config structs +	err = json.Unmarshal(byteData, &retval) +	if err != nil { +		log.Fatalf("Could not parse configuration file %s", retval.Flag.ConfigPath) +	} + +	// patch default config in case of debugging +	if retval.Flag.Debug { +		retval.Transport.Enabled = true +	} + +	return retval +} + +type FlagConfig struct { +	ConfigPath string +	Debug bool +} + +func (flagConfig *FlagConfig) read() { +	flag.StringVar(&(flagConfig.ConfigPath), "c", "/etc/birdscan/config.json", "Path to birdscan configuration file") +	flag.BoolVar(&(flagConfig.Debug), "d", false, "A debug flag to be used by source repository Makefile") +	flag.Parse() +}  | 
