summaryrefslogtreecommitdiff
path: root/src/runtime_config.go
blob: 8c484d07d720503bbafad04ba0d4ac5650e5d902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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"`
}

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)
	}

	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()
}