/
config.go
234 lines (193 loc) · 6.54 KB
/
config.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package config
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/omakoto/go-common/src/fileutils"
"github.com/omakoto/go-common/src/utils"
"github.com/omakoto/zenlog/zenlog/envs"
"github.com/omakoto/zenlog/zenlog/util"
"runtime"
)
var (
isLogger = false
isLoggerSet = false
config *Config
)
func SetIsLogger(logger bool) {
if isLoggerSet {
util.Fatalf("SetIsLogger already set")
}
isLogger = logger
isLoggerSet = true
}
// Config represents configuration parameters, read from ~/.zenlog.toml and overridden with the environmental variables.
type Config struct {
LogDir string `toml:"ZENLOG_DIR"`
TempDir string `toml:"ZENLOG_TEMP"`
SourceDir string `toml:"ZENLOG_SOURCE_DIR"`
StartCommand string `toml:"ZENLOG_START_COMMAND"`
PrefixCommands string `toml:"ZENLOG_PREFIX_COMMANDS"`
AlwaysNoLogCommands string `toml:"ZENLOG_ALWAYS_NO_LOG_COMMANDS"`
AutoFlush bool `toml:"ZENLOG_AUTO_FLUSH"`
UseExperimentalCommandParser bool `toml:"ZENLOG_USE_EXPERIMENTAL_COMMAND_PARSER"`
CommandSplitter string `toml:"ZENLOG_COMMAND_SPLITTER"`
CommentSplitter string `toml:"ZENLOG_COMMENT_SPLITTER"`
Maxproc int `toml:"ZENLOG_MAXPROC"`
CriticalCrashMaxSeconds int `toml:"ZENLOG_CRITICAL_CRASH_MAX_SECONDS"`
UseSplice bool `toml:"ZENLOG_USE_SPLICE"`
ZenlogPid int
OuterTty string
LoggerIn string
LoggerOut string
}
// If a string is empty, get from a given environmental variabne.
func overwriteWithEnviron(to *string, envKey string, def string) {
if val := os.Getenv(envKey); val != "" {
*to = val
} else {
*to = os.ExpandEnv(*to)
}
if *to == "" {
*to = def
}
}
func overwriteBoolWithEnviron(to *bool, envKey string) {
if val := os.Getenv(envKey); val != "" {
*to = (val == "1")
}
}
// Make sure a string ends with a slash.
func ensureSlash(v *string) {
if strings.HasSuffix(*v, "/") {
return
}
*v = *v + "/"
}
// InitConfigForLogger returns a Config for a new session, loading from ~/.zenlog.toml and the environmental variables.
func InitConfigForLogger() *Config {
file := os.Getenv(envs.ZenlogConf)
if file == "" {
file = os.ExpandEnv("$HOME/.zenlog.toml")
}
var c Config
c.UseExperimentalCommandParser = true // Default to true.
c.UseSplice = true
util.Debugf("config=%s", file)
data, err := ioutil.ReadFile(file)
if err == nil {
if _, err := toml.Decode(string(data), &c); err != nil {
util.Fatalf("Unable to load %s: %s", file, err)
}
} else if os.IsNotExist(err) {
util.Warn(err, "%s doesn't exist; using the default instead", file)
}
overwriteWithEnviron(&c.StartCommand, "ZENLOG_START_COMMAND", "")
overwriteWithEnviron(&c.LogDir, envs.ZenlogDir, os.ExpandEnv("$HOME/zenlog/"))
overwriteWithEnviron(&c.SourceDir, envs.ZenlogSourceDir, "")
overwriteWithEnviron(&c.PrefixCommands, "ZENLOG_PREFIX_COMMANDS", `(?:command|builtin|time|sudo|[a-zA-Z0-9_]+\=.*)`)
overwriteWithEnviron(&c.AlwaysNoLogCommands, "ZENLOG_ALWAYS_NO_LOG_COMMANDS", `(?:vi|vim|man|nano|pico|emacs|zenlog.*)`)
overwriteWithEnviron(&c.CommandSplitter, "ZENLOG_COMMAND_SPLITTER", "")
overwriteWithEnviron(&c.CommentSplitter, "ZENLOG_COMMENT_SPLITTER", "")
overwriteWithEnviron(&c.TempDir, envs.ZenlogTemp, "")
overwriteBoolWithEnviron(&c.AutoFlush, envs.ZenlogAutoFlush)
overwriteBoolWithEnviron(&c.UseExperimentalCommandParser, envs.ZenlogUseExperimentalCommandParser)
overwriteBoolWithEnviron(&c.UseSplice, envs.ZenlogUseSplice)
if c.Maxproc < 1 {
c.Maxproc = 1
}
if c.StartCommand == "" {
shell := os.Getenv("SHELL")
if shell != "" {
c.StartCommand = "exec " + shell + " -l"
} else {
c.StartCommand = "exec /bin/sh -l"
}
}
if c.TempDir == "" || !fileutils.DirExists(c.TempDir) {
c.TempDir = utils.FirstNonEmpty(os.Getenv("TEMP"), os.Getenv("TMP"), "/tmp/")
}
ensureSlash(&c.LogDir)
ensureSlash(&c.TempDir)
// For E2E testing, override the PID with _ZENLOG_LOGGER_PID, if set.
c.ZenlogPid = util.GetIntEnv("_ZENLOG_LOGGER_PID", os.Getpid())
runtime.GOMAXPROCS(c.Maxproc)
util.Dump("Logger config=", c)
return &c
}
// InitConfigForCommands returns a Config for subcommands, loading from ~/.zenlog.toml and the environmental variables.
// Some of the parameters (such as ZenlogPid) will be inherited from the current zenlog session.
func InitConfigForCommands() *Config {
var c Config
// Take over some of the parameters from the logger.
pid, err := strconv.Atoi(os.Getenv(envs.ZenlogPid))
util.Check(err, "ZENLOG_PID not integer")
c.ZenlogPid = pid
c.LogDir = os.Getenv(envs.ZenlogDir)
c.SourceDir = os.Getenv(envs.ZenlogSourceDir)
c.OuterTty = os.Getenv(envs.ZenlogOuterTty)
c.LoggerIn = os.Getenv(envs.ZenlogLoggerIn)
c.LoggerOut = os.Getenv(envs.ZenlogLoggerOut)
if c.ZenlogPid == 0 {
util.Fatalf(envs.ZenlogPid + " not set.")
}
if c.LogDir == "" {
util.Fatalf(envs.ZenlogDir + " not set.")
}
if c.OuterTty == "" {
util.Fatalf(envs.ZenlogOuterTty + " not set.")
}
if c.LoggerIn == "" {
util.Fatalf(envs.ZenlogLoggerIn + " not set.")
}
if c.LoggerOut == "" {
util.Fatalf(envs.ZenlogLoggerOut + " not set.")
}
// We still need to load certain parameters from TOML.
lc := InitConfigForLogger()
c.AlwaysNoLogCommands = lc.AlwaysNoLogCommands
c.PrefixCommands = lc.PrefixCommands
c.CommandSplitter = lc.CommandSplitter
c.CommentSplitter = lc.CommentSplitter
c.UseExperimentalCommandParser = lc.UseExperimentalCommandParser
c.TempDir = lc.TempDir
c.Maxproc = lc.Maxproc
util.Dump("Command config=", c)
return &c
}
// GetConfig returns the singleton config suitable for the current run mode.
func GetConfig() *Config {
if config == nil {
if isLogger {
config = InitConfigForLogger()
} else {
config = InitConfigForCommands()
}
}
return config
}
// ZenlogSrcTopDir returns the fullpath of the source top directory.
func ZenlogSrcTopDir() string {
zenlogBinDir := util.FindZenlogBinDir()
// Note, this needs to work even outside of a zenlog session, where InitConfigForCommands()
// doesn't work, so we need to use InitConfigForLogger().
config = InitConfigForLogger()
configSourceDir := config.SourceDir
if fileutils.DirExists(configSourceDir + "/subcommands") {
return configSourceDir
}
for _, d := range utils.StringSlice("/../", "/../zenlog/", "/../src/github.com/omakoto/zenlog/") {
candidate := zenlogBinDir + d
candidate, err := filepath.Abs(candidate)
util.Check(err, "Abs failed")
if fileutils.DirExists(candidate + "/subcommands") {
return candidate
}
}
log.Fatalf("Zenlog source directory not found.")
return ""
}