go-serviceman/installer/install_darwin.go

71 lines
1.5 KiB
Go
Raw Normal View History

2019-07-01 08:44:48 +00:00
package installer
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"git.rootprojects.org/root/go-serviceman/installer/static"
)
func install(c *Config) error {
// Darwin-specific config options
if c.PrivilegedPorts {
if !c.System {
return fmt.Errorf("You must use root-owned LaunchDaemons (not user-owned LaunchAgents) to use priveleged ports on OS X")
}
}
plistDir := "/Library/LaunchDaemons/"
if !c.System {
plistDir = filepath.Join(c.home, "Library/LaunchAgents")
}
// Check paths first
2019-07-02 06:25:16 +00:00
err := os.MkdirAll(filepath.Dir(plistDir), 0755)
2019-07-01 08:44:48 +00:00
if nil != err {
return err
}
// Create service file from template
b, err := static.ReadFile("dist/Library/LaunchDaemons/_rdns_.plist.tmpl")
if err != nil {
return err
}
s := string(b)
rw := &bytes.Buffer{}
// not sure what the template name does, but whatever
tmpl, err := template.New("service").Parse(s)
if err != nil {
return err
}
err = tmpl.Execute(rw, c)
if nil != err {
return err
}
// Write the file out
// TODO rdns
2019-07-02 06:02:09 +00:00
plistName := c.ReverseDNS + ".plist"
2019-07-01 08:44:48 +00:00
plistPath := filepath.Join(plistDir, plistName)
if err := ioutil.WriteFile(plistPath, rw.Bytes(), 0644); err != nil {
2019-07-02 06:25:16 +00:00
2019-07-01 08:44:48 +00:00
return fmt.Errorf("ioutil.WriteFile error: %v", err)
}
fmt.Printf("Installed. To start '%s' run the following:\n", c.Name)
// TODO template config file
2019-07-02 06:25:16 +00:00
if "" != c.home {
plistPath = strings.Replace(plistPath, c.home, "~", 1)
}
sudo := ""
if c.System {
sudo = "sudo "
}
fmt.Printf("\t%slaunchctl load -w %s\n", sudo, plistPath)
2019-07-01 08:44:48 +00:00
return nil
}