Skip to content

Commit

Permalink
fix: build command to start and wait
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmedeski committed Aug 23, 2024
1 parent 05c428d commit 95793f8
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions shell/shell.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package shell

import (
"bytes"
"os"
"os/exec"
"strings"

"github.com/joshmedeski/sesh/execwrap"
Expand All @@ -19,11 +22,29 @@ func NewShell(exec execwrap.Exec) Shell {
return &RealShell{exec}
}

func (c *RealShell) Cmd(cmd string, arg ...string) (string, error) {
command := c.exec.Command(cmd, arg...)
output, err := command.CombinedOutput()
trimmedOutput := strings.TrimSuffix(string(output), "\n")
return trimmedOutput, err
func (c *RealShell) Cmd(cmd string, args ...string) (string, error) {
foundCmd, err := c.exec.LookPath(cmd)
if err != nil {
return "", err
}
var stdout, stderr bytes.Buffer
command := exec.Command(foundCmd, args...)
command.Stdin = os.Stdin
command.Stdout = &stdout
command.Stderr = os.Stderr
command.Stderr = &stderr
if err := command.Start(); err != nil {
return "", err
}
if err := command.Wait(); err != nil {
errString := strings.TrimSpace(stderr.String())
if strings.HasPrefix(errString, "no server running on") {
return "", nil
}
return "", err
}
trimmedOutput := strings.TrimSuffix(string(stdout.String()), "\n")
return trimmedOutput, nil
}

func (c *RealShell) ListCmd(cmd string, arg ...string) ([]string, error) {
Expand Down

0 comments on commit 95793f8

Please sign in to comment.