-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
73 lines (58 loc) · 1.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"math/rand"
"os"
"runtime"
"time"
"github.com/spf13/cobra"
"github.com/yubo/golib/configer"
"github.com/yubo/golib/proc"
"k8s.io/klog/v2"
)
const (
moduleName = "golib-multi-cmds"
)
func main() {
cmd := newRootCmd()
cmd.AddCommand(
newHelloCmd(),
newStartCmd(),
)
if err := cmd.Execute(); err != nil {
os.Exit(proc.PrintErrln(err))
}
}
type config struct {
UserName string `json:"userName" flag:"user-name" default:"tom" description:"user name"`
}
func newRootCmd() *cobra.Command {
rand.Seed(time.Now().UnixNano())
runtime.GOMAXPROCS(runtime.NumCPU())
return &cobra.Command{}
}
func newStartCmd() *cobra.Command {
cf := &config{}
cmd := &cobra.Command{
Use: "start",
Short: "start demo",
RunE: func(cmd *cobra.Command, args []string) error {
klog.Infof("hello %s", cf.UserName)
return nil
},
}
configer.FlagSet(cmd.Flags(), cf)
return cmd
}
func newHelloCmd() *cobra.Command {
cf := &config{}
cmd := &cobra.Command{
Use: "hello",
Short: "hello demo",
RunE: func(cmd *cobra.Command, args []string) error {
klog.Infof("hello %s", cf.UserName)
return nil
},
}
configer.FlagSet(cmd.Flags(), cf)
return cmd
}