Skip to content

Commit

Permalink
Revert "refactor: improve tmux command (#59)" (#66)
Browse files Browse the repository at this point in the history
This reverts commit 960bd75.
  • Loading branch information
joshmedeski committed Feb 4, 2024
1 parent e3e8f10 commit 7e59d28
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 126 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ jobs:
go-version: "1.21"
- name: Install deps
run: go install github.com/jstemmer/go-junit-report/v2@latest
- if: startsWith(matrix.os, 'macOS')
run: |
brew update
brew install tmux
- name: Run tests
run: go test -cover -bench=. -benchmem -race -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml
- name: Test Summary
Expand Down
10 changes: 5 additions & 5 deletions tmux/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ func sortSessions(sessions []*TmuxSession) []*TmuxSession {

func List(o Options) ([]*TmuxSession, error) {
format := format()
output, err := command.Run([]string{"list-sessions", "-F", format})
if err != nil {
return nil, err
output, err := tmuxCmd([]string{"list-sessions", "-F", format})
cleanOutput := strings.TrimSpace(output)
if err != nil || strings.HasPrefix(cleanOutput, "no server running on") {
return nil, nil
}

sessionList := output
sessionList := strings.TrimSpace(string(output))
lines := strings.Split(sessionList, "\n")
sessions := processSessions(o, lines)

Expand Down
42 changes: 0 additions & 42 deletions tmux/list_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tmux

import (
_ "embed"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -87,47 +86,6 @@ func TestProcessSessions(t *testing.T) {
}
}

//go:embed testdata/session_list.txt
var sessionList string

func TestList(t *testing.T) {
testCase := map[string]struct {
MockResponse string
MockError error
Options Options
ExpectedLength int
Error error
}{
"happy path": {
MockResponse: sessionList,
ExpectedLength: 3,
},
"happy path show hidden": {
MockResponse: sessionList,
Options: Options{HideAttached: true},
ExpectedLength: 2,
},
}

for name, tc := range testCase {
t.Run(name, func(t *testing.T) {
command = &Command{
execFunc: func(string, []string) (string, error) {
return tc.MockResponse, tc.MockError
},
}
res, err := List(tc.Options)
require.ErrorIs(t, tc.Error, err)
if err != nil {
return
}

require.Len(t, res, tc.ExpectedLength)
t.Log(res)
})
}
}

func BenchmarkProcessSessions(b *testing.B) {
for n := 0; n < b.N; n++ {
processSessions(Options{}, []string{
Expand Down
3 changes: 0 additions & 3 deletions tmux/testdata/session_list.txt

This file was deleted.

93 changes: 21 additions & 72 deletions tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,11 @@ import (
"os"
"os/exec"
"strings"
"sync"

"github.com/joshmedeski/sesh/config"
"github.com/joshmedeski/sesh/dir"
)

var (
command *Command
once sync.Once
)

func init() {
once.Do(func() {
var err error
command, err = NewCommand()
if err != nil {
log.Fatal(err)
}
})
}

type Error struct{ msg string }

func (e Error) Error() string { return e.msg }

var ErrNotRunning = Error{"no server running"}

func executeCommand(command string, args []string) (string, error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
return "", err
}

if err := cmd.Wait(); err != nil {
if strings.Contains(stderr.String(), "no server running on") {
return "", ErrNotRunning
}

return "", err
}

out := strings.TrimSpace(stdout.String())
if strings.Contains(out, "no server running on") {
return "", ErrNotRunning
}

return out, nil
}

type Command struct {
cliPath string
execFunc func(string, []string) (string, error)
}

func NewCommand() (c *Command, err error) {
c = new(Command)

c.cliPath, err = exec.LookPath("tmux")
if err != nil {
return nil, err
}

c.execFunc = executeCommand

return c, nil
}

func (c *Command) Run(args []string) (string, error) {
return c.execFunc(c.cliPath, args)
}

func GetSession(s string) (TmuxSession, error) {
sessionList, err := List(Options{})
if err != nil {
Expand Down Expand Up @@ -112,7 +41,27 @@ func GetSession(s string) (TmuxSession, error) {
}

func tmuxCmd(args []string) (string, error) {
return command.Run(args)
tmux, err := exec.LookPath("tmux")
if err != nil {
return "", err
}
var stdout, stderr bytes.Buffer
cmd := exec.Command(tmux, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = &stdout
cmd.Stderr = os.Stderr
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
return "", err
}
if err := cmd.Wait(); err != nil {
errString := strings.TrimSpace(stderr.String())
if strings.HasPrefix(errString, "no server running on") {
return "", nil
}
return "", err
}
return stdout.String(), nil
}

func isAttached() bool {
Expand Down

0 comments on commit 7e59d28

Please sign in to comment.