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
|
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
}
func (c *initializer) parseConfig(args *Args) {
if strings.ToUpper(args.ConfigFile) == "NONE" {
return
}
if args.ConfigFile != "" {
c.parseSpecificConfig(args.ConfigFile)
return
}
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)
}
}
}
}
func (c *initializer) parseSpecificConfig(configFile string) {
fd, err := os.Open(configFile)
if err != nil {
panic(fmt.Sprintf("Unable to read config file: %v", err))
}
defer fd.Close()
cfgBytes, err := ioutil.ReadAll(fd)
if err != nil {
panic(fmt.Sprintf("Unable to read config file %s: %v", configFile, err))
}
err = json.Unmarshal([]byte(cfgBytes), c)
if err != nil {
panic(fmt.Sprintf("Unable to parse config file %s: %v", configFile, err))
}
}
func (c *initializer) transformConfig(sourceProcess source.Source, args *Args, additionalArgs []string,
client *ClientConfig, server *ServerConfig, common *CommonConfig) (*ClientConfig, *ServerConfig, *CommonConfig) {
if args.LogDir != "" {
common.LogDir = args.LogDir
}
if strings.Contains(common.LogDir, "~/") {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
common.LogDir = strings.ReplaceAll(common.LogDir, "~/", fmt.Sprintf("%s/", homeDir))
}
if common.LogStrategy == "" {
common.LogStrategy = "daily"
}
if args.Spartan {
args.Quiet = true
args.NoColor = true
if args.LogLevel == "" {
args.LogLevel = "ERROR"
}
}
if args.NoColor {
client.TermColorsEnable = false
}
if args.LogLevel != "" {
common.LogLevel = args.LogLevel
} else if sourceProcess == source.Client && args.ServersStr == "" && args.Discovery == "" {
// We are in serverless mode. Default log level is WARN.
common.LogLevel = "WARN"
}
if args.SSHPort != DefaultSSHPort {
common.SSHPort = args.SSHPort
}
if args.Discovery == "" && args.ServersStr == "" {
// We are not connecting to any servers.
args.Serverless = true
}
// Interpret additional args as file list.
if args.What == "" {
var files []string
for _, file := range flag.Args() {
files = append(files, file)
}
args.What = strings.Join(files, ",")
}
return client, server, common
}
|