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 (