bugfix service name handling

This commit is contained in:
AJ ONeal 2019-07-07 02:14:25 -06:00
parent d610b8cb61
commit 7035ede0b9
3 changed files with 25 additions and 42 deletions

View File

@ -36,13 +36,11 @@ func start(system bool, home string, name string) error {
if nil != err { if nil != err {
return err return err
} }
service = filepath.Join(srvSysPath, service)
} else { } else {
service, err = getOneUserSrv(home, sys, user, name) service, err = getOneUserSrv(home, sys, user, name)
if nil != err { if nil != err {
return err return err
} }
service = filepath.Join(home, srvUserPath, service)
} }
cmds := []Runnable{ cmds := []Runnable{
@ -52,9 +50,10 @@ func start(system bool, home string, name string) error {
Must: false, Must: false,
}, },
Runnable{ Runnable{
Exec: "launchctl", Exec: "launchctl",
Args: []string{"load", "-w", service}, Args: []string{"load", "-w", service},
Must: true, Must: true,
Badwords: []string{"No such file or directory", "service already loaded"},
}, },
} }

View File

@ -35,19 +35,17 @@ func start(system bool, home string, name string) error {
return err return err
} }
var service string // var service string
if system { if system {
service, err = getOneSysSrv(sys, user, name) _, err = getOneSysSrv(sys, user, name)
if nil != err { if nil != err {
return err return err
} }
service = filepath.Join(srvSysPath, service)
} else { } else {
service, err = getOneUserSrv(home, sys, user, name) _, err = getOneUserSrv(home, sys, user, name)
if nil != err { if nil != err {
return err return err
} }
service = filepath.Join(home, srvUserPath, service)
} }
var cmds []Runnable var cmds []Runnable

View File

@ -99,10 +99,10 @@ func getSystemSrvs() ([]string, error) {
} }
func getUserSrvs(home string) ([]string, error) { func getUserSrvs(home string) ([]string, error) {
dir := filepath.Join(home, srvUserPath) return getSrvs(filepath.Join(home, srvUserPath))
return getSrvs(dir)
} }
// "come.example.foo.plist" matches "foo"
func filterMatchingSrvs(plists []string, name string) []string { func filterMatchingSrvs(plists []string, name string) []string {
filtered := []string{} filtered := []string{}
@ -148,59 +148,45 @@ func getExactSrvMatch(srvs []string, name string) string {
} }
func getOneSysSrv(sys []string, user []string, name string) (string, error) { func getOneSysSrv(sys []string, user []string, name string) (string, error) {
service := getExactSrvMatch(user, name) if service := getExactSrvMatch(user, name); "" != service {
if "" != service { return filepath.Join(srvSysPath, service), nil
return service, nil
} }
var errstr string
// system service was wanted
n := len(sys) n := len(sys)
switch { switch {
case 0 == n: case 0 == n:
errstr += fmt.Sprintf("Didn't find user service matching %q\n", name) errstr := fmt.Sprintf("Didn't find user service matching %q\n", name)
if 0 != len(user) { if 0 != len(user) {
errstr += fmt.Sprintf("Did you intend to run a user service instead?\n\t%s\n", strings.Join(user, "\n\t")) errstr += fmt.Sprintf("Did you intend to run a user service instead?\n\t%s\n", strings.Join(user, "\n\t"))
} }
case n > 1:
errstr += fmt.Sprintf("Found more than one matching service:\n\t%s\n", strings.Join(sys, "\n\t"))
default:
service = filepath.Join(srvSysPath, sys[0])
}
if "" != errstr {
return "", fmt.Errorf(errstr) return "", fmt.Errorf(errstr)
case n > 1:
errstr := fmt.Sprintf("Found more than one matching service:\n\t%s\n", strings.Join(sys, "\n\t"))
return "", fmt.Errorf(errstr)
default:
return filepath.Join(srvSysPath, sys[0]), nil
} }
return service, nil
} }
func getOneUserSrv(home string, sys []string, user []string, name string) (string, error) { func getOneUserSrv(home string, sys []string, user []string, name string) (string, error) {
service := getExactSrvMatch(user, name) if service := getExactSrvMatch(user, name); "" != service {
if "" != service { return filepath.Join(home, srvUserPath, service), nil
return service, nil
} }
var errstr string
// user service was wanted
n := len(user) n := len(user)
switch { switch {
case 0 == n: case 0 == n:
errstr += fmt.Sprintf("Didn't find user service matching %q\n", name) errstr := fmt.Sprintf("Didn't find user service matching %q\n", name)
if 0 != len(sys) { if 0 != len(sys) {
errstr += fmt.Sprintf("Did you intend to run a system service instead?\n\t%s\n", strings.Join(sys, "\n\t")) errstr += fmt.Sprintf("Did you intend to run a system service instead?\n\t%s\n", strings.Join(sys, "\n\t"))
} }
case n > 1:
errstr += fmt.Sprintf("Found more than one matching service:\n\t%s\n", strings.Join(user, "\n\t"))
default:
service = filepath.Join(home, srvUserPath, user[0]+srvExt)
}
if "" != errstr {
return "", fmt.Errorf(errstr) return "", fmt.Errorf(errstr)
case n > 1:
errstr := fmt.Sprintf("Found more than one matching service:\n\t%s\n", strings.Join(user, "\n\t"))
return "", fmt.Errorf(errstr)
default:
return filepath.Join(home, srvUserPath, user[0]), nil
} }
return service, nil
} }
func adjustPrivs(system bool, cmds []Runnable) []Runnable { func adjustPrivs(system bool, cmds []Runnable) []Runnable {