15
15
package config
16
16
17
17
import (
18
+ // we need this for the ArduinoCreateAgent.plist in this package
19
+ _ "embed"
18
20
"fmt"
19
21
"os"
20
22
"os/exec"
23
+ "text/template"
21
24
22
25
"github.com/arduino/go-paths-helper"
23
26
log "github.com/sirupsen/logrus"
24
27
)
25
28
29
+ //go:embed ArduinoCreateAgent.plist
30
+ var launchdAgentDefinition []byte
31
+
26
32
// getLaunchdAgentPath will return the path of the launchd agent default path
27
33
func getLaunchdAgentPath () * paths.Path {
28
34
return GetDefaultHomeDir ().Join ("Library" , "LaunchAgents" , "ArduinoCreateAgent.plist" )
@@ -32,52 +38,43 @@ func getLaunchdAgentPath() *paths.Path {
32
38
// it will return nil in case of success,
33
39
// it will error if the file is already there or in any other case
34
40
func WritePlistFile () error {
35
- src , err := os .Executable ()
36
- if err != nil {
37
- return err
38
- }
39
41
40
42
launchdAgentPath := getLaunchdAgentPath ()
41
-
42
- // For now this file comes from the installbuilder autogenerated one
43
- launchdAgentDefinition := `<?xml version="1.0" encoding="UTF-8"?>
44
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
45
- <plist version="1.0">
46
- <dict>
47
- <key>KeepAlive</key>
48
- <false/>
49
- <key>Label</key>
50
- <string>ArduinoCreateAgent</string>
51
- <key>ProgramArguments</key>
52
- <array>
53
- <string>` + src + `</string>
54
- </array>
55
- <key>RunAtLoad</key>
56
- <true/>
57
-
58
- <key>AbandonProcessGroup</key>
59
- <true/>
60
- </dict>
61
- </plist>
62
- `
63
43
if launchdAgentPath .Exist () {
64
- // TODO check for differences in files(?)
65
44
// we already have an existing launchd plist file, so we don't have to do anything
66
45
return fmt .Errorf ("the autostart file %s already exists" , launchdAgentPath )
67
46
}
68
- // we need to create a new one
69
- return launchdAgentPath .WriteFile ([]byte (launchdAgentDefinition ))
47
+
48
+ src , err := os .Executable ()
49
+ if err != nil {
50
+ return err
51
+ }
52
+ data := struct {
53
+ Program string
54
+ RunAtLoad bool
55
+ }{
56
+ Program : src ,
57
+ RunAtLoad : false ,
58
+ }
59
+
60
+ t := template .Must (template .New ("launchdConfig" ).Parse (string (launchdAgentDefinition )))
61
+
62
+ // we need to create a new launchd plist file
63
+ plistFile , _ := launchdAgentPath .Create ()
64
+ return t .Execute (plistFile , data )
70
65
}
71
66
72
67
// LoadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
73
68
func LoadLaunchdAgent () error {
69
+ // https://www.launchd.info/
74
70
oscmd := exec .Command ("launchctl" , "load" , getLaunchdAgentPath ().String ())
75
71
err := oscmd .Run ()
76
72
return err
77
73
}
78
74
79
75
// UnloadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
80
76
func UnloadLaunchdAgent () error {
77
+ // https://www.launchd.info/
81
78
oscmd := exec .Command ("launchctl" , "unload" , getLaunchdAgentPath ().String ())
82
79
err := oscmd .Run ()
83
80
return err
@@ -93,6 +90,3 @@ func RemovePlistFile() error {
93
90
}
94
91
return nil
95
92
}
96
-
97
- // TODO win/linux ?
98
- // TODO read the doc about launchd https://www.launchd.info/
0 commit comments