wip: cli
This commit is contained in:
73
internal/cmd/cli.go
Normal file
73
internal/cmd/cli.go
Normal 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
|
||||
},
|
||||
})
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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
29
internal/cmd/root.go
Normal 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())
|
||||
},
|
||||
}
|
||||
)
|
Reference in New Issue
Block a user