@@ -17,7 +17,6 @@ package config
17
17
import (
18
18
// we need this for the ArduinoCreateAgent.plist in this package
19
19
_ "embed"
20
- "fmt"
21
20
"os"
22
21
"os/exec"
23
22
"text/template"
@@ -34,18 +33,36 @@ func getLaunchdAgentPath() *paths.Path {
34
33
return GetDefaultHomeDir ().Join ("Library" , "LaunchAgents" , "ArduinoCreateAgent.plist" )
35
34
}
36
35
37
- // WritePlistFile function will write the required plist file to $HOME/Library/LaunchAgents/ArduinoCreateAgent.plist
38
- // it will return nil in case of success,
39
- // it will error if the file is already there or in any other case
40
- func WritePlistFile () error {
41
-
36
+ // InstallPlistFile will handle the process of creating the plist file required for the autostart
37
+ // and loading it using launchd
38
+ func InstallPlistFile () {
42
39
launchdAgentPath := getLaunchdAgentPath ()
43
- if launchdAgentPath .Exist () {
40
+ if ! launchdAgentPath .Exist () {
41
+ err := WritePlistFile (launchdAgentPath )
42
+ if err != nil {
43
+ log .Error (err )
44
+ } 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
+ }
52
+ }
53
+ } else {
44
54
// we already have an existing launchd plist file, so we don't have to do anything
45
- return fmt .Errorf ("the autostart file %s already exists" , launchdAgentPath )
55
+ log .Infof ("the autostart file %s already exists: nothing to do" , launchdAgentPath )
56
+
46
57
}
58
+ }
47
59
60
+ // WritePlistFile function will write the required plist file to launchdAgentPath
61
+ // it will return nil in case of success,
62
+ // it will error in any other case
63
+ func WritePlistFile (launchdAgentPath * paths.Path ) error {
48
64
src , err := os .Executable ()
65
+
49
66
if err != nil {
50
67
return err
51
68
}
@@ -72,6 +89,18 @@ func LoadLaunchdAgent() error {
72
89
return err
73
90
}
74
91
92
+ func UninstallPlistFile () {
93
+ err := UnloadLaunchdAgent ()
94
+ if err != nil {
95
+ log .Error (err )
96
+ } else {
97
+ err = RemovePlistFile ()
98
+ if err != nil {
99
+ log .Error (err )
100
+ }
101
+ }
102
+ }
103
+
75
104
// UnloadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
76
105
func UnloadLaunchdAgent () error {
77
106
// https://www.launchd.info/
@@ -88,5 +117,6 @@ func RemovePlistFile() error {
88
117
log .Infof ("removing: %s" , launchdAgentPath )
89
118
return launchdAgentPath .Remove ()
90
119
}
120
+ log .Infof ("the autostart file %s do not exists: nothing to do" , launchdAgentPath )
91
121
return nil
92
122
}
0 commit comments