Skip to content

Commit

Permalink
By default, write patches to an output file (#214)
Browse files Browse the repository at this point in the history
* By default, write patches to an output file

If -o filename is given, write to filename.
If no -o filename is given and stdout is not a pipe, write to patches.json.
If no -o filename is given and stdout is a pipe, write to pipe.

* Simplify pipe rules
  • Loading branch information
ryanslade authored Jun 3, 2020
1 parent 97dd215 commit 8c11f03
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions cmd/src/actions_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -18,7 +19,7 @@ import (
"time"

"github.com/fatih/color"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-multierror"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/sourcegraph/go-diff/diff"
Expand Down Expand Up @@ -64,7 +65,7 @@ Execute an action on code in repositories. The output of an action is a set of p
Examples:
Execute an action defined in ~/run-gofmt.json:
Execute an action defined in ~/run-gofmt.json and save the patches it produced to 'patches.json'
$ src actions exec -f ~/run-gofmt.json
Expand All @@ -80,6 +81,10 @@ Examples:
$ src actions exec -f ~/run-gofmt.json | src campaign patchset create-from-patches
Execute an action and save the patches it produced to 'patches.json'
$ src actions exec -f ~/run-gofmt.json -o patches.json
Read and execute an action definition from standard input:
$ cat ~/my-action.json | src actions exec -f -
Expand Down Expand Up @@ -137,6 +142,7 @@ Format of the action JSON files:

var (
fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)")
outputFlag = flagSet.String("o", "patches.json", "The output file. Will be used as the destination for patches unless the command is being piped in which case patches are piped to stdout")
parallelismFlag = flagSet.Int("j", runtime.GOMAXPROCS(0), "The number of parallel jobs.")

cacheDirFlag = flagSet.String("cache", displayUserCacheDir, "Directory for caching results.")
Expand Down Expand Up @@ -184,6 +190,28 @@ Format of the action JSON files:
return err
}

var outputWriter io.Writer
if !*createPatchSetFlag && !*forceCreatePatchSetFlag {
// If stdout is a pipe, write to pipe, otherwise
// write to output file
fi, err := os.Stdout.Stat()
if err != nil {
return err
}
isPipe := fi.Mode()&os.ModeCharDevice == 0

if isPipe {
outputWriter = os.Stdout
} else {
f, err := os.Create(*outputFlag)
if err != nil {
return errors.Wrap(err, "creating output file")
}
defer f.Close()
outputWriter = f
}
}

err = validateActionDefinition(actionFile)
if err != nil {
return err
Expand Down Expand Up @@ -269,7 +297,7 @@ Format of the action JSON files:

logger.ActionSuccess(patches, true)

return json.NewEncoder(os.Stdout).Encode(patches)
return json.NewEncoder(outputWriter).Encode(patches)
}

if err != nil {
Expand Down

0 comments on commit 8c11f03

Please sign in to comment.