Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/command/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func newBackgroundCommand() *backgroundCommand {

b.cmd.AddCommand(newBackgroundInstallCommand().cmd)
b.cmd.AddCommand(newBackgroundUninstallCommand().cmd)
b.cmd.AddCommand(newBackgroundStatusCommand().cmd)
b.cmd.AddCommand(newBackgroundRunCommand().cmd)

return b
Expand Down
62 changes: 62 additions & 0 deletions internal/command/background_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package command

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/basecamp/once/internal/service"
)

type backgroundStatusCommand struct {
cmd *cobra.Command
}

func newBackgroundStatusCommand() *backgroundStatusCommand {
b := &backgroundStatusCommand{}
b.cmd = &cobra.Command{
Use: "status",
Short: "Show whether the background tasks service is installed and running",
Args: cobra.NoArgs,
RunE: b.run,
}
return b
}

// Private

func (b *backgroundStatusCommand) run(cmd *cobra.Command, args []string) error {
ctx := context.Background()
namespace := namespaceFlag(cmd)

svc, err := service.New()
if err != nil {
return err
}

serviceName := namespace + backgroundServiceSuffix

switch {
case !svc.IsInstalled(serviceName):
fmt.Printf("Service %s is not installed\n", svc.ServiceName(serviceName))
case svc.IsRunning(ctx, serviceName):
fmt.Printf("Service %s is installed and running\n", svc.ServiceName(serviceName))
return nil
default:
fmt.Printf("Service %s is installed but not running\n", svc.ServiceName(serviceName))
}

cmd.SilenceErrors = true
return notRunningError{}
}

type notRunningError struct{}

func (notRunningError) Error() string {
return "service is not running"
}

func (notRunningError) ExitCode() int {
return 3
}
4 changes: 4 additions & 0 deletions internal/service/launchd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (l *Launchd) IsInstalled(name string) bool {
return err == nil
}

func (l *Launchd) IsRunning(ctx context.Context, name string) bool {
return exec.CommandContext(ctx, "launchctl", "print", "system/"+l.label(name)).Run() == nil
Comment on lines +49 to +50
}

func (l *Launchd) Install(ctx context.Context, name, execPath, namespace string) error {
label := l.label(name)
path := l.plistPath(name)
Expand Down
1 change: 1 addition & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type Service interface {
IsInstalled(name string) bool
IsRunning(ctx context.Context, name string) bool
Install(ctx context.Context, name, execPath, namespace string) error
Remove(ctx context.Context, name string) error
ServiceName(name string) string
Expand Down
4 changes: 4 additions & 0 deletions internal/service/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (s *Systemd) IsInstalled(name string) bool {
return err == nil
}

func (s *Systemd) IsRunning(ctx context.Context, name string) bool {
return exec.CommandContext(ctx, "systemctl", "is-active", "--quiet", name).Run() == nil
}

func (s *Systemd) Install(ctx context.Context, name, execPath, namespace string) error {
path := s.unitFilePath(name)

Expand Down
Loading