1515package config
1616
1717import (
18+ "bytes"
1819 // we need this for the ArduinoCreateAgent.plist in this package
1920 _ "embed"
2021 "os"
@@ -38,33 +39,56 @@ func getLaunchdAgentPath() *paths.Path {
3839func InstallPlistFile () {
3940 launchdAgentPath := getLaunchdAgentPath ()
4041 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
4265 if err != nil {
4366 log .Error (err )
4467 } 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 )
5270 }
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-
5771 }
5872}
5973
6074// writePlistFile function will write the required plist file to launchdAgentPath
6175// it will return nil in case of success,
6276// it will error in any other case
6377func 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 ) {
6488 src , err := os .Executable ()
6589
6690 if err != nil {
67- return err
91+ return nil , err
6892 }
6993 data := struct {
7094 Program string
@@ -76,9 +100,12 @@ func writePlistFile(launchdAgentPath *paths.Path) error {
76100
77101 t := template .Must (template .New ("launchdConfig" ).Parse (string (launchdAgentDefinition )))
78102
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
82109}
83110
84111// loadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
0 commit comments