-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
78 lines (64 loc) · 1.7 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
package main
import (
"fmt"
"github.com/bmaximilian/gutils/cmd"
"github.com/bmaximilian/gutils/pkg/util/logger"
"github.com/spf13/viper"
"os"
"strings"
)
// Version of the cli tool
var Version = "0.0.1"
// Build (date) of the cli tool
var Build = "1"
// Set the default environment variables
// These variables can be overridden by the config file
func setDefaultEnvironment() {
defaultAppEnv := os.Getenv("APP_ENV")
if defaultAppEnv == "" {
defaultAppEnv = "production"
}
defaultConfigPathEnv := os.Getenv("ROOM_CALC_CONFIG_DIRECTORY")
if defaultConfigPathEnv == "" {
defaultConfigPathEnv = "."
}
viper.SetDefault("APP_ENV", defaultAppEnv)
viper.SetDefault("ROOM_CALC_CONFIG_DIRECTORY", defaultConfigPathEnv)
viper.SetDefault("VERSION", Version)
viper.SetDefault("BUILD", Build)
viper.SetDefault("log.file", "./logs/gutils.log")
viper.SetDefault("log.level", "INFO")
cmd.SetDefaults()
}
// Loads the config file
func initConfig() {
configSuffix := ""
switch viper.Get("APP_ENV") {
case "development":
configSuffix = ".development"
break
default:
break
}
viper.SetConfigType("yaml")
viper.AddConfigPath(viper.Get("ROOM_CALC_CONFIG_DIRECTORY").(string))
viper.SetConfigName("gutils.config" + configSuffix)
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
}
func main() {
setDefaultEnvironment()
initConfig()
logger.SetLogPath(viper.Get("log.file").(string))
logLevel := strings.ToUpper(viper.Get("log.level").(string))
if logger.GetLogLevelForName(logLevel) == nil {
logLevel = "INFO"
}
l := logger.GetLogger()
l.SetLogLevel(logLevel)
l.SetForceCli(true)
// From now on we can use the logger to log our messages
cmd.Execute()
}