summaryrefslogtreecommitdiff
path: root/internal/config/initializer.go
blob: 5247699bac928243505f6e16ac3516df258cade7 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package config

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"

	"github.com/mimecast/dtail/internal/source"
)

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

type transformCb func(*initializer, *Args, []string) error

func (c *initializer) parseConfig(args *Args) error {
	if strings.ToUpper(args.ConfigFile) == "NONE" {
		return nil
	}

	if args.ConfigFile != "" {
		return c.parseSpecificConfig(args.ConfigFile)
	}

	if homeDir, err := os.UserHomeDir(); err != nil {
		var paths []string
		paths = append(paths, fmt.Sprintf("%s/.config/dtail/dtail.conf", homeDir))
		paths = append(paths, fmt.Sprintf("%s/.dtail.conf", homeDir))
		for _, configPath := range paths {
			if _, err := os.Stat(configPath); !os.IsNotExist(err) {
				c.parseSpecificConfig(configPath)
			}
		}
	}

	return nil
}

func (c *initializer) parseSpecificConfig(configFile string) error {
	fd, err := os.Open(configFile)
	if err != nil {
		return fmt.Errorf("Unable to read config file: %v", err)
	}
	defer fd.Close()

	cfgBytes, err := ioutil.ReadAll(fd)
	if err != nil {
		return fmt.Errorf("Unable to read config file %s: %v", configFile, err)
	}

	if err := json.Unmarshal([]byte(cfgBytes), c); err != nil {
		return fmt.Errorf("Unable to parse config file %s: %v", configFile, err)
	}

	return nil
}

func (i *initializer) transformConfig(sourceProcess source.Source, args *Args, additionalArgs []string) error {

	switch sourceProcess {
	case source.Server:
		return i.optimusPrime(transformServer, args, additionalArgs)
	case source.Client:
		return i.optimusPrime(transformClient, args, additionalArgs)
	case source.HealthCheck:
		return i.optimusPrime(transformHealthCheck, args, additionalArgs)
	default:
		return fmt.Errorf("Unable to transform config, unknown source '%s'", sourceProcess)
	}
}

func (i *initializer) optimusPrime(sourceCb transformCb, args *Args, additionalArgs []string) error {
	// Copy args to config objects.
	if args.SSHPort != DefaultSSHPort {
		i.Common.SSHPort = args.SSHPort
	}
	if args.LogLevel != DefaultLogLevel {
		i.Common.LogLevel = args.LogLevel
	}
	if args.NoColor {
		i.Client.TermColorsEnable = false
	}
	if args.LogDir != "" {
		i.Common.LogDir = args.LogDir
	}
	if args.Logger != "" {
		i.Common.Logger = args.Logger
	}

	// Setup log directory.
	if strings.Contains(i.Common.LogDir, "~/") {
		homeDir, err := os.UserHomeDir()
		if err != nil {
			panic(err)
		}
		i.Common.LogDir = strings.ReplaceAll(i.Common.LogDir, "~/", fmt.Sprintf("%s/", homeDir))
	}

	// Serverless mode.
	if args.Discovery == "" && (args.ServersStr == "" ||
		strings.ToLower(args.ServersStr) == "serverless") {
		// We are not connecting to any servers.
		args.Serverless = true
		i.Common.LogLevel = "WARN"
	}

	// Source type specific transormations.
	sourceCb(i, args, additionalArgs)

	// Spartan mode.
	if args.Spartan {
		args.Quiet = true
		args.NoColor = true
		i.Client.TermColorsEnable = false
		if args.LogLevel == "" {
			args.LogLevel = "ERROR"
			i.Common.LogLevel = "ERROR"
		}
	}
	// Interpret additional args as file list or as query.
	if args.What == "" {
		var files []string
		for _, arg := range flag.Args() {
			if args.QueryStr == "" && strings.Contains(strings.ToLower(arg), "select ") {
				args.QueryStr = arg
				continue
			}
			files = append(files, arg)
		}
		args.What = strings.Join(files, ",")
	}

	return nil
}

func transformClient(i *initializer, args *Args, additionalArgs []string) error {
	return nil
}

func transformServer(i *initializer, args *Args, additionalArgs []string) error {
	return nil
}

func transformHealthCheck(i *initializer, args *Args, additionalArgs []string) error {
	args.TrustAllHosts = true
	if !args.Serverless && args.ServersStr == "" {
		args.ServersStr = fmt.Sprintf("localhost:%d", DefaultSSHPort)
	}
	return nil
}