Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (63 loc) Β· 1.93 KB
/
Copy pathmain.go
File metadata and controls
73 lines (63 loc) Β· 1.93 KB
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
package main
import (
"os"
// Embed the IANA time-zone database into the binary so named zones
// (time.parse_time location=, in_location, is_valid_timezone) resolve even
// when the host has no /usr/share/zoneinfo β i.e. minimal/scratch/distroless
// containers and slimmed binaries. Costs ~450 KB; keeps the CLI portable
// ("runs anywhere") instead of depending on host tz data.
_ "time/tzdata"
"github.com/1set/starcli/cli"
"github.com/1set/starcli/config"
"github.com/1set/starcli/module/sys"
"github.com/1set/starcli/web"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
log *zap.SugaredLogger
)
func init() {
// fix for Windows terminal output
enableANSIControl()
}
func main() {
// parse args
args := cli.ParseArgs()
// set log level
initLogger(args.LogLevel)
// load config
if err := config.InitConfig(args.ConfigFile); err != nil {
log.Fatalw("fail to load config", zap.Error(err))
}
log.Debugw("config loaded", "config_file", viper.ConfigFileUsed(), "host_name", config.GetHostname())
// main
os.Exit(cli.Process(args))
}
func initLogger(level string) {
// build the root console logger (stderr), keeping diagnostics off stdout
lvl := zapcore.InfoLevel
if err := lvl.Set(level); err != nil {
lvl = zapcore.InfoLevel
}
encCfg := zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
core := zapcore.NewCore(zapcore.NewConsoleEncoder(encCfg), zapcore.Lock(os.Stderr), lvl)
log = zap.New(core).Sugar().With(zap.Int("pid", os.Getpid()))
// set log for sub-packages
cli.SetLog(log)
web.SetLog(log)
sys.SetLog(log)
}