Skip to content

Commit

Permalink
feat: add startup command
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmedeski committed Aug 2, 2024
1 parent 6a62cbe commit 0f93be1
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 41 deletions.
88 changes: 88 additions & 0 deletions cloner/mock_Cloner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions connector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/model"
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/zoxide"
"github.com/stretchr/testify/assert"
Expand All @@ -19,6 +20,7 @@ func TestConfigStrategy(t *testing.T) {
mockHome := new(home.MockHome)
mockLister := new(lister.MockLister)
mockNamer := new(namer.MockNamer)
mockStartup := new(startup.MockStartup)
mockTmux := new(tmux.MockTmux)
mockZoxide := new(zoxide.MockZoxide)

Expand All @@ -28,6 +30,7 @@ func TestConfigStrategy(t *testing.T) {
mockHome,
mockLister,
mockNamer,
mockStartup,
mockTmux,
mockZoxide,
}
Expand Down
3 changes: 2 additions & 1 deletion connector/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ func (c *RealConnector) Connect(name string, opts model.ConnectOpts) (string, er

for _, strategy := range strategies {
if connection, err := strategy(c, name); err != nil {
} else if connection.Found {
return "", fmt.Errorf("failed to establish connection: %w", err)
} else if connection.Found {
// TODO: allow CLI flag to disable zoxide and overwrite all settings?
// sesh connect --ignore-zoxide "dotfiles"
if connection.AddToZoxide {
c.zoxide.Add(connection.Session.Path)
}
if connection.New {
c.tmux.NewSession(connection.Session.Name, connection.Session.Path)
c.startup.Exec(connection.Session)
}
// TODO: configure the ability to create a session in a detached way (like update)
// TODO: configure the ability to create a popup instead of switching (with no tmux bar?)
Expand Down
18 changes: 11 additions & 7 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/model"
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/zoxide"
)
Expand All @@ -15,13 +16,14 @@ type Connector interface {
}

type RealConnector struct {
config model.Config
dir dir.Dir
home home.Home
lister lister.Lister
namer namer.Namer
tmux tmux.Tmux
zoxide zoxide.Zoxide
config model.Config
dir dir.Dir
home home.Home
lister lister.Lister
namer namer.Namer
startup startup.Startup
tmux tmux.Tmux
zoxide zoxide.Zoxide
}

func NewConnector(
Expand All @@ -30,6 +32,7 @@ func NewConnector(
home home.Home,
lister lister.Lister,
namer namer.Namer,
startup startup.Startup,
tmux tmux.Tmux,
zoxide zoxide.Zoxide,
) Connector {
Expand All @@ -39,6 +42,7 @@ func NewConnector(
home,
lister,
namer,
startup,
tmux,
zoxide,
}
Expand Down
3 changes: 3 additions & 0 deletions connector/tmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/model"
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/zoxide"
"github.com/stretchr/testify/assert"
Expand All @@ -19,6 +20,7 @@ func TestEstablishTmuxConnection(t *testing.T) {
mockHome := new(home.MockHome)
mockLister := new(lister.MockLister)
mockNamer := new(namer.MockNamer)
mockStartup := new(startup.MockStartup)
mockTmux := new(tmux.MockTmux)
mockZoxide := new(zoxide.MockZoxide)

Expand All @@ -28,6 +30,7 @@ func TestEstablishTmuxConnection(t *testing.T) {
mockHome,
mockLister,
mockNamer,
mockStartup,
mockTmux,
mockZoxide,
}
Expand Down
7 changes: 4 additions & 3 deletions lister/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func listConfig(l *RealLister) (model.SeshSessions, error) {
return model.SeshSessions{}, fmt.Errorf("couldn't expand home: %q", err)
}
directory[key] = model.SeshSession{
Src: "config",
Name: session.Name,
Path: path,
Src: "config",
Name: session.Name,
Path: path,
StartupCommand: session.StartupCommand,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type (
}

DefaultSessionConfig struct {
StartupScript string `toml:"startup_script"`
// TODO: mention breaking change in v2 release notes
// StartupScript string `toml:"startup_script"`
StartupCommand string `toml:"startup_command"`
Tmuxp string `toml:"tmuxp"`
Tmuxinator string `toml:"tmuxinator"`
Expand Down
7 changes: 4 additions & 3 deletions model/sesh_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type (
Name string // The display name
Path string // The absolute directory path

Attached int // Whether the session is currently attached
Windows int // The number of windows in the session
Score float64 // The score of the session (from Zoxide)
StartupCommand string // The command to run when the session is started
Attached int // Whether the session is currently attached
Windows int // The number of windows in the session
Score float64 // The score of the session (from Zoxide)
}

SeshSrcs struct {
Expand Down
5 changes: 1 addition & 4 deletions namer/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import "strings"
// Gets the name from a git bare repository
func gitBareName(n *RealNamer, path string) (string, error) {
var name string
isGit, commonDir, err := n.git.GitCommonDir(path)
if err != nil {
return "", err
}
isGit, commonDir, _ := n.git.GitCommonDir(path)
if isGit && strings.HasSuffix(commonDir, "/.bare") {
topLevelDir := strings.TrimSuffix(commonDir, "/.bare")
relativePath := strings.TrimPrefix(path, topLevelDir)
Expand Down
20 changes: 0 additions & 20 deletions namer/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,3 @@ func TestFromPath(t *testing.T) {
assert.Equal(t, "neovim", name)
})
}

func TestConvertToValidName(t *testing.T) {
t.Run("Test with dot", func(t *testing.T) {
input := "test.name"
want := "test_name"
assert.Equal(t, want, convertToValidName(input))
})

t.Run("Test with colon", func(t *testing.T) {
input := "test:name"
want := "test_name"
assert.Equal(t, want, convertToValidName(input))
})

t.Run("Test with multiple special characters", func(t *testing.T) {
input := "test.name:with.multiple"
want := "test_name_with_multiple"
assert.Equal(t, want, convertToValidName(input))
})
}
4 changes: 3 additions & 1 deletion seshcli/seshcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/joshmedeski/sesh/pathwrap"
"github.com/joshmedeski/sesh/runtimewrap"
"github.com/joshmedeski/sesh/shell"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/zoxide"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -44,8 +45,9 @@ func App(version string) cli.App {

// core dependencies
lister := lister.NewLister(config, home, tmux, zoxide)
startup := startup.NewStartup(config, lister, tmux)
namer := namer.NewNamer(path, git)
connector := connector.NewConnector(config, dir, home, lister, namer, tmux, zoxide)
connector := connector.NewConnector(config, dir, home, lister, namer, startup, tmux, zoxide)

return cli.App{
Name: "sesh",
Expand Down
11 changes: 11 additions & 0 deletions startup/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package startup

import "github.com/joshmedeski/sesh/model"

func configStrategy(s *RealStartup, session model.SeshSession) (string, error) {
config, exists := s.lister.FindConfigSession(session.Name)
if exists && config.StartupCommand != "" {
return config.StartupCommand, nil
}
return "", nil
}
11 changes: 11 additions & 0 deletions startup/defaultconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package startup

import "github.com/joshmedeski/sesh/model"

func defaultConfigStrategy(s *RealStartup, session model.SeshSession) (string, error) {
defaultConfig := s.config.DefaultSessionConfig
if defaultConfig.StartupCommand != "" {
return defaultConfig.StartupCommand, nil
}
return "", nil
}
Loading

0 comments on commit 0f93be1

Please sign in to comment.