summaryrefslogtreecommitdiff
path: root/config/config.go
blob: 5463c5f39fae133583f51f679535e9146f6cc515 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package config

import (
	"encoding/json"
	"io/ioutil"
	"os"
)

// ControlUser is used for various DTail specific operations.
const ControlUser string = "DTAIL-CONTROL-USER"

// Client holds a DTail client configuration.
var Client *ClientConfig

// Server holds a DTail server configuration.
var Server *ServerConfig

// Common holds common configs of both both, client and server.
var Common *CommonConfig

// Used to initialize the configuration.
type configInitializer struct {
	Common *CommonConfig
	Server *ServerConfig
	Client *ClientConfig
}

// Parse and read a given config file in JSON format.
func (c *configInitializer) parseConfig(configFile string) {
	fd, err := os.Open(configFile)
	if err != nil {
		panic(err)
	}
	defer fd.Close()

	cfgBytes, err := ioutil.ReadAll(fd)
	if err != nil {
		panic(err)
	}

	err = json.Unmarshal([]byte(cfgBytes), c)
	if err != nil {
		panic(err)
	}
}

// Init the DTail configuration.
func Init(configFile string) {
	initializer := configInitializer{
		Common: newDefaultCommonConfig(),
		Server: newDefaultServerConfig(),
		Client: newDefaultClientConfig(),
	}

	if configFile == "" {
		configFile = "./cfg/dtail.json"
	}

	if _, err := os.Stat(configFile); !os.IsNotExist(err) {
		initializer.parseConfig(configFile)
	}

	// Assign pointers to global variables, so that we can access the
	// configuration from any place of the program.
	Common = initializer.Common
	Server = initializer.Server
	Client = initializer.Client

	if Server.MapreduceLogFormat == "" {
		Server.MapreduceLogFormat = "default"
	}
}