app/system/setup.go

145 lines
3.2 KiB
Go
Raw Permalink Normal View History

2021-12-16 04:11:33 +00:00
package system
import (
2021-12-16 11:44:07 +00:00
"fmt"
2021-12-16 04:11:33 +00:00
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"kumoly.io/kumoly/app/util"
)
func init() {
Home, err := os.UserHomeDir()
if err != nil {
Home = "/"
}
// Setup defaults
// =========================================================
// App
viper.SetDefault("name", "app")
viper.SetDefault("domain", "kumoly.io")
viper.SetDefault("timezone", "Asia/Taipei")
// mode [prod, dev]
viper.SetDefault("prod", PROD)
// log.level : [-1(trace):5(panic)] 7 to disable
viper.SetDefault("log.level", int(zerolog.InfoLevel))
viper.SetDefault("log.pretty", false)
// data: data dir
viper.SetDefault("data", filepath.Join(Home, ".kumoly"))
// =========================================================
// viper settings
viper.SetEnvPrefix("app")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
viper.SetConfigType("json")
2021-12-16 06:25:57 +00:00
setup()
2021-12-16 04:11:33 +00:00
}
func setup() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.DurationFieldInteger = true
zerolog.SetGlobalLevel(zerolog.Level(viper.GetInt("log.level")))
if !viper.GetBool("prod") {
log.Logger = log.With().Caller().Logger()
}
if viper.GetBool("log.pretty") {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006/01/02 15:04:05",
FormatCaller: func(i interface{}) string {
var c string
if cc, ok := i.(string); ok {
c = cc
}
2021-12-16 11:44:07 +00:00
if len(c) > 0 {
// shorten caller to mod/file:line
segs := strings.Split(c, ":")
file := segs[len(segs)-2]
short := file
skip := false
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
if !skip {
skip = true
continue
}
short = file[i+1:]
break
}
}
return fmt.Sprintf("%v:%v", short, segs[len(segs)-1])
}
2021-12-16 04:11:33 +00:00
return c
},
})
}
l = log.With().Str("mod", "system").Logger()
tz, err := time.LoadLocation(viper.GetString("timezone"))
if err == nil {
time.Local = tz
} else {
l.Error().Err(err).Msg("setup error")
}
}
func SetConfigPath(path string) {
ConfigRead(path)
}
func SetDataPath(path string) {
viper.Set("data", path)
}
// ConfigRead read in config
func ConfigRead(cfgFile string) {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
// read config from default path
viper.SetConfigName("app")
viper.AddConfigPath(viper.GetString("data"))
}
if err := viper.ReadInConfig(); err != nil {
l.Error().Err(err).Msg("ReadInConfig error")
}
setup()
}
// ConfigWrite write current config to file
func ConfigWrite(file string) {
viper.WriteConfigAs(file)
}
// ConfigShow print config to stdout
func ConfigShow(format string) (string, error) {
if format == "" {
format = "json"
}
path := filepath.Join(viper.GetString("data"), "tmp")
util.Mkdir(path)
file := filepath.Join(path, "config."+format)
err := viper.WriteConfigAs(file)
if err != nil {
l.Error().Err(err).Msg("WriteConfigAs error")
return "", err
}
data, err := ioutil.ReadFile(file)
if err != nil {
l.Error().Err(err).Msg("ReadFile error")
return "", err
}
return string(data), nil
}