@@ -16,6 +16,7 @@ package config
16
16
17
17
import (
18
18
// we need this for the ArduinoCreateAgent.plist in this package
19
+ "bytes"
19
20
_ "embed"
20
21
"os"
21
22
"os/exec"
@@ -38,33 +39,56 @@ func getLaunchdAgentPath() *paths.Path {
38
39
func InstallPlistFile () {
39
40
launchdAgentPath := getLaunchdAgentPath ()
40
41
if ! launchdAgentPath .Exist () {
41
- err := writePlistFile (launchdAgentPath )
42
+ writeLoadExit (launchdAgentPath )
43
+ } else {
44
+ // we already have an existing launchd plist file, so we check if it's updated
45
+ launchAgentContent , _ := launchdAgentPath .ReadFile ()
46
+ launchAgentContentNew , _ := getLaunchdAgentDefinition ()
47
+ if bytes .Equal (launchAgentContent , launchAgentContentNew ) {
48
+ log .Infof ("the autostart file %s already exists: nothing to do" , launchdAgentPath )
49
+ } else {
50
+ log .Infof ("the autostart file %s needs to be updated" , launchdAgentPath )
51
+ removePlistFile ()
52
+ writeLoadExit (launchdAgentPath )
53
+ }
54
+
55
+ }
56
+ }
57
+
58
+ // writeLoadExit function will write the plist file, load it, and then exit, because launchd will start a new instance.
59
+ func writeLoadExit (launchdAgentPath * paths.Path ) {
60
+ err := writePlistFile (launchdAgentPath )
61
+ if err != nil {
62
+ log .Error (err )
63
+ } else {
64
+ err = loadLaunchdAgent () // this will load the agent: basically starting a new instance
42
65
if err != nil {
43
66
log .Error (err )
44
67
} else {
45
- err = loadLaunchdAgent () // this will load the agent: basically starting a new instance
46
- if err != nil {
47
- log .Error (err )
48
- } else {
49
- log .Info ("Quitting, another instance of the agent has been started by launchd" )
50
- os .Exit (0 )
51
- }
68
+ log .Info ("Quitting, another instance of the agent has been started by launchd" )
69
+ os .Exit (0 )
52
70
}
53
- } else {
54
- // we already have an existing launchd plist file, so we don't have to do anything
55
- log .Infof ("the autostart file %s already exists: nothing to do" , launchdAgentPath )
56
-
57
71
}
58
72
}
59
73
60
74
// writePlistFile function will write the required plist file to launchdAgentPath
61
75
// it will return nil in case of success,
62
76
// it will error in any other case
63
77
func writePlistFile (launchdAgentPath * paths.Path ) error {
78
+ definition , err := getLaunchdAgentDefinition ()
79
+ if err != nil {
80
+ return err
81
+ }
82
+ // we need to create a new launchd plist file
83
+ return launchdAgentPath .WriteFile (definition )
84
+ }
85
+
86
+ // getLaunchdAgentDefinition will return the definition of the new LaunchdAgent
87
+ func getLaunchdAgentDefinition () ([]byte , error ) {
64
88
src , err := os .Executable ()
65
89
66
90
if err != nil {
67
- return err
91
+ return nil , err
68
92
}
69
93
data := struct {
70
94
Program string
@@ -76,9 +100,12 @@ func writePlistFile(launchdAgentPath *paths.Path) error {
76
100
77
101
t := template .Must (template .New ("launchdConfig" ).Parse (string (launchdAgentDefinition )))
78
102
79
- // we need to create a new launchd plist file
80
- plistFile , _ := launchdAgentPath .Create ()
81
- return t .Execute (plistFile , data )
103
+ buf := bytes .NewBuffer (nil )
104
+ err = t .Execute (buf , data )
105
+ if err != nil {
106
+ return nil , err
107
+ }
108
+ return buf .Bytes (), nil
82
109
}
83
110
84
111
// loadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
0 commit comments