feat: add yaml config support

This commit is contained in:
loveuer
2025-03-20 11:55:47 +08:00
parent 32b30ae183
commit 2e13671bb7
9 changed files with 139 additions and 145 deletions

View File

@ -10,8 +10,15 @@ import (
"ultone/internal/unix"
)
var (
cliCommand = &cobra.Command{
var ()
func initCli() *cobra.Command {
var (
svc string
cliClient *rpc.Client
)
cmd := &cobra.Command{
Use: "cli",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.Debug(cmd.Context(), "[cmd.cli] svc address: %s", svc)
@ -34,13 +41,8 @@ var (
},
}
svc string
cliClient *rpc.Client
)
func initCli() {
cliCommand.PersistentFlags().StringVar(&svc, "svc", opt.RpcAddress, "server unix listen address")
cliCommand.AddCommand(&cobra.Command{
cmd.PersistentFlags().StringVar(&svc, "svc", opt.RpcAddress, "server unix listen address")
cmd.AddCommand(&cobra.Command{
Use: "set",
RunE: func(cmd *cobra.Command, args []string) (err error) {
log.Debug(cmd.Context(), "[cli.set] all args: %v", args)
@ -73,4 +75,6 @@ func initCli() {
return nil
},
})
return cmd
}

View File

@ -7,6 +7,5 @@ import (
func init() {
time.Local = time.FixedZone("CST", 8*3600)
initRoot()
initCli()
initRoot(initCli())
}

View File

@ -6,17 +6,17 @@ import (
"ultone/internal/opt"
)
func initRoot() {
rootCommand.PersistentFlags().BoolVar(&opt.Debug, "debug", false, "debug mode")
func initRoot(cmds ...*cobra.Command) {
rootCommand.PersistentFlags().BoolVar(&opt.Cfg.Debug, "debug", false, "debug mode")
rootCommand.PersistentFlags().StringVarP(&filename, "config", "c", "etc/config.json", "config json file path")
rootCommand.PersistentPreRun = func(cmd *cobra.Command, args []string) {
if opt.Debug {
if opt.Cfg.Debug {
log.SetLogLevel(log.LogLevelDebug)
}
}
rootCommand.AddCommand(cliCommand)
rootCommand.AddCommand(cmds...)
}
var (

View File

@ -39,7 +39,7 @@ func (c *Client) Session(ctxs ...context.Context) *gorm.DB {
session := c.cli.Session(&gorm.Session{Context: ctx})
if opt.Debug {
if opt.Cfg.Debug {
session = session.Debug()
}

View File

@ -1,71 +1,69 @@
package opt
import (
"encoding/json"
"fmt"
"github.com/loveuer/nf/nft/log"
"os"
"github.com/spf13/viper"
"ultone/internal/tool"
)
type listen struct {
Http string `json:"http"`
Grpc string `json:"grpc"`
Http string `mapstructure:"http"`
Grpc string `mapstructure:"grpc"`
}
type db struct {
Type string `json:"-"` // postgres, mysql, sqlite
Uri string `json:"uri"`
Type string `mapstructure:"-"` // postgres, mysql, sqlite
Uri string `mapstructure:"uri"`
}
type cache struct {
Uri string `json:"uri"`
Uri string `mapstructure:"uri"`
}
type es struct {
Uri string `json:"uri"`
Uri string `mapstructure:"uri"`
Index struct {
Staff string `json:"staff"`
} `json:"index"`
Staff string `mapstructure:"staff"`
} `mapstructure:"index"`
}
type Nebula struct {
Uri string `json:"uri"`
Username string `json:"username"`
Password string `json:"password"`
Space string `json:"space"`
Uri string `mapstructure:"uri"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Space string `mapstructure:"space"`
}
type config struct {
Name string `json:"name"`
Listen listen `json:"listen"`
DB db `json:"db"`
Cache cache `json:"cache"`
ES es `json:"es"`
Nebula Nebula `json:"nebula"`
Name string `mapstructure:"name"`
Debug bool `mapstructure:"-"`
Listen listen `mapstructure:"listen"`
DB db `mapstructure:"db"`
Cache cache `mapstructure:"cache"`
ES es `mapstructure:"es"`
Nebula Nebula `mapstructure:"nebula"`
}
var (
Debug bool
Mode string
Cfg = &config{}
Cfg = &config{}
)
func Init(filename string) error {
var (
err error
bs []byte
)
log.Info("opt.Init: start reading config file: %s", filename)
if bs, err = os.ReadFile(filename); err != nil {
return fmt.Errorf("opt.Init: read config file=%s err=%v", filename, err)
viper.SetConfigFile(filename)
if err = viper.ReadInConfig(); err != nil {
return fmt.Errorf("opt.Init: readin config failed, err = %s", err.Error())
}
if err = json.Unmarshal(bs, Cfg); err != nil {
return fmt.Errorf("opt.Init: json marshal config=%s err=%v", string(bs), err)
if err = viper.Unmarshal(Cfg); err != nil {
return fmt.Errorf("opt.Init: unmarshal config failed, err = %s", err.Error())
}
tool.TablePrinter(Cfg)

View File

@ -42,10 +42,10 @@ func (h *Handler) Setting(in *SettingReq, out *Resp[bool]) error {
defer opt.Locker.Unlock()
if in.Debug {
opt.Debug = true
opt.Cfg.Debug = true
log.Info(h.Ctx, "set global debug[true]")
} else {
opt.Debug = false
opt.Cfg.Debug = false
log.Info(h.Ctx, "set global debug[false]")
}