app/system/setup.go

150 lines
3.3 KiB
Go
Raw 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]
2021-12-19 10:52:40 +00:00
viper.SetDefault("prod", true)
2021-12-16 04:11:33 +00:00
// 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 16:09:51 +00:00
Setup()
2021-12-16 04:11:33 +00:00
}
2021-12-16 16:09:51 +00:00
func Setup() {
2021-12-16 04:11:33 +00:00
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.DurationFieldInteger = true
zerolog.SetGlobalLevel(zerolog.Level(viper.GetInt("log.level")))
2021-12-19 10:52:40 +00:00
util.PROD = viper.GetBool("prod")
2021-12-19 18:46:00 +00:00
if !util.PROD {
2021-12-16 04:11:33 +00:00
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
2021-12-16 16:28:41 +00:00
ptr := 0
2021-12-16 11:44:07 +00:00
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
2021-12-16 16:28:41 +00:00
if ptr == 0 {
short = file[i+1:]
ptr = i
} else {
short = fmt.Sprintf("%v/%v", file[i+1:ptr], short)
break
2021-12-16 11:44:07 +00:00
}
2021-12-16 16:28:41 +00:00
}
if file[i] == '@' {
ptr = i
2021-12-16 11:44:07 +00:00
}
}
2021-12-19 18:51:43 +00:00
return fmt.Sprintf("%v:%v >", short, segs[len(segs)-1])
2021-12-16 11:44:07 +00:00
}
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")
}
2021-12-16 16:09:51 +00:00
Setup()
2021-12-16 04:11:33 +00:00
}
// 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
}