app/system/setup.go

128 lines
2.8 KiB
Go
Raw Normal View History

2021-12-16 04:11:33 +00:00
package system
import (
"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")
}
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
}
// if len(c) > 0 {
// }
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
}