forked from OpenIoTHub/gateway-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (106 loc) · 2.43 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
package main
import (
"fmt"
client "github.com/OpenIoTHub/gateway-go/client"
"github.com/OpenIoTHub/gateway-go/config"
"github.com/OpenIoTHub/gateway-go/services"
"github.com/urfave/cli/v2"
"log"
"os"
"time"
)
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)
func main() {
services.Version = version
myApp := cli.NewApp()
myApp.Name = "gateway-go"
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{"GatewayConfigFilePath"},
Destination: &config.ConfigFilePath,
},
},
Action: func(c *cli.Context) error {
config.InitConfigFile()
return nil
},
},
{
Name: "test",
Aliases: []string{"t"},
Usage: "test this command",
Action: func(c *cli.Context) error {
fmt.Println("ok")
return nil
},
},
}
myApp.Flags = []cli.Flag{
//TODO 应该设置工作目录,各组件共享
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: config.ConfigFilePath,
Usage: "config file path",
EnvVars: []string{"GatewayConfigFilePath"},
Destination: &config.ConfigFilePath,
},
//token 登录
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Value: config.GatewayLoginToken,
Usage: "login server by gateway token ",
EnvVars: []string{"GatewayLoginToken"},
Destination: &config.GatewayLoginToken,
},
}
myApp.Action = func(c *cli.Context) error {
if config.GatewayLoginToken != "" {
config.UseGateWayToken()
} else {
_, err := os.Stat(config.ConfigFilePath)
if err != nil {
config.InitConfigFile()
}
config.UseConfigFile()
}
go client.Run()
for {
time.Sleep(time.Hour)
}
}
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
}