Fix creating windows (#17)

* Add --debug flag
* Fix creating windows
master
Ivan 3 years ago committed by GitHub
parent f9b4217af3
commit 2a6aec6ce7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
package main package main
import ( import (
"log"
"os/exec" "os/exec"
"strings" "strings"
) )
@ -11,12 +12,19 @@ type Commander interface {
} }
type DefaultCommander struct { type DefaultCommander struct {
logger *log.Logger
} }
func (c DefaultCommander) Exec(cmd *exec.Cmd) (string, error) { func (c DefaultCommander) Exec(cmd *exec.Cmd) (string, error) {
output, err := cmd.CombinedOutput() if c.logger != nil {
c.logger.Println(strings.Join(cmd.Args, " "))
}
output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
if c.logger != nil {
c.logger.Println(err)
}
return "", &ShellError{strings.Join(cmd.Args, " "), err} return "", &ShellError{strings.Join(cmd.Args, " "), err}
} }
@ -24,8 +32,15 @@ func (c DefaultCommander) Exec(cmd *exec.Cmd) (string, error) {
} }
func (c DefaultCommander) ExecSilently(cmd *exec.Cmd) error { func (c DefaultCommander) ExecSilently(cmd *exec.Cmd) error {
if c.logger != nil {
c.logger.Println(strings.Join(cmd.Args, " "))
}
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
if c.logger != nil {
c.logger.Println(err)
}
return &ShellError{strings.Join(cmd.Args, " "), err} return &ShellError{strings.Join(cmd.Args, " "), err}
} }
return nil return nil

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path/filepath" "path/filepath"
@ -33,7 +34,17 @@ func main() {
os.Exit(1) os.Exit(1)
} }
commander := DefaultCommander{} var logger *log.Logger
if options.Debug {
logFile, err := os.Create(filepath.Join(userConfigDir, "smug.log"))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
logger = log.New(logFile, "", 0)
}
commander := DefaultCommander{logger}
tmux := Tmux{commander} tmux := Tmux{commander}
smug := Smug{tmux, commander} smug := Smug{tmux, commander}

@ -9,7 +9,7 @@ import (
const usage = `Smug - tmux session manager. const usage = `Smug - tmux session manager.
Usage: Usage:
smug <command> <project> [-w <window>]... [--attach] smug <command> <project> [-w <window>]... [--attach] [--debug]
Options: Options:
-w List of windows to start. If session exists, those windows will be attached to current session. -w List of windows to start. If session exists, those windows will be attached to current session.
@ -27,6 +27,7 @@ type Options struct {
Project string Project string
Windows []string Windows []string
Attach bool Attach bool
Debug bool
} }
func ParseOptions(p docopt.Parser, argv []string) (Options, error) { func ParseOptions(p docopt.Parser, argv []string) (Options, error) {
@ -50,6 +51,11 @@ func ParseOptions(p docopt.Parser, argv []string) (Options, error) {
return Options{}, err return Options{}, err
} }
debug, err := arguments.Bool("--debug")
if err != nil {
return Options{}, err
}
var windows []string var windows []string
if strings.Contains(project, ":") { if strings.Contains(project, ":") {
@ -60,5 +66,5 @@ func ParseOptions(p docopt.Parser, argv []string) (Options, error) {
windows = arguments["-w"].([]string) windows = arguments["-w"].([]string)
} }
return Options{cmd, project, windows, attach}, nil return Options{cmd, project, windows, attach, debug}, nil
} }

@ -13,19 +13,19 @@ var usageTestTable = []struct {
}{ }{
{ {
[]string{"start", "smug"}, []string{"start", "smug"},
Options{"start", "smug", []string{}, false}, Options{"start", "smug", []string{}, false, false},
}, },
{ {
[]string{"start", "smug", "-wfoo"}, []string{"start", "smug", "-wfoo"},
Options{"start", "smug", []string{"foo"}, false}, Options{"start", "smug", []string{"foo"}, false, false},
}, },
{ {
[]string{"start", "smug:foo,bar"}, []string{"start", "smug:foo,bar"},
Options{"start", "smug", []string{"foo", "bar"}, false}, Options{"start", "smug", []string{"foo", "bar"}, false, false},
}, },
{ {
[]string{"start", "smug", "--attach"}, []string{"start", "smug", "--attach", "--debug"},
Options{"start", "smug", []string{}, true}, Options{"start", "smug", []string{}, true, true},
}, },
} }

@ -114,7 +114,6 @@ func (smug Smug) Start(config Config, windows []string, attach bool) error {
} }
} }
var createdWindows []string
for wIndex, w := range config.Windows { for wIndex, w := range config.Windows {
if (len(windows) == 0 && w.Manual) || (len(windows) > 0 && !Contains(windows, w.Name)) { if (len(windows) == 0 && w.Manual) || (len(windows) > 0 && !Contains(windows, w.Name)) {
continue continue
@ -125,17 +124,12 @@ func (smug Smug) Start(config Config, windows []string, attach bool) error {
windowRoot = filepath.Join(sessionRoot, w.Root) windowRoot = filepath.Join(sessionRoot, w.Root)
} }
var window string window := ses + w.Name
if (!sessionExists && wIndex > 0 && len(windows) == 0) || (sessionExists && len(windows) > 0) {
if (wIndex == 0 || len(createdWindows) == 0) && !sessionExists { _, err = smug.tmux.NewWindow(ses, w.Name, windowRoot)
window = ses + w.Name
} else {
window, err = smug.tmux.NewWindow(ses, w.Name, windowRoot)
if err != nil { if err != nil {
return err return err
} }
createdWindows = append(createdWindows, window)
} }
for _, c := range w.Commands { for _, c := range w.Commands {

@ -98,6 +98,40 @@ var testTable = []struct {
"win2", "win2",
}, },
}, },
{
Config{
Session: "ses",
Root: "root",
Windows: []Window{
{
Name: "win1",
Manual: false,
Commands: []string{"command1", "command2"},
},
{
Name: "win2",
Manual: false,
Commands: []string{"command3", "command4"},
},
},
},
[]string{
"tmux has-session -t ses",
"tmux new -Pd -s ses -n win1 -c root",
"tmux send-keys -t ses:win1 command1 Enter",
"tmux send-keys -t ses:win1 command2 Enter",
"tmux select-layout -t ses:win1 even-horizontal",
"tmux neww -Pd -t ses: -n win2 -c root",
"tmux send-keys -t ses:win2 command3 Enter",
"tmux send-keys -t ses:win2 command4 Enter",
"tmux select-layout -t ses:win2 even-horizontal",
"tmux attach -d -t ses:",
},
[]string{
"tmux kill-session -t ses",
},
[]string{},
},
} }
type MockCommander struct { type MockCommander struct {

Loading…
Cancel
Save