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

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

@ -9,7 +9,7 @@ import (
const usage = `Smug - tmux session manager.
Usage:
smug <command> <project> [-w <window>]... [--attach]
smug <command> <project> [-w <window>]... [--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
}

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

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

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

Loading…
Cancel
Save