summaryrefslogtreecommitdiff
path: root/config/config.go
diff options
context:
space:
mode:
authorPaul Bütow <pbuetow@mimecast.com>2020-01-09 20:30:15 +0000
committerPaul Bütow <pbuetow@mimecast.com>2020-01-09 20:30:15 +0000
commit3755a9911ecb05886577095f2b8cc8b9e4066a3a (patch)
tree86e24bc466986cb5c9c6d167a918e6064defeafc /config/config.go
Release of DTail v1.0.0v1.0.0
Diffstat (limited to 'config/config.go')
-rw-r--r--config/config.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 0000000..5463c5f
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,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"
+ }
+}