-
Notifications
You must be signed in to change notification settings - Fork 21
/
install.go
111 lines (90 loc) · 2.7 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
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
package main
import (
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
var (
//go:embed hack/cc.chlc.batt.plist
plistTemplate string
plistPath = "/Library/LaunchDaemons/cc.chlc.batt.plist"
)
func installDaemon() error {
// Get the path to the current executable
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get the path to the current executable: %w", err)
}
exePath, err = filepath.Abs(exePath)
if err != nil {
return fmt.Errorf("failed to get the absolute path to the current executable: %w", err)
}
err = os.Chmod(exePath, 0755)
if err != nil {
return fmt.Errorf("failed to chmod the current executable to 0755: %w", err)
}
logrus.Infof("current executable path: %s", exePath)
tmpl := strings.ReplaceAll(plistTemplate, "/path/to/batt", exePath)
logrus.Infof("writing launch daemon to /Library/LaunchDaemons")
// mkdir -p
err = os.MkdirAll("/Library/LaunchDaemons", 0755)
if err != nil {
return fmt.Errorf("failed to create /Library/LaunchDaemons: %w", err)
}
// warn if the file already exists
_, err = os.Stat(plistPath)
if err == nil {
logrus.Errorf("%s already exists", plistPath)
return fmt.Errorf("%s already exists. This is often caused by an incorrect installation. Did you forget to uninstall batt before installing it again? Please uninstall it first, by running 'sudo batt uninstall'. If you already removed batt, you can solve this problem by 'sudo rm %s'", plistPath, plistPath)
}
err = os.WriteFile(plistPath, []byte(tmpl), 0644)
if err != nil {
return fmt.Errorf("failed to write %s: %w", plistPath, err)
}
// chown root:wheel
err = os.Chown(plistPath, 0, 0)
if err != nil {
return fmt.Errorf("failed to chown %s: %w", plistPath, err)
}
logrus.Infof("starting batt")
// run launchctl load /Library/LaunchDaemons/cc.chlc.batt.plist
err = exec.Command(
"/bin/launchctl",
"load",
plistPath,
).Run()
if err != nil {
return fmt.Errorf("failed to load %s: %w", plistPath, err)
}
return nil
}
func uninstallDaemon() error {
logrus.Infof("stopping batt")
// run launchctl unload /Library/LaunchDaemons/cc.chlc.batt.plist
err := exec.Command(
"/bin/launchctl",
"unload",
plistPath,
).Run()
if err != nil {
return fmt.Errorf("failed to unload %s: %w. Are you root?", plistPath, err)
}
logrus.Infof("removing launch daemon")
// if the file doesn't exist, we don't need to remove it
_, err = os.Stat(plistPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to stat %s: %w", plistPath, err)
}
err = os.Remove(plistPath)
if err != nil {
return fmt.Errorf("failed to remove %s: %w. Are you root?", plistPath, err)
}
return nil
}