-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.go
54 lines (45 loc) · 1.29 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
package launchd
import (
"fmt"
"time"
"github.com/brasic/launchd/state"
"github.com/fatih/color"
)
var bold = color.New(color.FgCyan, color.Bold)
// Install sets up a new service by writing a plist file and telling launchd about it.
// It also starts the service and waits for it to come up if RunAtLoad is true.
func (s *Service) Install() (err error) {
if s.InstallState().Is(state.Installed) {
// Nothing to do
return nil
}
content, err := s.RenderPlist()
if err != nil {
return err
}
if err = s.WritePlist(content); err != nil {
return err
}
if _, err = s.Bootstrap(); err != nil {
return err
}
if s.RunAtLoad && s.waitUntilRunning(5*time.Second) {
return nil
}
return fmt.Errorf("Timed out waiting for service to boot")
}
func (s *Service) waitUntilRunning(timeout time.Duration) bool {
_, timedOut := s.PollUntil(state.Running, timeout)
fmt.Println(finalStatus(timedOut, s.UserSpecifier()))
return !timedOut
}
func finalStatus(timedOut bool, specifier string) string {
cmd := bold.Sprintf("launchctl print %s", specifier)
var statusLine string
if timedOut {
statusLine = "timed out waiting for service to come up. Something is probably wrong.\nRun `%s` for more detail."
} else {
statusLine = "done!\nRun `%s` for more detail."
}
return fmt.Sprintf(statusLine, cmd)
}