Skip to content

Commit 70cc527

Browse files
authored
feat: add --command flag to connect command (#12)
Execute a CLI command when you connect to a new session by specifying the `--command` or `-c` command.
1 parent bb3b83f commit 70cc527

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

cmds/choose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func Choose() *cli.Command {
6565
}
6666
choice := strings.TrimSpace(cmdOutput.String())
6767
// TODO: get choice from Session structs array
68-
connect.Connect(choice, false)
68+
connect.Connect(choice, false, "")
6969
return nil
7070
},
7171
}

cmds/connect.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ func Connect() *cli.Command {
1818
Aliases: []string{"s"},
1919
Usage: "Always switch the session (and never attach). This is useful for third-party tools like Raycast.",
2020
},
21+
&cli.StringFlag{
22+
Name: "command",
23+
Aliases: []string{"c"},
24+
Usage: "Execute a command when connecting to a new session. Will be ignored if the session exists.",
25+
},
2126
},
2227
Action: func(cCtx *cli.Context) error {
2328
session := cCtx.Args().First()
2429
alwaysSwitch := cCtx.Bool("switch")
30+
command := cCtx.String("command")
2531
if session == "" {
2632
return cli.Exit("No session provided", 0)
2733
}
28-
connect.Connect(session, alwaysSwitch)
34+
connect.Connect(session, alwaysSwitch, command)
2935
return nil
3036
},
3137
}

connect/connect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"github.com/joshmedeski/sesh/zoxide"
77
)
88

9-
func Connect(choice string, alwaysSwitch bool) error {
9+
func Connect(choice string, alwaysSwitch bool, command string) error {
1010
session := session.Determine(choice)
1111
zoxide.Add(session.Path)
1212
tmux.Connect(tmux.TmuxSession{
1313
Name: session.Name,
1414
Path: session.Path,
15-
}, alwaysSwitch)
15+
}, alwaysSwitch, command)
1616
return nil
1717
}

session/determine.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package session
22

33
import (
4-
"fmt"
5-
"os"
4+
"log"
65
)
76

87
func Determine(choice string) Session {
9-
path, err := DeterminPath(choice)
8+
path, err := DeterminePath(choice)
109
if err != nil {
11-
fmt.Println("Could't determine the session path", err)
12-
os.Exit(1)
10+
log.Fatal("Couldn't determine the session path", err)
1311
}
1412

1513
name := DetermineName(path)
1614
if name == "" {
17-
fmt.Println("Could't determine the session name", err)
18-
os.Exit(1)
15+
log.Fatal("Couldn't determine the session name", err)
1916
}
2017

2118
return Session{

session/path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/joshmedeski/sesh/tmux"
88
)
99

10-
func DeterminPath(choice string) (string, error) {
10+
func DeterminePath(choice string) (string, error) {
1111
fullPath := dir.FullPath(choice)
1212
if path.IsAbs(fullPath) {
1313
return fullPath, nil

tmux/tmux.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package tmux
22

33
import (
44
"bytes"
5-
"fmt"
5+
"log"
66
"os"
77
"os/exec"
88
"strings"
@@ -64,6 +64,14 @@ func switchSession(session string) error {
6464
return nil
6565
}
6666

67+
func runPersistentCommand(session string, command string) error {
68+
finalCmd := []string{"send-keys", "-t", session, command, "Enter"}
69+
if _, err := tmuxCmd(finalCmd); err != nil {
70+
return err
71+
}
72+
return nil
73+
}
74+
6775
func NewSession(s TmuxSession) (string, error) {
6876
out, err := tmuxCmd([]string{"new-session", "-d", "-s", s.Name, "-c", s.Path})
6977
if err != nil {
@@ -72,12 +80,15 @@ func NewSession(s TmuxSession) (string, error) {
7280
return out, nil
7381
}
7482

75-
func Connect(s TmuxSession, alwaysSwitch bool) error {
83+
func Connect(s TmuxSession, alwaysSwitch bool, command string) error {
7684
isSession := IsSession(s.Name)
7785
if !isSession {
7886
_, err := NewSession(s)
7987
if err != nil {
80-
fmt.Println(err)
88+
log.Fatal(err)
89+
}
90+
if command != "" {
91+
runPersistentCommand(s.Name, command)
8192
}
8293
}
8394
isAttached := isAttached()

0 commit comments

Comments
 (0)