-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_linux.go
116 lines (99 loc) · 3.47 KB
/
manage_linux.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
)
func isServiceAvailable(name string) bool {
home, err := os.UserHomeDir()
if err != nil {
return false
}
return fileOrFolderExists(filepath.Join(home, startupPath, "telltail-"+name+".service"))
}
func manageService(name string, action serviceAction) error {
if !contains(validServices, name) {
return cli.Exit("Invalid service name. Valid values are: "+strings.Join(validServices, ", "), exitServiceNotPresent)
}
if !isServiceAvailable(name) {
// can also occur if user dir is not identifiable
return cli.Exit("This service is unavailable. Install it first before you act upon it.", exitServiceNotPresent)
}
// We've intentionally used the word start/stop over enable/disable because the
// latter in our mind means load up the service and enable it or disable the service and unload it.
// This is not we want. We expect telltail to be intentionally disabled by the user for some period of time, not permanently.
// That's why we'll make sure that telltail loads up on boot by `systemctl enable`-ing the service
cmd := exec.Command("systemctl", "--user", "enable", "tellail-"+name)
cmd.Run()
// config might have changed by us or manually by the user, and this cmd loads that new config
switch action {
case startService:
case restartService:
cmd = exec.Command("systemctl", "--user", "daemon-reload")
cmd.Run()
}
switch action {
case startService:
cmd = exec.Command("systemctl", "--user", "start", "telltail-"+name)
case stopService:
cmd = exec.Command("systemctl", "--user", "stop", "telltail-"+name)
case restartService:
cmd = exec.Command("systemctl", "--user", "restart", "telltail-"+name)
}
cmd.Run()
return nil
}
func editCenterAuthKey() error {
if !isServiceAvailable("center") {
// can also occur if user dir is not identifiable
return cli.Exit("This service is unavailable. Install it first before you act upon it.", exitServiceNotPresent)
}
home, err := os.UserHomeDir()
if err != nil {
return cli.Exit("Cannot determine your home folder", exitCannotDetermineUserHomeDir)
}
fullpath := filepath.Join(home, startupPath, "telltail-center.service.d", "override.conf")
input, err := os.ReadFile(fullpath)
if err != nil {
return cli.Exit("Cannot find the config file", exitFileNotReadable)
}
ableToParseExistingAuthKey := false
lines := strings.Split(string(input), "\n")
startStr := "Environment=TS_AUTHKEY="
for i, line := range lines {
if strings.HasPrefix(line, startStr) {
ableToParseExistingAuthKey = true
existingAuthKey := strings.TrimPrefix(line, startStr)
if existingAuthKey == "" {
fmt.Println("There doesn't seem to be an existing auth key, but we can add one.")
} else {
fmt.Println("Existing auth key is", existingAuthKey)
}
fmt.Print("Enter new key (or hit return to keep the existing one): ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
newKey := scanner.Text()
if newKey == "" {
return nil
} else {
lines[i] = startStr + newKey
}
break
}
}
if !ableToParseExistingAuthKey {
return cli.Exit("Unable to find auth key because the config looks invalid", exitInvalidConfig)
}
output := strings.Join(lines, "\n")
// https://stackoverflow.com/a/18415935/7683365
err = os.WriteFile(fullpath, []byte(output), 0644)
if err != nil {
return cli.Exit("Unable to write to config file", exitFileNotModifiable)
}
manageService("center", restartService)
return nil
}