Replace std out/err path to an existing directory (#307)

This commit is contained in:
Ali Yousuf
2022-04-27 17:08:57 +05:00
committed by GitHub
parent 5c08916379
commit ed46af2312
+35 -15
View File
@@ -27,12 +27,15 @@ type darwinSystem struct{}
func (darwinSystem) String() string { func (darwinSystem) String() string {
return version return version
} }
func (darwinSystem) Detect() bool { func (darwinSystem) Detect() bool {
return true return true
} }
func (darwinSystem) Interactive() bool { func (darwinSystem) Interactive() bool {
return interactive return interactive
} }
func (darwinSystem) New(i Interface, c *Config) (Service, error) { func (darwinSystem) New(i Interface, c *Config) (Service, error) {
s := &darwinLaunchdService{ s := &darwinLaunchdService{
i: i, i: i,
@@ -106,6 +109,18 @@ func (s *darwinLaunchdService) getServiceFilePath() (string, error) {
return "/Library/LaunchDaemons/" + s.Name + ".plist", nil return "/Library/LaunchDaemons/" + s.Name + ".plist", nil
} }
func (s *darwinLaunchdService) getLogPath(logType string) (string, error) {
if s.userService {
homeDir, err := s.getHomeDir()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/.%s.%s.log", homeDir, s.Name, logType), nil
}
return fmt.Sprintf("%s/%s.%s.log", "/var/log", s.Name, logType), nil
}
func (s *darwinLaunchdService) template() *template.Template { func (s *darwinLaunchdService) template() *template.Template {
functions := template.FuncMap{ functions := template.FuncMap{
"bool": func(v bool) string { "bool": func(v bool) string {
@@ -154,20 +169,25 @@ func (s *darwinLaunchdService) Install() error {
return err return err
} }
stdOutPath, _ := s.getLogPath("out")
stdErrPath, _ := s.getLogPath("err")
var to = &struct { var to = &struct {
*Config *Config
Path string Path string
KeepAlive, RunAtLoad bool KeepAlive, RunAtLoad bool
SessionCreate bool SessionCreate bool
StandardOut bool StandardOutPath string
StandardError bool StandardErrorPath string
}{ }{
Config: s.Config, Config: s.Config,
Path: path, Path: path,
KeepAlive: s.Option.bool(optionKeepAlive, optionKeepAliveDefault), KeepAlive: s.Option.bool(optionKeepAlive, optionKeepAliveDefault),
RunAtLoad: s.Option.bool(optionRunAtLoad, optionRunAtLoadDefault), RunAtLoad: s.Option.bool(optionRunAtLoad, optionRunAtLoadDefault),
SessionCreate: s.Option.bool(optionSessionCreate, optionSessionCreateDefault), SessionCreate: s.Option.bool(optionSessionCreate, optionSessionCreateDefault),
StandardOutPath: stdOutPath,
StandardErrorPath: stdErrPath,
} }
return s.template().Execute(f, to) return s.template().Execute(f, to)
@@ -216,6 +236,7 @@ func (s *darwinLaunchdService) Start() error {
} }
return run("launchctl", "load", confPath) return run("launchctl", "load", confPath)
} }
func (s *darwinLaunchdService) Stop() error { func (s *darwinLaunchdService) Stop() error {
confPath, err := s.getServiceFilePath() confPath, err := s.getServiceFilePath()
if err != nil { if err != nil {
@@ -223,6 +244,7 @@ func (s *darwinLaunchdService) Stop() error {
} }
return run("launchctl", "unload", confPath) return run("launchctl", "unload", confPath)
} }
func (s *darwinLaunchdService) Restart() error { func (s *darwinLaunchdService) Restart() error {
err := s.Stop() err := s.Stop()
if err != nil { if err != nil {
@@ -233,9 +255,7 @@ func (s *darwinLaunchdService) Restart() error {
} }
func (s *darwinLaunchdService) Run() error { func (s *darwinLaunchdService) Run() error {
var err error err := s.i.Start(s)
err = s.i.Start(s)
if err != nil { if err != nil {
return err return err
} }
@@ -255,6 +275,7 @@ func (s *darwinLaunchdService) Logger(errs chan<- error) (Logger, error) {
} }
return s.SystemLogger(errs) return s.SystemLogger(errs)
} }
func (s *darwinLaunchdService) SystemLogger(errs chan<- error) (Logger, error) { func (s *darwinLaunchdService) SystemLogger(errs chan<- error) (Logger, error) {
return newSysLogger(s.Name, errs) return newSysLogger(s.Name, errs)
} }
@@ -288,11 +309,10 @@ var launchdConfig = `<?xml version='1.0' encoding='UTF-8'?>
<key>Disabled</key> <key>Disabled</key>
<false/> <false/>
<key>StandardOutPath</key> {{if .StandardOutPath}}<key>StandardOutPath</key>
<string>/usr/local/var/log/{{html .Name}}.out.log</string> <string>{{html .StandardOutPath}}</string>{{end}}
<key>StandardErrorPath</key> {{if .StandardErrorPath}}<key>StandardErrorPath</key>
<string>/usr/local/var/log/{{html .Name}}.err.log</string> <string>{{html .StandardErrorPath}}</string>{{end}}
</dict> </dict>
</plist> </plist>
` `