Skip to content

Commit

Permalink
feat: add --command flag to connect command (#12)
Browse files Browse the repository at this point in the history
Execute a CLI command when you connect to a new session by specifying the `--command` or `-c` command.
  • Loading branch information
joebonneau authored Jan 16, 2024
1 parent bb3b83f commit 70cc527
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmds/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Choose() *cli.Command {
}
choice := strings.TrimSpace(cmdOutput.String())
// TODO: get choice from Session structs array
connect.Connect(choice, false)
connect.Connect(choice, false, "")
return nil
},
}
Expand Down
8 changes: 7 additions & 1 deletion cmds/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ func Connect() *cli.Command {
Aliases: []string{"s"},
Usage: "Always switch the session (and never attach). This is useful for third-party tools like Raycast.",
},
&cli.StringFlag{
Name: "command",
Aliases: []string{"c"},
Usage: "Execute a command when connecting to a new session. Will be ignored if the session exists.",
},
},
Action: func(cCtx *cli.Context) error {
session := cCtx.Args().First()
alwaysSwitch := cCtx.Bool("switch")
command := cCtx.String("command")
if session == "" {
return cli.Exit("No session provided", 0)
}
connect.Connect(session, alwaysSwitch)
connect.Connect(session, alwaysSwitch, command)
return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/joshmedeski/sesh/zoxide"
)

func Connect(choice string, alwaysSwitch bool) error {
func Connect(choice string, alwaysSwitch bool, command string) error {
session := session.Determine(choice)
zoxide.Add(session.Path)
tmux.Connect(tmux.TmuxSession{
Name: session.Name,
Path: session.Path,
}, alwaysSwitch)
}, alwaysSwitch, command)
return nil
}
11 changes: 4 additions & 7 deletions session/determine.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package session

import (
"fmt"
"os"
"log"
)

func Determine(choice string) Session {
path, err := DeterminPath(choice)
path, err := DeterminePath(choice)
if err != nil {
fmt.Println("Could't determine the session path", err)
os.Exit(1)
log.Fatal("Couldn't determine the session path", err)
}

name := DetermineName(path)
if name == "" {
fmt.Println("Could't determine the session name", err)
os.Exit(1)
log.Fatal("Couldn't determine the session name", err)
}

return Session{
Expand Down
2 changes: 1 addition & 1 deletion session/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/joshmedeski/sesh/tmux"
)

func DeterminPath(choice string) (string, error) {
func DeterminePath(choice string) (string, error) {
fullPath := dir.FullPath(choice)
if path.IsAbs(fullPath) {
return fullPath, nil
Expand Down
17 changes: 14 additions & 3 deletions tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tmux

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -64,6 +64,14 @@ func switchSession(session string) error {
return nil
}

func runPersistentCommand(session string, command string) error {
finalCmd := []string{"send-keys", "-t", session, command, "Enter"}
if _, err := tmuxCmd(finalCmd); err != nil {
return err
}
return nil
}

func NewSession(s TmuxSession) (string, error) {
out, err := tmuxCmd([]string{"new-session", "-d", "-s", s.Name, "-c", s.Path})
if err != nil {
Expand All @@ -72,12 +80,15 @@ func NewSession(s TmuxSession) (string, error) {
return out, nil
}

func Connect(s TmuxSession, alwaysSwitch bool) error {
func Connect(s TmuxSession, alwaysSwitch bool, command string) error {
isSession := IsSession(s.Name)
if !isSession {
_, err := NewSession(s)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
if command != "" {
runPersistentCommand(s.Name, command)
}
}
isAttached := isAttached()
Expand Down

0 comments on commit 70cc527

Please sign in to comment.