-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
122 lines (118 loc) · 2.84 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//go:generate chmod +x ./scripts/generate/generate-bindata.sh
//go:generate ./scripts/generate/generate-bindata.sh
package main
import (
"fmt"
"github.com/IoTDevice/phicomm-r1-controler/config"
"github.com/IoTDevice/phicomm-r1-controler/services"
//_ "github.com/go-bindata/go-bindata"
"github.com/urfave/cli/v2"
"log"
"os"
)
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)
func main() {
myApp := cli.NewApp()
myApp.Name = "phicomm-r1-controler"
myApp.Usage = "-c [config file path]"
myApp.Version = buildVersion(version, commit, date, builtBy)
myApp.Commands = []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "init config file",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"ConfigFilePath"},
Destination: &config.ConfigFilePath,
},
},
Action: func(c *cli.Context) error {
config.LoadSnapcraftConfigPath()
config.InitConfigFile()
return nil
},
},
{
Name: "r1-ip",
Aliases: []string{"r"},
Usage: "斐讯R1的ip直接免配置文件运行",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "ip",
Aliases: []string{"i"},
Value: "",
Usage: "斐讯R1的ip地址",
EnvVars: []string{"R1IP"},
Destination: &config.SingleIpPort,
},
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: 0,
Usage: "指定提供服务的端口",
EnvVars: []string{"R1PORT"},
Destination: &config.SingleServicePort,
},
},
Action: func(c *cli.Context) error {
config.LoadSnapcraftConfigPath()
return services.Run(c)
},
},
{
Name: "test",
Aliases: []string{"t"},
Usage: "test this command",
Action: func(c *cli.Context) error {
fmt.Println("ok")
return nil
},
},
}
myApp.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"ConfigFilePath"},
Destination: &config.ConfigFilePath,
},
}
myApp.Action = func(c *cli.Context) error {
config.LoadSnapcraftConfigPath()
_, err := os.Stat(config.ConfigFilePath)
if err != nil {
config.InitConfigFile()
}
config.UseConfigFile()
return services.Run(c)
}
err := myApp.Run(os.Args)
if err != nil {
log.Println(err.Error())
}
}
func buildVersion(version, commit, date, builtBy string) string {
var result = version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
return result
}