Skip to content

Commit

Permalink
add kill command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuki HASEGAWA committed Jul 5, 2019
1 parent 3d409f6 commit 573a32d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
61 changes: 61 additions & 0 deletions command/kill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package command

import (
"context"
"flag"

"github.com/google/subcommands"

"github.com/corrupt952/tmuxist/config"
shell_helper "github.com/corrupt952/tmuxist/helper/shell"
"github.com/corrupt952/tmuxist/logger"
"github.com/corrupt952/tmuxist/renderer"
)

// KillCommand represents a kill tmux session command.
type KillCommand struct {
profile string
}

// Name returns the name of KillCommand.
func (*KillCommand) Name() string {
return "kill"
}

// Synopsis returns a short string describing KillCommand.
func (*KillCommand) Synopsis() string {
return "kill tmux session"
}

// Usage returns a long string explaining KillCommand and givinig usage.
func (*KillCommand) Usage() string {
return "kill: tmuxist kill [-profile profile]\n"
}

// SetFlags adds the flags for KillCommand to the specified set.
func (cmd *KillCommand) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.profile, "profile", "default", "Profile")
}

// Execute executes kill tmux session and returns an ExitStatus.
func (cmd *KillCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
fpath, err := config.ConfigurationPath(cmd.profile)
if err != nil {
logger.Err(err.Error())
return subcommands.ExitFailure
}

c, err := config.LoadFile(fpath)
if err != nil {
logger.Err(err.Error())
return subcommands.ExitFailure
}

r := renderer.KillRenderer{c}
if err := shell_helper.Exec(r.Render()); err != nil {
logger.Err(err.Error())
return subcommands.ExitFailure
}

return subcommands.ExitSuccess
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
subcommands.Register(&command.EditCommand{}, "")
subcommands.Register(&command.PrintCommand{}, "")
subcommands.Register(&command.StartCommand{}, "")
subcommands.Register(&command.KillCommand{}, "")
subcommands.Register(&command.VersionCommand{}, "")
subcommands.Register(subcommands.HelpCommand(), "")
subcommands.Register(subcommands.CommandsCommand(), "")
Expand Down
26 changes: 26 additions & 0 deletions renderer/kill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package renderer

import (
"fmt"

"github.com/corrupt952/tmuxist/config"
)

// KillRenderer represents startup shell script.
type KillRenderer struct {
Config *config.Config
}

// Render returns startup shell script.
func (r *KillRenderer) Render() string {
s := ""
c := r.Config

name := ""
if c.Name != "" {
name = fmt.Sprintf("-t %s", c.Name)
}
s += fmt.Sprintf(fmt.Sprintf("tmux kill-session %s", name))

return s
}
24 changes: 24 additions & 0 deletions renderer/kill_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package renderer

import (
"testing"

"github.com/corrupt952/tmuxist/config"
test_helper "github.com/corrupt952/tmuxist/helper/test"
)

func TestKillRenderer_Render(t *testing.T) {
r := KillRenderer{&config.Config{}}

actual := r.Render()
expected := "tmux kill-session "
test_helper.AssertEquals(t, actual, expected)
}

func TestKillRenderer_Render_WithName(t *testing.T) {
r := KillRenderer{&config.Config{Name: "tmuxist"}}

actual := r.Render()
expected := "tmux kill-session -t tmuxist"
test_helper.AssertEquals(t, actual, expected)
}

0 comments on commit 573a32d

Please sign in to comment.