1515package config
1616
1717import (
18+ // we need this for the ArduinoCreateAgent.plist in this package
19+ _ "embed"
1820 "fmt"
1921 "os"
2022 "os/exec"
23+ "text/template"
2124
2225 "github.com/arduino/go-paths-helper"
2326 log "github.com/sirupsen/logrus"
2427)
2528
29+ //go:embed ArduinoCreateAgent.plist
30+ var launchdAgentDefinition []byte
31+
2632// getLaunchdAgentPath will return the path of the launchd agent default path
2733func getLaunchdAgentPath () * paths.Path {
2834 return GetDefaultHomeDir ().Join ("Library" , "LaunchAgents" , "ArduinoCreateAgent.plist" )
@@ -32,52 +38,43 @@ func getLaunchdAgentPath() *paths.Path {
3238// it will return nil in case of success,
3339// it will error if the file is already there or in any other case
3440func WritePlistFile () error {
35- src , err := os .Executable ()
36- if err != nil {
37- return err
38- }
3941
4042 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- `
6343 if launchdAgentPath .Exist () {
64- // TODO check for differences in files(?)
6544 // we already have an existing launchd plist file, so we don't have to do anything
6645 return fmt .Errorf ("the autostart file %s already exists" , launchdAgentPath )
6746 }
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 )
7065}
7166
7267// LoadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
7368func LoadLaunchdAgent () error {
69+ // https://www.launchd.info/
7470 oscmd := exec .Command ("launchctl" , "load" , getLaunchdAgentPath ().String ())
7571 err := oscmd .Run ()
7672 return err
7773}
7874
7975// UnloadLaunchdAgent will use launchctl to load the agent, will return an error if something goes wrong
8076func UnloadLaunchdAgent () error {
77+ // https://www.launchd.info/
8178 oscmd := exec .Command ("launchctl" , "unload" , getLaunchdAgentPath ().String ())
8279 err := oscmd .Run ()
8380 return err
@@ -93,6 +90,3 @@ func RemovePlistFile() error {
9390 }
9491 return nil
9592}
96-
97- // TODO win/linux ?
98- // TODO read the doc about launchd https://www.launchd.info/
0 commit comments