Skip to content

Commit

Permalink
Merged updates from feature/82979704_jrubin_update_envetcd_with_outpu…
Browse files Browse the repository at this point in the history
…t_file_option
  • Loading branch information
Carl Saturnino committed Nov 20, 2014
1 parent af8d820 commit 79ed6d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2014-11-18 Joshua Rubin <jrubin@zvelo.com>

* Start feature "Update envetcd with output file option"
https://www.pivotaltracker.com/story/show/82979704
https://github.com/zvelo/envetcd/pull/2
5 changes: 4 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func run(c *cli.Context) {
command := args[0:]

log.Printf("[DEBUG] (cli) creating Runner")
runner := newRunner(c, command)
runner, err := newRunner(c, command)
if err != nil {
handleError(err, exitCodeParseFlagsError)
}

log.Printf("[DEBUG] (cli) creating etcd API client")
etcdConfig := newEtcdConfig(c)
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func init() {
Value: "WARN",
Usage: "set log level (DEBUG, INFO, WARN, ERR)",
},
cli.StringFlag{
Name: "output, o",
EnvVar: "ENVETCD_OUTPUT",
Usage: "write stdout from the command to this file",
},
cli.BoolFlag{
Name: "no-sync",
EnvVar: "ENVETCD_NO_SYNC",
Expand Down
47 changes: 38 additions & 9 deletions runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"regexp"
Expand All @@ -28,17 +30,30 @@ type runner struct {

// data is the latest representation of the data from etcd.
data KeyPairs

outFile *os.File
outStream io.Writer
}

func newRunner(c *cli.Context, command []string) *runner {
func newRunner(c *cli.Context, command []string) (*runner, error) {
run := &runner{
command: command,
sanitize: !c.Bool("no-sanitize"),
upcase: !c.Bool("no-upcase"),
cleanEnv: c.Bool("clean-env"),
command: command,
sanitize: !c.Bool("no-sanitize"),
upcase: !c.Bool("no-upcase"),
cleanEnv: c.Bool("clean-env"),
outStream: os.Stdout,
}

if output := c.String("output"); len(output) > 0 {
outFile, err := os.Create(output)
if err != nil {
return nil, err
}
run.outFile = outFile
run.outStream = bufio.NewWriter(outFile)
}

return run
return run, nil
}

// Run executes and manages the child process with the correct environment. The
Expand Down Expand Up @@ -70,7 +85,7 @@ func (r *runner) run() error {
}

cmd := exec.Command(r.command[0], r.command[1:]...)
cmd.Stdout = os.Stdout
cmd.Stdout = r.outStream
cmd.Stderr = os.Stderr
cmd.Env = cmdEnv
err := cmd.Start()
Expand All @@ -82,8 +97,22 @@ func (r *runner) run() error {
// (if any) don't cause us to exit, and start a goroutine
// to wait for that process to end.
r.exitCh = make(chan int, 1)
go func(cmd *exec.Cmd, exitCh chan<- int) {
go func(cmd *exec.Cmd, exitCh chan<- int, outFile *os.File) {
err := cmd.Wait()

if outFile != nil {
writer, ok := cmd.Stdout.(*bufio.Writer)
if ok {
writer.Flush()

}

if err := outFile.Close(); err != nil {
exitCh <- exitCodeError
return
}
}

if err == nil {
exitCh <- exitCodeOK
return
Expand All @@ -98,7 +127,7 @@ func (r *runner) run() error {
}

exitCh <- exitCodeError
}(cmd, r.exitCh)
}(cmd, r.exitCh, r.outFile)

return nil
}

0 comments on commit 79ed6d7

Please sign in to comment.