This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemd.go
More file actions
106 lines (84 loc) · 1.84 KB
/
Copy pathsystemd.go
File metadata and controls
106 lines (84 loc) · 1.84 KB
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
// +build linux
package systemservice
import (
"bytes"
"os"
"path/filepath"
"strings"
"text/template"
)
func runSystemCtlCommand(cmd string, label string) (out string, err error) {
args := strings.Split(cmd, " ")
if !isRoot() {
args = append(args, "--user")
}
if label != "" {
args = append(args, label)
}
logger.Log("running command: systemctl ", strings.Join(args, " "))
return runCommand("systemctl", args...)
}
/*
unitFile represents a launchctl unitFile file
*/
type unitFile struct {
Label string
Command string
Description string
Documentation string
StdOutPath string
StdErrPath string
User string
}
func newUnitFile(serv *SystemService) unitFile {
cmd := serv.Command
label := cmd.Label
user := username()
if isRoot() {
user = "root"
}
unit := unitFile{
Label: label,
Command: cmd.String(),
Description: cmd.Description,
Documentation: cmd.Documentation,
User: user,
}
return unit
}
func (u *unitFile) Generate() (string, error) {
var tmpl bytes.Buffer
t := template.Must(template.New("unitFile").Parse(unitFileTemplate()))
if err := t.Execute(&tmpl, u); err != nil {
return "", err
}
return tmpl.String(), nil
}
func (u *unitFile) Path() string {
file := u.Label + ".service"
if isRoot() {
return filepath.Join("/etc/systemd/system", file)
}
return filepath.Join(homeDir(), ".config/systemd/user", file)
}
func (u *unitFile) Remove() error {
return os.Remove(u.Path())
}
/*
unitFileTemplate generates the contents of the unitFile file.
*/
func unitFileTemplate() string {
return `[Unit]
After=network.target
Description={{ .Description }}
Documentation={{ .Documentation }}
[Service]
ExecStart={{ .Command }}
Restart=on-failure
Type=simple
StandardOutput=null
[Install]
WantedBy=multi-user.target
`
}
// StandardOutput=file:{{ .LogPath }}