Add RunClient function, it will used by app dashboard

This commit is contained in:
shawnlu
2024-07-07 21:29:02 +08:00
parent d8bf22bef0
commit d2a06d2685
+57
View File
@@ -1,10 +1,67 @@
package cli
import (
"os"
"github.com/lucheng0127/virtuallan/pkg/client"
"github.com/urfave/cli/v2"
)
type ClientOpts func(*ClientFlags)
type ClientFlags struct {
target string
user string
passwd string
key string
logLevel string
}
func SetClientTarget(t string) ClientOpts {
return func(c *ClientFlags) {
c.target = t
}
}
func SetClientUser(u string) ClientOpts {
return func(c *ClientFlags) {
c.user = u
}
}
func SetClientPasswd(p string) ClientOpts {
return func(c *ClientFlags) {
c.passwd = p
}
}
func SetClientKey(k string) ClientOpts {
return func(c *ClientFlags) {
c.key = k
}
}
func SetClientLogLevel(l string) ClientOpts {
return func(c *ClientFlags) {
c.logLevel = l
}
}
func RunClient(opts ...ClientOpts) error {
clientFlags := new(ClientFlags)
for _, opt := range opts {
opt(clientFlags)
}
app := &cli.App{
Commands: []*cli.Command{
NewClientCmd(),
},
}
return app.Run([]string{os.Args[0], "client", "-t", clientFlags.target, "-u", clientFlags.user, "-p", clientFlags.passwd, "-k", clientFlags.key, "-l", clientFlags.logLevel})
}
func NewClientCmd() *cli.Command {
return &cli.Command{
Name: "client",