package daemon import ( "fmt" "log" "os" "text/template" "github.com/gravitl/netmaker/netclient/ncutils" ) const MAC_SERVICE_NAME = "com.gravitl.netclient" func CreateAndRunMacDaemon() error { _, err := os.Stat("~/Library/LaunchAgents") if os.IsNotExist(err) { os.Mkdir("~/Library/LaunchAgents", 0744) } err = CreateMacService(MAC_SERVICE_NAME) if err != nil { return err } _, err = ncutils.RunCmd("launchctl load ~/Library/LaunchAgents/"+MAC_SERVICE_NAME+".plist", true) return err } func CleanupMac() { //StopWindowsDaemon() //RemoveWindowsDaemon() //os.RemoveAll(ncutils.GetNetclientPath()) log.Println("TODO: Not implemented yet") } func CreateMacService(servicename string) error { tdata := MacTemplateData{ Label: servicename, Program: "/etc/netclient/netclient", KeepAlive: true, RunAtLoad: true, } fileLoc := fmt.Sprintf("%s/Library/LaunchAgents/%s.plist", os.Getenv("HOME"), tdata.Label) launchdFile, err := os.Open(fileLoc) if err != nil { return err } launchdTemplate := template.Must(template.New("launchdTemplate").Parse(MacTemplate())) return launchdTemplate.Execute(launchdFile, tdata) } func MacTemplate() string { return ` Label{{.Label}} Program{{.Program}} StandardOutPath/tmp/{{.Label}}.out.log StandardErrorPath/tmp/{{.Label}}.err.log KeepAlive<{{.KeepAlive}}/> RunAtLoad<{{.RunAtLoad}}/> StartCalendarInterval Minute */1 ` } type MacTemplateData struct { Label string Program string KeepAlive bool RunAtLoad bool }