From 75e77dd325cc95ff61b500e69e326120816d8d2b Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 13 Apr 2021 10:43:35 +0400 Subject: [PATCH] Add --detach flag (#56) --- options.go | 13 ++++++++- options_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++------ smug.go | 2 +- smug_test.go | 50 ++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 10 deletions(-) diff --git a/options.go b/options.go index d528901..06decb1 100644 --- a/options.go +++ b/options.go @@ -24,6 +24,7 @@ type Options struct { Config string Windows []string Attach bool + Detach bool Debug bool } @@ -32,6 +33,7 @@ var ErrHelp = errors.New("help requested") const ( WindowsUsage = "List of windows to start. If session exists, those windows will be attached to current session." AttachUsage = "Force switch client for a session" + DetachUsage = "Detach tmux session. The same as -d flag in the tmux" DebugUsage = "Print all commands to ~/.config/smug/smug.log" FileUsage = "A custom path to a config file" ) @@ -65,6 +67,7 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) { config := flags.StringP("file", "f", "", FileUsage) windows := flags.StringArrayP("windows", "w", []string{}, WindowsUsage) attach := flags.BoolP("attach", "a", false, AttachUsage) + detach := flags.Bool("detach", false, DetachUsage) debug := flags.BoolP("debug", "d", false, DebugUsage) err := flags.Parse(argv) @@ -88,5 +91,13 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) { windows = &wl } - return Options{cmd, project, *config, *windows, *attach, *debug}, nil + return Options{ + Project: project, + Config: *config, + Command: cmd, + Windows: *windows, + Attach: *attach, + Detach: *detach, + Debug: *debug, + }, nil } diff --git a/options_test.go b/options_test.go index aad7a71..f551de3 100644 --- a/options_test.go +++ b/options_test.go @@ -16,43 +16,99 @@ var usageTestTable = []struct { }{ { []string{"start", "smug"}, - Options{"start", "smug", "", []string{}, false, false}, + Options{ + Command: "start", + Project: "smug", + Config: "", + Windows: []string{}, + Attach: false, + Detach: false, + Debug: false, + }, nil, 0, }, { []string{"start", "smug", "-w", "foo"}, - Options{"start", "smug", "", []string{"foo"}, false, false}, + Options{ + Command: "start", + Project: "smug", + Config: "", + Windows: []string{"foo"}, + Attach: false, + Detach: false, + Debug: false, + }, nil, 0, }, { []string{"start", "smug:foo,bar"}, - Options{"start", "smug", "", []string{"foo", "bar"}, false, false}, + Options{ + Command: "start", + Project: "smug", + Config: "", + Windows: []string{"foo", "bar"}, + Attach: false, + Detach: false, + Debug: false, + }, nil, 0, }, { - []string{"start", "smug", "--attach", "--debug"}, - Options{"start", "smug", "", []string{}, true, true}, + []string{"start", "smug", "--attach", "--debug", "--detach"}, + Options{ + Command: "start", + Project: "smug", + Config: "", + Windows: []string{}, + Attach: true, + Detach: true, + Debug: true, + }, nil, 0, }, { []string{"start", "smug", "-ad"}, - Options{"start", "smug", "", []string{}, true, true}, + Options{ + Command: "start", + Project: "smug", + Config: "", + Windows: []string{}, + Attach: true, + Detach: false, + Debug: true, + }, nil, 0, }, { []string{"start", "-f", "test.yml"}, - Options{"start", "", "test.yml", []string{}, false, false}, + Options{ + Command: "start", + Project: "", + Config: "test.yml", + Windows: []string{}, + Attach: false, + Detach: false, + Debug: false, + }, nil, 0, }, { []string{"start", "-f", "test.yml", "-w", "win1", "-w", "win2"}, - Options{"start", "", "test.yml", []string{"win1", "win2"}, false, false}, + Options{ + Command: "start", + Project: "", + Config: "test.yml", + Windows: []string{"win1", "win2"}, + Attach: false, + Detach: false, + Debug: false, + }, nil, 0, }, diff --git a/smug.go b/smug.go index 4e97eba..32e8287 100644 --- a/smug.go +++ b/smug.go @@ -159,7 +159,7 @@ func (smug Smug) Start(config Config, options Options, context Context) error { smug.tmux.KillWindow(sessionName + defaultWindowName) smug.tmux.RenumberWindows(sessionName) - if len(windows) == 0 && len(config.Windows) > 0 { + if len(windows) == 0 && len(config.Windows) > 0 && options.Detach == false { return smug.switchOrAttach(sessionName+config.Windows[0].Name, attach, context.InsideTmuxSession) } diff --git a/smug_test.go b/smug_test.go index ae91c8f..8f1accd 100644 --- a/smug_test.go +++ b/smug_test.go @@ -15,6 +15,36 @@ var testTable = []struct { stopCommands []string commanderOutputs []string }{ + { + Config{ + Session: "ses", + Root: "root", + BeforeStart: []string{"command1", "command2"}, + Windows: []Window{ + { + Name: "win1", + }, + }, + }, + Options{ + Detach: true, + }, + Context{}, + []string{ + "tmux has-session -t ses:", + "/bin/sh -c command1", + "/bin/sh -c command2", + "tmux new -Pd -s ses -n smug_def -c root", + "tmux neww -Pd -t ses: -c root -F #{window_id} -n win1", + "tmux select-layout -t xyz even-horizontal", + "tmux kill-window -t ses:smug_def", + "tmux move-window -r -s ses: -t ses:", + }, + []string{ + "tmux kill-session -t ses", + }, + []string{"xyz"}, + }, { Config{ Session: "ses", @@ -123,6 +153,7 @@ var testTable = []struct { }, []string{"xyz"}, }, + { Config{ Session: "ses", @@ -286,6 +317,25 @@ var testTable = []struct { }, []string{""}, }, + { + Config{ + Session: "ses", + Root: "root", + Windows: []Window{ + {Name: "win1"}, + }, + }, + Options{Attach: true}, + Context{InsideTmuxSession: true}, + []string{ + "tmux has-session -t ses:", + "tmux switch-client -t ses:", + }, + []string{ + "tmux kill-session -t ses", + }, + []string{""}, + }, } type MockCommander struct {