Skip to content

Commit

Permalink
change: consolidate start and attach commands
Browse files Browse the repository at this point in the history
  • Loading branch information
corrupt952 committed Sep 3, 2024
1 parent 1c9c31b commit 9630db9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 68 deletions.
62 changes: 0 additions & 62 deletions command/attach.go

This file was deleted.

34 changes: 30 additions & 4 deletions command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"os"
"strings"

"github.com/google/subcommands"

Expand Down Expand Up @@ -34,6 +35,23 @@ func (*StartCommand) Usage() string {
// SetFlags adds the flags for StartCommand to the specified set.
func (cmd *StartCommand) SetFlags(f *flag.FlagSet) {}

// HasSession checks if the session is already exists.
func (cmd *StartCommand) HasSession(c *config.Config) bool {
r := renderer.ListSessionsRenderer{}
output, err := shell_helper.ExecWithOutput(r.Render())
if err != nil {
logger.Err(err.Error())
return false
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if line == c.Name {
return true
}
}
return false
}

// Execute executes startup tmux session and returns an ExitStatus.
func (cmd *StartCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
path, err := config.ConfigurationPath()
Expand All @@ -52,10 +70,18 @@ func (cmd *StartCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interf
return subcommands.ExitFailure
}

r := renderer.StartRenderer{Config: c}
if err := shell_helper.Exec(r.Render()); err != nil {
logger.Err(err.Error())
return subcommands.ExitFailure
if cmd.HasSession(c) {
r := renderer.AttachRenderer{Config: c}
if err := shell_helper.Exec(r.Render()); err != nil {
logger.Err(err.Error())
return subcommands.ExitFailure
}
} else {
r := renderer.StartRenderer{Config: c}
if err := shell_helper.Exec(r.Render()); err != nil {
logger.Err(err.Error())
return subcommands.ExitFailure
}
}

return subcommands.ExitSuccess
Expand Down
10 changes: 9 additions & 1 deletion helper/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shell
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
)
Expand Down Expand Up @@ -30,9 +31,16 @@ func Exec(command string) error {
"/bin/sh",
[]string{
CurrentShell(),
"-c",
"-cx",
command,
},
os.Environ(),
)
}

// ExecWithOutput executes command on current shell and returns output
func ExecWithOutput(command string) (string, error) {
cmd := exec.Command(CurrentShell(), "-c", command)
out, err := cmd.CombinedOutput()
return string(out), err
}
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func main() {
subcommands.Register(&command.InitCommand{}, "")
subcommands.Register(&command.StartCommand{}, "")
subcommands.Register(&command.KillCommand{}, "")
subcommands.Register(&command.AttachCommand{}, "")
subcommands.Register(&command.VersionCommand{}, "")
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
Expand Down
9 changes: 9 additions & 0 deletions renderer/list_sessions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package renderer

// ListSessionsRenderer represents startup shell script.
type ListSessionsRenderer struct {}

// Render returns
func (r *ListSessionsRenderer) Render() string {
return "tmux list-sessions -F '#{session_name}'"
}
16 changes: 16 additions & 0 deletions renderer/list_sessions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package renderer

import (
"testing"

"tmuxist/config"
test_helper "tmuxist/helper/test"
)

func TestListSessionsRenderer_Render(t *testing.T) {
r := ListSessionsRenderer{}

actual := r.Render()
expected := "tmux list-sessions -F '#{session_name}'"
test_helper.AssertEquals(t, actual, expected)
}

0 comments on commit 9630db9

Please sign in to comment.