Skip to content

Commit

Permalink
Fix inconsistent "status" sub-command output
Browse files Browse the repository at this point in the history
Fixes #110
  • Loading branch information
rs committed Apr 13, 2020
1 parent 1a4ea16 commit 5fe3fc0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
24 changes: 3 additions & 21 deletions host/service/internal/run_unix.go
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io"
"os/exec"
"strings"
"syscall"
Expand All @@ -21,29 +20,12 @@ func RunOutput(command string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, command, args...)
stdoutPipe, err := cmd.StdoutPipe()
stdout, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("%s: cannot connect stdout: %w", command, err)
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return "", fmt.Errorf("%s: cannot connect stderr: %w", command, err)
}
var stdout, stderr bytes.Buffer
go copy(&stdout, stdoutPipe)
go copy(&stderr, stderrPipe)
if err := cmd.Start(); err != nil {
return "", fmt.Errorf("%q failed: %w", command, err)
}
if err := cmd.Wait(); err != nil {
cancel()
return "", fmt.Errorf("%s %s: %w: %s", command, strings.Join(args, " "), err, stderr.String())
return "", fmt.Errorf("%s %s: %w", command, strings.Join(args, " "), err)
}
return strings.TrimSpace(stdout.String()), nil
}

func copy(w io.Writer, r io.ReadCloser) {
_, _ = io.Copy(w, r)
return string(bytes.TrimSpace(stdout)), nil
}

func ExitCode(err error) int {
Expand Down
16 changes: 16 additions & 0 deletions host/service/launchd/service.go
Expand Up @@ -3,6 +3,7 @@
package launchd

import (
"errors"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -31,10 +32,16 @@ func New(c service.Config) (Service, error) {
}

func (s Service) Install() error {
if os.Geteuid() != 0 {
return errors.New("permission denied")
}
return internal.CreateWithTemplate(s.Path, tmpl, 0644, s.Config)
}

func (s Service) Uninstall() error {
if os.Geteuid() != 0 {
return errors.New("permission denied")
}
_ = s.Stop()
if err := os.Remove(s.Path); err != nil {
if os.IsNotExist(err) {
Expand All @@ -45,6 +52,9 @@ func (s Service) Uninstall() error {
}

func (s Service) Status() (service.Status, error) {
if os.Geteuid() != 0 {
return service.StatusUnknown, errors.New("permission denied")
}
out, err := launchctl("list", s.Name)
if err != nil && internal.ExitCode(err) == -1 {
if !strings.Contains(err.Error(), "failed with StandardError") {
Expand All @@ -66,11 +76,17 @@ func (s Service) Status() (service.Status, error) {
}

func (s Service) Start() error {
if os.Geteuid() != 0 {
return errors.New("permission denied")
}
_, err := launchctl("load", s.Path)
return err
}

func (s Service) Stop() error {
if os.Geteuid() != 0 {
return errors.New("permission denied")
}
_, err := launchctl("unload", s.Path)
return err
}
Expand Down
2 changes: 2 additions & 0 deletions host/service/systemd/service.go
Expand Up @@ -4,6 +4,7 @@ package systemd

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -52,6 +53,7 @@ func (s Service) Uninstall() error {

func (s Service) Status() (service.Status, error) {
out, err := internal.RunOutput("systemctl", "is-active", s.Name)
fmt.Println(out, err)
if internal.ExitCode(err) == 0 && err != nil {
return service.StatusUnknown, err
}
Expand Down

0 comments on commit 5fe3fc0

Please sign in to comment.