/
service.go
133 lines (108 loc) · 2.23 KB
/
service.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
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"os"
"syscall"
"github.com/kardianos/service"
)
type program struct{}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
run(false)
}
func (p *program) Stop(s service.Service) error {
shutdownSignal <- syscall.SIGINT
return nil
}
func manageService(cmd string) {
wd, err := os.Getwd()
if err != nil {
fmt.Println("Error when trying to get the working directory:", err.Error())
os.Exit(1)
}
serviceConfig := &service.Config{
Name: "lincast",
DisplayName: "LinCast",
Description: "LinCast, your podcasts player",
WorkingDirectory: wd,
}
p := &program{}
s, err := service.New(p, serviceConfig)
if err != nil {
fmt.Println("Error when trying to create a new instance of Service:", err.Error())
os.Exit(1)
}
switch cmd {
case "install":
{
err := s.Install()
if err != nil {
fmt.Println("Error installing the service:", err.Error())
os.Exit(1)
}
fmt.Println("Service installed!")
}
case "uninstall":
{
err := s.Uninstall()
if err != nil {
fmt.Println("Error uninstalling the service:", err.Error())
os.Exit(1)
}
fmt.Println("Service uninstalled!")
}
case "start":
{
err := s.Start()
if err != nil {
fmt.Println("Error starting the service:", err.Error())
os.Exit(1)
}
fmt.Println("Service started!")
}
case "stop":
{
err := s.Stop()
if err != nil {
fmt.Println("Error stopping the service:", err.Error())
os.Exit(1)
}
fmt.Println("Service stopped!")
}
case "restart":
{
err := s.Restart()
if err != nil {
fmt.Println("Error restarting the service:", err.Error())
os.Exit(1)
}
fmt.Println("Service restarted!")
}
case "status":
{
status, err := s.Status()
if err != nil {
fmt.Println("Error getting the status of the service:", err.Error())
os.Exit(1)
}
fmt.Print("Status of the service: ")
switch status {
case service.StatusRunning:
fmt.Println("running")
case service.StatusStopped:
fmt.Println("stopped")
case service.StatusUnknown:
fmt.Println("unknown")
}
}
default:
{
fmt.Printf("Unknown command '%s'\n", cmd)
os.Exit(1)
}
}
os.Exit(0)
}