-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (73 loc) · 2.2 KB
/
Copy pathmain.go
File metadata and controls
84 lines (73 loc) · 2.2 KB
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
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"examples/internal"
"fmt"
"log/slog"
"os"
"github.com/taobig/xcli"
"github.com/urfave/cli/v3"
)
var (
configFile string //参数变量:配置文件路径
logLevel string //参数变量:覆盖配置文件中的日志级别
)
func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
var serviceConf = &xcli.ServiceConfig{
ServiceName: "demoService",
ServiceDisplayName: "demoService",
ServiceDescription: "...",
EnvVars: map[string]string{
"ENV_VAR": "value",
},
}
startAction := func(ctx context.Context, cmd *cli.Command) {
fmt.Println("arg test:", cmd.Bool("test")) //获取参数示例一
fmt.Println("arg configFile:", configFile) //获取参数示例二
fmt.Println("ENV_VAR:", os.Getenv("ENV_VAR")) //获取环境变量示例
fmt.Println("programme start")
{ // optional: 如果执行完以上内容就希望结束进程,可以调用KillProcessItself。这样还是会正常触发stopAction回调的。
err := internal.KillProcessItself()
if err != nil {
panic(err)
}
}
// 注意:一定不要在这里执行无限循环、http ListenAndServe()等阻塞操作,否则将会导致程序在执行<start>子命令时无法正常返回
}
stopAction := func() error {
fmt.Println("programme exit")
return nil
}
//xCli, err := xcli.New(serviceConf, startAction, xcli.DefaultStopCallback)
xCli, err := xcli.New(serviceConf, startAction, stopAction)
if err != nil {
panic(err)
}
flags := []cli.Flag{
&cli.StringFlag{
Name: "configFile",
Aliases: []string{"f"},
Usage: "the config file",
Value: "config.yaml",
Destination: &configFile,
},
&cli.StringFlag{
Name: "logLevel",
Aliases: []string{"l"},
Usage: "the log level, overwrite the config file's log level. e.g., debug, info, warn, error, fatal, panic",
Value: "",
Destination: &logLevel,
},
&cli.BoolFlag{
// ./xx --test cCtx.Bool("test") => true
// ./xx cCtx.Bool("test") => false
Name: "test",
//Aliases: []string{"t"},
Usage: "test参数,不使用设置Destination方式接收",
Value: false,
},
}
var commands []*cli.Command
xCli.Start(flags, commands)
}