Skip to content

Commit

Permalink
Merge pull request #83 from pressly/debug
Browse files Browse the repository at this point in the history
CLI: Implement --debug and --disable-prefix options
  • Loading branch information
VojtechVitek authored Jul 18, 2016
2 parents f0875f1 + 0c0fcca commit 149bfb7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
9 changes: 9 additions & 0 deletions cmd/sup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var (
onlyHosts string
exceptHosts string

debug bool
disablePrefix bool

showVersion bool
showHelp bool

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
}

Expand Down
40 changes: 30 additions & 10 deletions sup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
11 changes: 10 additions & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 149bfb7

Please sign in to comment.