This commit is contained in:
loveuer
2024-07-26 17:59:02 +08:00
parent d55c6b1932
commit eea192e385
14 changed files with 250 additions and 22 deletions

73
internal/cmd/cli.go Normal file
View File

@ -0,0 +1,73 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"net/rpc"
"net/url"
"ultone/internal/log"
"ultone/internal/unix"
)
var (
cliCommand = &cobra.Command{
Use: "cli",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.Debug(cmd.Context(), "[cmd.cli] svc address: %s", svc)
uri, err := url.Parse(svc)
if err != nil {
return err
}
if cliClient, err = rpc.Dial(uri.Scheme, uri.Host+uri.Path); err != nil {
return fmt.Errorf("rpc dial [%s] [%s] err: %w", uri.Scheme, uri.Host+uri.Path, err)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
log.Debug(cmd.Context(), "[cli] start run cli... all args: %v", args)
return nil
},
}
svc string
cliClient *rpc.Client
)
func initCli() {
cliCommand.PersistentFlags().StringVar(&svc, "svc", "unix:///tmp/.cli.sock", "server unix listen address")
cliCommand.AddCommand(&cobra.Command{
Use: "set",
RunE: func(cmd *cobra.Command, args []string) (err error) {
log.Debug(cmd.Context(), "[cli.set] all args: %v", args)
if len(args) < 2 {
return fmt.Errorf("at least 2 args required")
}
switch args[0] {
case "debug":
out := &unix.Resp[bool]{}
in := &unix.SettingReq{Debug: false}
switch args[1] {
case "true":
in.Debug = true
case "false":
default:
return fmt.Errorf("unknown debug value")
}
if err = cliClient.Call("svc.Setting", in, out); err != nil {
return err
}
log.Info(cmd.Context(), out.Msg)
}
return nil
},
})
}

View File

@ -6,17 +6,18 @@ import (
"ultone/internal/controller"
"ultone/internal/database/cache"
"ultone/internal/database/db"
"ultone/internal/log"
"ultone/internal/model"
"ultone/internal/opt"
"ultone/internal/tool"
"ultone/internal/unix"
)
var (
filename string
)
func Execute(ctx context.Context) error {
func execute(ctx context.Context) error {
tool.Must(opt.Init(filename))
tool.Must(db.Init())
tool.Must(cache.Init())
@ -31,7 +32,17 @@ func Execute(ctx context.Context) error {
tool.Must(controller.Init(ctx))
tool.Must(api.Start(ctx))
// todo: if need some cli operation, should start local unix rpc svc
tool.Must(unix.Start(ctx))
<-ctx.Done()
log.Warn(ctx, "received quit signal...(2s)")
<-tool.Timeout(2).Done()
return nil
}
func Execute(ctx context.Context) error {
return rootCommand.ExecuteContext(ctx)
}

View File

@ -1,16 +1,12 @@
package cmd
import (
"flag"
"time"
"ultone/internal/opt"
)
func init() {
time.Local = time.FixedZone("CST", 8*3600)
flag.StringVar(&filename, "c", "etc/config.json", "config json file path")
flag.BoolVar(&opt.Debug, "debug", false, "")
flag.Parse()
initRoot()
initCli()
}

29
internal/cmd/root.go Normal file
View File

@ -0,0 +1,29 @@
package cmd
import (
"github.com/loveuer/nf/nft/log"
"github.com/spf13/cobra"
"ultone/internal/opt"
)
func initRoot() {
rootCommand.PersistentFlags().BoolVar(&opt.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 {
log.SetLogLevel(log.LogLevelDebug)
}
}
rootCommand.AddCommand(cliCommand)
}
var (
rootCommand = &cobra.Command{
Use: "nf-app",
RunE: func(cmd *cobra.Command, args []string) error {
return execute(cmd.Context())
},
}
)