-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.go
66 lines (56 loc) · 1.46 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
package main
import (
"encoding/json"
"log"
"os"
"path/filepath"
"golang.org/x/sys/windows/svc/mgr"
)
// Install registers a service using the given config filename
func Install(conf string) {
exe, err := os.Executable()
if err != nil {
log.Println("Error: Couldn't find servicify's path")
log.Println(err)
os.Exit(exitPathNotFound)
}
confPath, err := filepath.Abs(conf)
if err != nil {
log.Println("Error: Couldn't find the absolute path to config file")
log.Println(err)
os.Exit(exitCantFindConfigAbsPath)
}
file, err := os.Open(confPath)
if err != nil {
log.Println("Error: Couldn't open config file")
log.Println(err)
os.Exit(exitCantReadConfig)
}
var c Config
err = json.NewDecoder(file).Decode(&c)
if err != nil {
log.Println("Error: Couldn't decode config file")
log.Println(err)
os.Exit(exitCantReadConfig)
}
mc, err := c.Mold()
if err != nil {
log.Println("Error: Invalid config file")
log.Println(err)
os.Exit(exitInvalidConfigFile)
}
m, err := mgr.Connect()
if err != nil {
log.Println("Error: Couldn't connect to Service Control Manage")
log.Println(err)
os.Exit(exitCantConnectSCM)
}
defer m.Disconnect()
s, err := m.CreateService(c.Name, exe, mc, "-run", confPath)
if err != nil {
log.Println("Error: Couldn't create service")
log.Println(err)
os.Exit(exitCantCreateService)
}
log.Printf("Success: Service{ %s } created\n", s.Name)
}