Skip to content

Commit 7e59d28

Browse files
authored
Revert "refactor: improve tmux command (#59)" (#66)
This reverts commit 960bd75.
1 parent e3e8f10 commit 7e59d28

File tree

5 files changed

+26
-126
lines changed

5 files changed

+26
-126
lines changed

.github/workflows/ci-cd.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ jobs:
2525
go-version: "1.21"
2626
- name: Install deps
2727
run: go install github.com/jstemmer/go-junit-report/v2@latest
28-
- if: startsWith(matrix.os, 'macOS')
29-
run: |
30-
brew update
31-
brew install tmux
3228
- name: Run tests
3329
run: go test -cover -bench=. -benchmem -race -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml
3430
- name: Test Summary

tmux/list.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ func sortSessions(sessions []*TmuxSession) []*TmuxSession {
156156

157157
func List(o Options) ([]*TmuxSession, error) {
158158
format := format()
159-
output, err := command.Run([]string{"list-sessions", "-F", format})
160-
if err != nil {
161-
return nil, err
159+
output, err := tmuxCmd([]string{"list-sessions", "-F", format})
160+
cleanOutput := strings.TrimSpace(output)
161+
if err != nil || strings.HasPrefix(cleanOutput, "no server running on") {
162+
return nil, nil
162163
}
163-
164-
sessionList := output
164+
sessionList := strings.TrimSpace(string(output))
165165
lines := strings.Split(sessionList, "\n")
166166
sessions := processSessions(o, lines)
167167

tmux/list_test.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tmux
22

33
import (
4-
_ "embed"
54
"testing"
65

76
"github.com/stretchr/testify/require"
@@ -87,47 +86,6 @@ func TestProcessSessions(t *testing.T) {
8786
}
8887
}
8988

90-
//go:embed testdata/session_list.txt
91-
var sessionList string
92-
93-
func TestList(t *testing.T) {
94-
testCase := map[string]struct {
95-
MockResponse string
96-
MockError error
97-
Options Options
98-
ExpectedLength int
99-
Error error
100-
}{
101-
"happy path": {
102-
MockResponse: sessionList,
103-
ExpectedLength: 3,
104-
},
105-
"happy path show hidden": {
106-
MockResponse: sessionList,
107-
Options: Options{HideAttached: true},
108-
ExpectedLength: 2,
109-
},
110-
}
111-
112-
for name, tc := range testCase {
113-
t.Run(name, func(t *testing.T) {
114-
command = &Command{
115-
execFunc: func(string, []string) (string, error) {
116-
return tc.MockResponse, tc.MockError
117-
},
118-
}
119-
res, err := List(tc.Options)
120-
require.ErrorIs(t, tc.Error, err)
121-
if err != nil {
122-
return
123-
}
124-
125-
require.Len(t, res, tc.ExpectedLength)
126-
t.Log(res)
127-
})
128-
}
129-
}
130-
13189
func BenchmarkProcessSessions(b *testing.B) {
13290
for n := 0; n < b.N; n++ {
13391
processSessions(Options{}, []string{

tmux/testdata/session_list.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

tmux/tmux.go

Lines changed: 21 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,11 @@ import (
77
"os"
88
"os/exec"
99
"strings"
10-
"sync"
1110

1211
"github.com/joshmedeski/sesh/config"
1312
"github.com/joshmedeski/sesh/dir"
1413
)
1514

16-
var (
17-
command *Command
18-
once sync.Once
19-
)
20-
21-
func init() {
22-
once.Do(func() {
23-
var err error
24-
command, err = NewCommand()
25-
if err != nil {
26-
log.Fatal(err)
27-
}
28-
})
29-
}
30-
31-
type Error struct{ msg string }
32-
33-
func (e Error) Error() string { return e.msg }
34-
35-
var ErrNotRunning = Error{"no server running"}
36-
37-
func executeCommand(command string, args []string) (string, error) {
38-
var stdout, stderr bytes.Buffer
39-
cmd := exec.Command(command, args...)
40-
cmd.Stdin = os.Stdin
41-
cmd.Stdout = &stdout
42-
cmd.Stderr = &stderr
43-
44-
if err := cmd.Start(); err != nil {
45-
return "", err
46-
}
47-
48-
if err := cmd.Wait(); err != nil {
49-
if strings.Contains(stderr.String(), "no server running on") {
50-
return "", ErrNotRunning
51-
}
52-
53-
return "", err
54-
}
55-
56-
out := strings.TrimSpace(stdout.String())
57-
if strings.Contains(out, "no server running on") {
58-
return "", ErrNotRunning
59-
}
60-
61-
return out, nil
62-
}
63-
64-
type Command struct {
65-
cliPath string
66-
execFunc func(string, []string) (string, error)
67-
}
68-
69-
func NewCommand() (c *Command, err error) {
70-
c = new(Command)
71-
72-
c.cliPath, err = exec.LookPath("tmux")
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
c.execFunc = executeCommand
78-
79-
return c, nil
80-
}
81-
82-
func (c *Command) Run(args []string) (string, error) {
83-
return c.execFunc(c.cliPath, args)
84-
}
85-
8615
func GetSession(s string) (TmuxSession, error) {
8716
sessionList, err := List(Options{})
8817
if err != nil {
@@ -112,7 +41,27 @@ func GetSession(s string) (TmuxSession, error) {
11241
}
11342

11443
func tmuxCmd(args []string) (string, error) {
115-
return command.Run(args)
44+
tmux, err := exec.LookPath("tmux")
45+
if err != nil {
46+
return "", err
47+
}
48+
var stdout, stderr bytes.Buffer
49+
cmd := exec.Command(tmux, args...)
50+
cmd.Stdin = os.Stdin
51+
cmd.Stdout = &stdout
52+
cmd.Stderr = os.Stderr
53+
cmd.Stderr = &stderr
54+
if err := cmd.Start(); err != nil {
55+
return "", err
56+
}
57+
if err := cmd.Wait(); err != nil {
58+
errString := strings.TrimSpace(stderr.String())
59+
if strings.HasPrefix(errString, "no server running on") {
60+
return "", nil
61+
}
62+
return "", err
63+
}
64+
return stdout.String(), nil
11665
}
11766

11867
func isAttached() bool {

0 commit comments

Comments
 (0)