diff --git a/README.md b/README.md index dc7b76e..8ff5895 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Stack Up is a simple deployment tool that performs given set of commands on mult | `-e`, `--env=[]` | Set environment variables | | `--only REGEXP` | Filter hosts matching regexp | | `--except REGEXP` | Filter out hosts matching regexp | +| `--debug`, `-D` | Enable debug/verbose mode | +| `--disable-prefix`| Disable hostname prefix | | `--help`, `-h` | Show help/usage | | `--version`, `-v` | Print version | diff --git a/cmd/sup/main.go b/cmd/sup/main.go index ec96ff6..e62b1e3 100644 --- a/cmd/sup/main.go +++ b/cmd/sup/main.go @@ -19,6 +19,9 @@ var ( onlyHosts string exceptHosts string + debug bool + disablePrefix bool + showVersion bool showHelp bool @@ -47,6 +50,10 @@ func init() { flag.StringVar(&onlyHosts, "only", "", "Filter hosts using regexp") flag.StringVar(&exceptHosts, "except", "", "Filter out hosts using regexp") + flag.BoolVar(&debug, "D", false, "Enable debug mode") + flag.BoolVar(&debug, "debug", false, "Enable debug mode") + flag.BoolVar(&disablePrefix, "disable-prefix", false, "Disable hostname prefix") + flag.BoolVar(&showVersion, "v", false, "Print version") flag.BoolVar(&showVersion, "version", false, "Print version") flag.BoolVar(&showHelp, "h", false, "Show help") @@ -271,6 +278,8 @@ func main() { fmt.Fprintln(os.Stderr, err) os.Exit(1) } + app.Debug(debug) + app.Prefix(!disablePrefix) // Run all the commands in the given network. err = app.Run(network, commands...) diff --git a/localhost.go b/localhost.go index c7a1d9b..ebdc495 100644 --- a/localhost.go +++ b/localhost.go @@ -38,7 +38,7 @@ func (c *LocalhostClient) Run(task *Task) error { return fmt.Errorf("Command already running") } - cmd := exec.Command("bash", "-c", c.env+"set -x;"+task.Run) + cmd := exec.Command("bash", "-c", c.env+task.Run) c.cmd = cmd c.stdout, err = cmd.StdoutPipe() diff --git a/ssh.go b/ssh.go index 9974c46..f8ff8e4 100644 --- a/ssh.go +++ b/ssh.go @@ -192,7 +192,7 @@ func (c *SSHClient) Run(task *Task) error { } // Start the remote command. - if err := sess.Start(c.env + "set -x;" + task.Run); err != nil { + if err := sess.Start(c.env + task.Run); err != nil { return ErrTask{task, err.Error()} } diff --git a/sup.go b/sup.go index 3db65f4..4c42745 100644 --- a/sup.go +++ b/sup.go @@ -16,7 +16,9 @@ import ( const VERSION = "0.4" type Stackup struct { - conf *Supfile + conf *Supfile + debug bool + prefix bool } func New(conf *Supfile) (*Stackup, error) { @@ -114,7 +116,7 @@ func (sup *Stackup) Run(network *Network, commands ...*Command) error { // Run command or run multiple commands defined by target sequentially. for _, cmd := range commands { // Translate command into task(s). - tasks, err := CreateTasks(cmd, clients, env) + tasks, err := sup.createTasks(cmd, clients, env) if err != nil { return errors.Wrap(err, "creating task failed") } @@ -126,9 +128,13 @@ func (sup *Stackup) Run(network *Network, commands ...*Command) error { // Run tasks on the provided clients. for _, c := range task.Clients { - prefix, prefixLen := c.Prefix() - if len(prefix) < maxLen { // Left padding. - prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix + var prefix string + var prefixLen int + if sup.prefix { + prefix, prefixLen = c.Prefix() + if len(prefix) < maxLen { // Left padding. + prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix + } } err := c.Run(task) @@ -205,16 +211,22 @@ func (sup *Stackup) Run(network *Network, commands ...*Command) error { go func(c Client) { defer wg.Done() if err := c.Wait(); err != nil { - prefix, prefixLen := c.Prefix() - if len(prefix) < maxLen { // Left padding. - prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix + var prefix string + if sup.prefix { + var prefixLen int + prefix, prefixLen = c.Prefix() + if len(prefix) < maxLen { // Left padding. + prefix = strings.Repeat(" ", maxLen-prefixLen) + prefix + } } if e, ok := err.(*ssh.ExitError); ok && e.ExitStatus() != 15 { // TODO: Store all the errors, and print them after Wait(). - fmt.Fprintln(os.Stderr, errors.Wrap(e, prefix)) + fmt.Fprintf(os.Stderr, "%s%v\n", prefix, e) os.Exit(e.ExitStatus()) } - fmt.Fprintf(os.Stderr, "%v", errors.Wrap(err, prefix)) + fmt.Fprintf(os.Stderr, "%s%v\n", prefix, err) + + // TODO: Shouldn't os.Exit(1) here. Instead, collect the exit statuses for later. os.Exit(1) } }(c) @@ -231,3 +243,11 @@ func (sup *Stackup) Run(network *Network, commands ...*Command) error { return nil } + +func (sup *Stackup) Debug(value bool) { + sup.debug = value +} + +func (sup *Stackup) Prefix(value bool) { + sup.prefix = value +} diff --git a/task.go b/task.go index ac2fd9d..eebc3c7 100644 --- a/task.go +++ b/task.go @@ -17,7 +17,7 @@ type Task struct { TTY bool } -func CreateTasks(cmd *Command, clients []Client, env string) ([]*Task, error) { +func (sup *Stackup) createTasks(cmd *Command, clients []Client, env string) ([]*Task, error) { var tasks []*Task cwd, err := os.Getwd() @@ -77,6 +77,9 @@ func CreateTasks(cmd *Command, clients []Client, env string) ([]*Task, error) { Run: string(data), TTY: true, } + if sup.debug { + task.Run = "set -x;" + task.Run + } if cmd.Stdin { task.Input = os.Stdin } @@ -111,6 +114,9 @@ func CreateTasks(cmd *Command, clients []Client, env string) ([]*Task, error) { Clients: []Client{local}, TTY: true, } + if sup.debug { + task.Run = "set -x;" + task.Run + } if cmd.Stdin { task.Input = os.Stdin } @@ -123,6 +129,9 @@ func CreateTasks(cmd *Command, clients []Client, env string) ([]*Task, error) { Run: cmd.Run, TTY: true, } + if sup.debug { + task.Run = "set -x;" + task.Run + } if cmd.Stdin { task.Input = os.Stdin }