diff --git a/internal/command/command.go b/internal/command/command.go index e72e3ee..d45af71 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -11,9 +11,9 @@ import ( "github.com/apex/log" "github.com/blacktop/lporg/internal/database" - "github.com/blacktop/lporg/internal/database/utils" "github.com/blacktop/lporg/internal/desktop" "github.com/blacktop/lporg/internal/dock" + "github.com/blacktop/lporg/internal/utils" "github.com/glebarez/sqlite" "github.com/pkg/errors" "gopkg.in/yaml.v3" diff --git a/internal/command/utils.go b/internal/command/utils.go index c297d35..3f4ecfa 100644 --- a/internal/command/utils.go +++ b/internal/command/utils.go @@ -2,15 +2,13 @@ package command import ( "context" - "fmt" "os" - "os/exec" "os/user" "path/filepath" "time" "github.com/apex/log" - "github.com/blacktop/lporg/internal/database/utils" + "github.com/blacktop/lporg/internal/utils" "github.com/pkg/errors" ) @@ -71,37 +69,11 @@ var PorgASCIIArt = ` ` -// RunCommand runs cmd on file -func RunCommand(ctx context.Context, cmd string, args ...string) (string, error) { - - var c *exec.Cmd - - if ctx != nil { - c = exec.CommandContext(ctx, cmd, args...) - } else { - c = exec.Command(cmd, args...) - } - - output, err := c.Output() - if err != nil { - return string(output), err - } - - // check for exec context timeout - if ctx != nil { - if ctx.Err() == context.DeadlineExceeded { - return "", fmt.Errorf("command %s timed out", cmd) - } - } - - return string(output), nil -} - func restartDock() error { ctx := context.Background() utils.Indent(log.Info)("restarting Dock") - if _, err := RunCommand(ctx, "killall", "Dock"); err != nil { + if _, err := utils.RunCommand(ctx, "killall", "Dock"); err != nil { return errors.Wrap(err, "killing Dock process failed") } diff --git a/internal/database/config.go b/internal/database/config.go index fe847d3..f76c513 100644 --- a/internal/database/config.go +++ b/internal/database/config.go @@ -4,7 +4,7 @@ import ( "os" "github.com/apex/log" - "github.com/blacktop/lporg/internal/database/utils" + "github.com/blacktop/lporg/internal/utils" yaml "gopkg.in/yaml.v3" ) diff --git a/internal/database/database.go b/internal/database/database.go index 1d28f80..ae47015 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -8,7 +8,7 @@ import ( "sort" "github.com/apex/log" - "github.com/blacktop/lporg/internal/database/utils" + "github.com/blacktop/lporg/internal/utils" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" "gorm.io/gorm" diff --git a/internal/dock/dock.go b/internal/dock/dock.go index 0cb6ae3..47b45cf 100644 --- a/internal/dock/dock.go +++ b/internal/dock/dock.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "github.com/blacktop/lporg/internal/command" + "github.com/blacktop/lporg/internal/utils" "howett.net/plist" ) @@ -229,7 +229,7 @@ func (p *Plist) Save() error { } func (p *Plist) kickstart() error { - out, err := command.RunCommand(context.Background(), "/bin/launchctl", "kickstart", "-k", fmt.Sprintf("gui/%d/com.apple.Dock.agent", os.Getuid())) + out, err := utils.RunCommand(context.Background(), "/bin/launchctl", "kickstart", "-k", fmt.Sprintf("gui/%d/com.apple.Dock.agent", os.Getuid())) if err != nil { return fmt.Errorf("failed to kickstart dock: %v", err) } diff --git a/internal/database/utils/utils.go b/internal/utils/utils.go similarity index 71% rename from internal/database/utils/utils.go rename to internal/utils/utils.go index a851415..370e832 100644 --- a/internal/database/utils/utils.go +++ b/internal/utils/utils.go @@ -1,6 +1,10 @@ package utils import ( + "context" + "fmt" + "os/exec" + "github.com/apex/log" "github.com/apex/log/handlers/cli" ) @@ -64,3 +68,29 @@ func checkError(err error) { log.WithError(err).Fatal("failed") } } + +// RunCommand runs cmd on file +func RunCommand(ctx context.Context, cmd string, args ...string) (string, error) { + + var c *exec.Cmd + + if ctx != nil { + c = exec.CommandContext(ctx, cmd, args...) + } else { + c = exec.Command(cmd, args...) + } + + output, err := c.Output() + if err != nil { + return string(output), err + } + + // check for exec context timeout + if ctx != nil { + if ctx.Err() == context.DeadlineExceeded { + return "", fmt.Errorf("command %s timed out", cmd) + } + } + + return string(output), nil +}