From 35d89699533ec6d6f4db8e12507556df1e79c477 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Thu, 24 Nov 2022 19:29:20 +1030 Subject: [PATCH] Add working directory --- templates/basic.service | 1 + unitard.go | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/templates/basic.service b/templates/basic.service index 61ad18d..079f776 100644 --- a/templates/basic.service +++ b/templates/basic.service @@ -4,6 +4,7 @@ Description={{ .description }} [Service] +WorkingDirectory={{ .workingDirectory }} ExecStart={{ .execStart }} [Install] diff --git a/unitard.go b/unitard.go index 7593212..ff257bc 100644 --- a/unitard.go +++ b/unitard.go @@ -8,6 +8,7 @@ import ( "io" "os" "os/exec" + "path" "regexp" "strings" "text/template" @@ -17,8 +18,9 @@ import ( var fs embed.FS type Unit struct { - name string - binary string + name string + binary string + binaryPath string systemCtlPath string // path to systemctl command unitFilePath string @@ -37,9 +39,11 @@ func NewUnit(unitName string, unitOpts ...UnitOpts) (Unit, error) { if !ok { return Unit{}, fmt.Errorf("sorry, name '%s' is not valid", unitName) } + path, binPath := binaryInfo() u := Unit{ - name: unitName, - binary: binaryName(), + name: unitName, + binary: binPath, + binaryPath: path, } if len(unitOpts) > 0 { @@ -91,8 +95,9 @@ func (u Unit) writeTemplate(f io.Writer) error { } data := map[string]string{ - "description": u.name, - "execStart": u.binary, + "description": u.name, + "execStart": u.binary, + "workingDirectory": u.binaryPath, } err = t.ExecuteTemplate(f, "basic.service", data) return err @@ -163,14 +168,15 @@ func (u Unit) runExpectZero(command string, args ...string) error { return nil } -// binaryName returns the fully-qualified path to the binary on disk -func binaryName() string { +// binaryName returns the fully-qualified path to the binary and the qualified path of the binary +func binaryInfo() (string, string) { binary, err := os.Executable() if err != nil { panic(err) } + dir, file := path.Split(binary) - return binary + return dir, dir + file } func checkName(name string) bool {