-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (87 loc) · 2.11 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/jblawatt/gomsglog/gomsglog"
"github.com/jblawatt/gomsglog/gomsglog/cmd"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
var (
binary = "ml"
version = "dev"
builDate = time.Now().Format("2006-01-02")
)
func main() {
setVersionInfo(binary, version, builDate)
setupViper()
if err := cmd.RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func setVersionInfo(binary string, version string, build string) {
gomsglog.SetBinaryInfo(binary)
gomsglog.SetVersionInfo(version)
gomsglog.SetBuildDateInfo(builDate)
}
func setupViper() {
viper.SetEnvPrefix("ML_")
viper.AutomaticEnv()
viper.BindEnv("debug", "ML_DEBUG")
viper.SetConfigName(".mlrc")
viper.AddConfigPath(".")
viper.AddConfigPath(os.Getenv("HOME"))
if runtime.GOOS == "windows" {
viper.AddConfigPath(path.Join(os.Getenv("APPDATA"), "ml"))
}
if runtime.GOOS == "linux" {
viper.AddConfigPath(path.Join(os.Getenv("HOME"), ".config", "ml"))
}
viper.ReadInConfig()
viper.SetDefault("database.dialect", "sqlite3")
viper.SetDefault("database.connectionstring", "db.sqlite3")
viper.SetDefault("database.debug", false)
viper.SetDefault("loglevel", "WARN")
viper.SetDefault("debug", false)
if runtime.GOOS == "windows" {
viper.SetDefault("editor", "notepad")
}
if runtime.GOOS == "linux" {
viper.SetDefault("editor", "vim")
}
setupLog()
jww.DEBUG.Printf("Using config file %s.", viper.ConfigFileUsed())
}
var thresholds = []jww.Threshold{
jww.LevelTrace,
jww.LevelDebug,
jww.LevelInfo,
jww.LevelWarn,
jww.LevelError,
jww.LevelFatal,
jww.LevelCritical,
}
func setupLog() {
logfile := viper.GetString("logfile")
level := viper.GetString("loglevel")
level = strings.ToUpper(level)
for _, t := range thresholds {
if t.String() == level {
jww.SetLogThreshold(t)
}
}
if logfile != "" && logfile != "stdout" {
f, _ := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
log.SetOutput(f)
jww.SetLogOutput(f)
} else {
log.SetOutput(os.Stdout)
jww.SetLogOutput(os.Stdout)
}
}