Skip to content

Commit 8c11f03

Browse files
authored
By default, write patches to an output file (#214)
* 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
1 parent 97dd215 commit 8c11f03

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

cmd/src/actions_exec.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"flag"
99
"fmt"
10+
"io"
1011
"io/ioutil"
1112
"os"
1213
"os/exec"
@@ -18,7 +19,7 @@ import (
1819
"time"
1920

2021
"github.com/fatih/color"
21-
multierror "github.com/hashicorp/go-multierror"
22+
"github.com/hashicorp/go-multierror"
2223
"github.com/mattn/go-isatty"
2324
"github.com/pkg/errors"
2425
"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
6465
6566
Examples:
6667
67-
Execute an action defined in ~/run-gofmt.json:
68+
Execute an action defined in ~/run-gofmt.json and save the patches it produced to 'patches.json'
6869
6970
$ src actions exec -f ~/run-gofmt.json
7071
@@ -80,6 +81,10 @@ Examples:
8081
8182
$ src actions exec -f ~/run-gofmt.json | src campaign patchset create-from-patches
8283
84+
Execute an action and save the patches it produced to 'patches.json'
85+
86+
$ src actions exec -f ~/run-gofmt.json -o patches.json
87+
8388
Read and execute an action definition from standard input:
8489
8590
$ cat ~/my-action.json | src actions exec -f -
@@ -137,6 +142,7 @@ Format of the action JSON files:
137142

138143
var (
139144
fileFlag = flagSet.String("f", "-", "The action file. If not given or '-' standard input is used. (Required)")
145+
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")
140146
parallelismFlag = flagSet.Int("j", runtime.GOMAXPROCS(0), "The number of parallel jobs.")
141147

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

193+
var outputWriter io.Writer
194+
if !*createPatchSetFlag && !*forceCreatePatchSetFlag {
195+
// If stdout is a pipe, write to pipe, otherwise
196+
// write to output file
197+
fi, err := os.Stdout.Stat()
198+
if err != nil {
199+
return err
200+
}
201+
isPipe := fi.Mode()&os.ModeCharDevice == 0
202+
203+
if isPipe {
204+
outputWriter = os.Stdout
205+
} else {
206+
f, err := os.Create(*outputFlag)
207+
if err != nil {
208+
return errors.Wrap(err, "creating output file")
209+
}
210+
defer f.Close()
211+
outputWriter = f
212+
}
213+
}
214+
187215
err = validateActionDefinition(actionFile)
188216
if err != nil {
189217
return err
@@ -269,7 +297,7 @@ Format of the action JSON files:
269297

270298
logger.ActionSuccess(patches, true)
271299

272-
return json.NewEncoder(os.Stdout).Encode(patches)
300+
return json.NewEncoder(outputWriter).Encode(patches)
273301
}
274302

275303
if err != nil {

0 commit comments

Comments
 (0)