Skip to content

Commit

Permalink
fix: import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Sep 3, 2023
1 parent 1d88229 commit bc7a870
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 2 additions & 30 deletions internal/command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions internal/dock/dock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/blacktop/lporg/internal/command"
"github.com/blacktop/lporg/internal/utils"
"howett.net/plist"
)

Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions internal/database/utils/utils.go → internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package utils

import (
"context"
"fmt"
"os/exec"

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
)
Expand Down Expand Up @@ -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
}

0 comments on commit bc7a870

Please sign in to comment.