-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kazuki HASEGAWA
committed
Jul 5, 2019
1 parent
3d409f6
commit 573a32d
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |