/
cmd.go
68 lines (60 loc) · 1.29 KB
/
cmd.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
package ginz
import (
"context"
"os"
"github.com/suisrc/logger.zgo"
"github.com/urfave/cli/v2"
)
var (
DEBUG = false
)
// SetConfigFile 设定配置文件
func SetServeConfig(s string) Option {
return func(o *Options) {
o.ConfigFile = s
}
}
// SetVersion 设定版本号b
func SetServeVersion(s string) Option {
return func(o *Options) {
o.Version = s
}
}
// SetBuildInjector 设定注入助手
func SetBuildInjector(b BuildInjector) Option {
return func(o *Options) {
o.BuildInjector = b
}
}
// CmdWeb ...
func CmdWeb(ctx context.Context, action func(c *cli.Context) error) *cli.Command {
return &cli.Command{
Name: "web",
Usage: "运行web服务",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "conf",
Aliases: []string{"c"},
Usage: "配置文件(.json,.yaml,.toml)",
DefaultText: "config.toml",
//Required: true,
},
},
Action: action,
}
}
func RunWeb(app *cli.App, run func(ctx context.Context, opts ...Option) error) {
ctx := context.Background()
app.Commands = []*cli.Command{
CmdWeb(ctx, func(c *cli.Context) error {
sc := SetServeConfig(c.String("conf"))
sv := SetServeVersion(app.Version)
return run(ctx, sc, sv)
}),
}
err := app.Run(os.Args)
if err != nil {
logger.Errorf(ctx, logger.ErrorWW(err))
panic(err)
}
}