diff --git a/commander.go b/commander.go index ae81042..abd3b89 100644 --- a/commander.go +++ b/commander.go @@ -1,6 +1,7 @@ package main import ( + "log" "os/exec" "strings" ) @@ -11,12 +12,19 @@ type Commander interface { } type DefaultCommander struct { + logger *log.Logger } 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 c.logger != nil { + c.logger.Println(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 { + if c.logger != nil { + c.logger.Println(strings.Join(cmd.Args, " ")) + } + err := cmd.Run() if err != nil { + if c.logger != nil { + c.logger.Println(err) + } return &ShellError{strings.Join(cmd.Args, " "), err} } return nil diff --git a/main.go b/main.go index 5c10965..77813e1 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/ioutil" + "log" "os" "path/filepath" @@ -33,7 +34,17 @@ func main() { 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} smug := Smug{tmux, commander} diff --git a/options.go b/options.go index cde6dac..1729874 100644 --- a/options.go +++ b/options.go @@ -9,7 +9,7 @@ import ( const usage = `Smug - tmux session manager. Usage: - smug [-w ]... [--attach] + smug [-w ]... [--attach] [--debug] Options: -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 Windows []string Attach bool + Debug bool } 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 } + debug, err := arguments.Bool("--debug") + if err != nil { + return Options{}, err + } + var windows []string if strings.Contains(project, ":") { @@ -60,5 +66,5 @@ func ParseOptions(p docopt.Parser, argv []string) (Options, error) { windows = arguments["-w"].([]string) } - return Options{cmd, project, windows, attach}, nil + return Options{cmd, project, windows, attach, debug}, nil } diff --git a/options_test.go b/options_test.go index 024768d..1a581d9 100644 --- a/options_test.go +++ b/options_test.go @@ -13,19 +13,19 @@ var usageTestTable = []struct { }{ { []string{"start", "smug"}, - Options{"start", "smug", []string{}, false}, + Options{"start", "smug", []string{}, false, false}, }, { []string{"start", "smug", "-wfoo"}, - Options{"start", "smug", []string{"foo"}, false}, + Options{"start", "smug", []string{"foo"}, false, false}, }, { []string{"start", "smug:foo,bar"}, - Options{"start", "smug", []string{"foo", "bar"}, false}, + Options{"start", "smug", []string{"foo", "bar"}, false, false}, }, { - []string{"start", "smug", "--attach"}, - Options{"start", "smug", []string{}, true}, + []string{"start", "smug", "--attach", "--debug"}, + Options{"start", "smug", []string{}, true, true}, }, } diff --git a/smug.go b/smug.go index 43f37b5..4eebb6d 100644 --- a/smug.go +++ b/smug.go @@ -114,7 +114,6 @@ func (smug Smug) Start(config Config, windows []string, attach bool) error { } } - var createdWindows []string for wIndex, w := range config.Windows { if (len(windows) == 0 && w.Manual) || (len(windows) > 0 && !Contains(windows, w.Name)) { continue @@ -125,17 +124,12 @@ func (smug Smug) Start(config Config, windows []string, attach bool) error { windowRoot = filepath.Join(sessionRoot, w.Root) } - var window string - - if (wIndex == 0 || len(createdWindows) == 0) && !sessionExists { - window = ses + w.Name - } else { - - window, err = smug.tmux.NewWindow(ses, w.Name, windowRoot) + window := ses + w.Name + if (!sessionExists && wIndex > 0 && len(windows) == 0) || (sessionExists && len(windows) > 0) { + _, err = smug.tmux.NewWindow(ses, w.Name, windowRoot) if err != nil { return err } - createdWindows = append(createdWindows, window) } for _, c := range w.Commands { diff --git a/smug_test.go b/smug_test.go index 47144ae..839360b 100644 --- a/smug_test.go +++ b/smug_test.go @@ -98,6 +98,40 @@ var testTable = []struct { "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 {