-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
install.go
74 lines (62 loc) · 1.58 KB
/
install.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/kardianos/service"
)
type program struct{}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}
func (p *program) run() {
// Do work here
main()
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
func serviceInstall(isInstall bool, serviceName string, user string) {
cwd, _ := os.Getwd()
wd, _ := filepath.Abs(cwd)
if serviceName == "" {
serviceName = "sealdice"
}
svcConfig := &service.Config{
Name: serviceName,
DisplayName: "SealDice Service",
Description: "SealDice: A TRPG Dice Bot Service.",
WorkingDirectory: wd,
}
if user != "" {
svcConfig.UserName = user
}
prg := &program{}
fmt.Fprintln(os.Stdout, "正在试图访问系统服务 ...")
s, err := service.New(prg, svcConfig)
if isInstall {
fmt.Fprintln(os.Stdout, "正在安装系统服务,安装完成后,SealDice将自动随系统启动")
if err != nil {
fmt.Fprintf(os.Stdout, "安装失败: %s\n", err.Error())
}
_, err = s.Logger(nil)
if err != nil {
fmt.Fprintf(os.Stdout, "安装失败: %s\n", err.Error())
}
err = s.Install()
if err != nil {
fmt.Fprintf(os.Stdout, "安装失败: %s\n", err.Error())
return
}
fmt.Fprintln(os.Stdout, "安装完成,正在启动……")
_ = s.Start()
} else {
fmt.Fprintln(os.Stdout, "正在卸载系统服务……")
_ = s.Stop()
_ = s.Uninstall()
fmt.Fprintln(os.Stdout, "系统服务已删除")
}
}