diff --git a/cmd/src/actions_exec.go b/cmd/src/actions_exec.go index ba09c7d729..abe5c27af8 100644 --- a/cmd/src/actions_exec.go +++ b/cmd/src/actions_exec.go @@ -7,6 +7,7 @@ import ( "encoding/json" "flag" "fmt" + "io" "io/ioutil" "os" "os/exec" @@ -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" @@ -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 @@ -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 - @@ -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.") @@ -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 @@ -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 {