-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
113 lines (101 loc) · 2.25 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
package main
import (
// 需在此处添加代码。[1]
"github.com/cobolbaby/log-agent/cmd"
"github.com/kardianos/osext"
"github.com/kardianos/service"
"log"
"os"
"path/filepath"
// "runtime"
)
const Usage = `
Usage:
throttle <ops> [<duration>]
throttle -h | --help
throttle --version
Options:
-h, --help output help information
-v, --version output version
`
var (
GIT_COMMIT string
BUILD_TIME string
GO_VERSION string
)
type program struct{}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
cmd.Run()
}
func (p *program) Stop(s service.Service) error {
return nil
}
func getDefaultConfigPath() (string, error) {
fullexecpath, err := osext.Executable()
if err != nil {
return "", err
}
dir, _ := filepath.Split(fullexecpath)
return filepath.Join(dir, "conf", "logagent.ini"), nil
}
func main() {
// runtime.GOMAXPROCS(runtime.NumCPU() / 2.0)
//服务的配置信息
configPath, err := getDefaultConfigPath()
if err != nil {
log.Fatal(err)
}
cfg := &service.Config{
Name: "DCAgent",
Arguments: []string{"-c", configPath},
}
// Interface 接口
prg := &program{}
// 构建服务对象
s, err := service.New(prg, cfg)
if err != nil {
log.Fatal(err)
}
// logger 用于记录系统日志
logger, err := s.Logger(nil)
if err != nil {
log.Fatal(err)
}
// 依据传入参数来决定是执行start/status/stop
// os.Args 提供原始命令行参数访问功能。注意,切片中的第一个参数是该程序的路径,并且 os.Args[1:]保存所有程序的的参数。
args := os.Args
if len(args) < 2 {
log.Fatal(Usage)
}
switch args[1] {
case "-v", "version":
log.Println("Git Commit: " + GIT_COMMIT)
log.Println("Build Time: " + BUILD_TIME)
log.Println("Go Version: " + GO_VERSION)
case "-c":
if len(args) < 3 {
log.Fatal(Usage)
}
if err = s.Run(); err != nil {
logger.Error(err)
}
case "-t":
if len(args) < 3 {
log.Fatal(Usage)
}
cmd.Test()
case "start", "stop", "restart", "install", "uninstall":
// Ps: 需要拥有管理员的权限
if err = service.Control(s, os.Args[1]); err != nil {
log.Fatal(err)
}
case "status":
// TODO:查看当前程序运行状态
default:
log.Fatal(Usage)
}
}