Add --detach flag (#56)

master
Ivan 3 years ago committed by GitHub
parent d3f5d57af4
commit 75e77dd325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}

@ -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,
},

@ -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)
}

@ -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 {

Loading…
Cancel
Save