Skip to content

Commit 117f5ef

Browse files
committed
Use - to denote reading from stdin
Instead of taking --stdin-format to imply reading from stdin, use the convention of supplying `-` as an argument to denote reading from stdin, and just use the flag to alter how it's read. This will line up better with expectations; and, since --stdin-format can have a default (of "yaml"), invocations can be concise: jk transform ./script.js - .. rather than jk transform ./script.js --stdin-format=yaml Note that _no_ arguments does not imply reading from stdin. I did not want something that accidentally supplies no filenames to block, since that would make automation using `jk transform` brittle.
1 parent 45c3d12 commit 117f5ef

File tree

5 files changed

+42
-19
lines changed

5 files changed

+42
-19
lines changed

std/cmd/transform.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Format, Overwrite, read, stdin } from '../index';
1+
import { Format, Overwrite, read, stdin, print } from '../index';
22
import * as host from '@jkcfg/std/internal/host'; // magic module
33
import * as param from '../param';
44
import { generate, File, GenerateParams, maybeSetFormat } from './generate';
@@ -29,15 +29,16 @@ function transform(fn: TransformFn): void {
2929
const inputFiles = param.Object('jk.transform.input', {});
3030
const outputs = [];
3131

32-
const stdinFormat = param.String('jk.transform.stdin.format', '');
33-
if (stdinFormat !== '') {
34-
const format = valuesFormatFromExtension(stdinFormat);
35-
const path = `stdin.${stdinFormat}`; // path is a stand-in
36-
const value = read(stdin, { format }).then(v => v.map(transformOne));
37-
outputs.push({ path, value, format });
38-
}
39-
4032
for (const path of Object.keys(inputFiles)) {
33+
if (path === '') { // read from stdin
34+
const stdinFormat = param.String('jk.transform.stdin.format', 'yaml');
35+
const format = valuesFormatFromExtension(stdinFormat);
36+
const path = `stdin.${stdinFormat}`; // path is a stand-in
37+
const value = read(stdin, { format }).then(v => v.map(transformOne));
38+
outputs.push({ path, value, format });
39+
continue;
40+
}
41+
4142
const format = valuesFormatFromPath(path);
4243
outputs.push(host.read(path, { format }).then((obj): File => {
4344
switch (format) {

tests/test-transform-stdin.js.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jk transform -c "v => v + 1" --stdin-format=json - ./test-transform-files/*.json
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
7
2+
2
3+
3

tests/test-transform-stdin.js.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6

transform.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ var transformCmd = &cobra.Command{
1818
}
1919

2020
const transformExamples = `
21-
running the default export of a module (or file) on each input document
21+
22+
run the default export of script.js on each YAML value read from
23+
stdin, printing the transformed values to stdout
24+
25+
jk transform ./script.js -
26+
27+
run the default export of a module (or file) on each input document,
28+
and write the results back to outputdir/
29+
2230
jk transform -o outputdir/ ./script.js ./inputdir/*.json
23-
running a function on each input, and printing the results to stdout
31+
32+
run a function on each input file, and print the results to stdout
33+
2434
jk transform --stdout -c '({ name: n, ...fields }) => ({ name: n + "-dev", ...fields })' inputdir/*.yaml
2535
`
2636

@@ -33,17 +43,18 @@ var transformOptions struct {
3343
overwrite bool
3444
// force the format of the output
3545
format string
36-
// read from stdin, expecting a stream of values in the format given
46+
// when reading from stdin (`-` is supplied as an argument),
47+
// expect a stream of values in the format given
3748
stdinFormat string
3849
}
3950

4051
func init() {
4152
initScriptFlags(transformCmd, &transformOptions.scriptOptions)
4253
initExecFlags(transformCmd, &transformOptions.vmOptions)
43-
transformCmd.PersistentFlags().BoolVar(&transformOptions.stdout, "stdout", false, "print the resulting values to stdout")
54+
transformCmd.PersistentFlags().BoolVar(&transformOptions.stdout, "stdout", false, "print the resulting values to stdout (implied if reading from stdin)")
4455
transformCmd.PersistentFlags().BoolVar(&transformOptions.overwrite, "overwrite", false, "allow input file(s) to be overwritten by output file(s); otherwise, an error will be thrown")
45-
transformCmd.PersistentFlags().StringVar(&transformOptions.format, "format", "", "force all values to this format")
46-
transformCmd.PersistentFlags().StringVar(&transformOptions.stdinFormat, "stdin-format", "", "read values from stdin, assuming this format; implies --stdout")
56+
transformCmd.PersistentFlags().StringVar(&transformOptions.format, "format", "", "force all output values to this format")
57+
transformCmd.PersistentFlags().StringVar(&transformOptions.stdinFormat, "stdin-format", "yaml", "assume this format for values from stdin (either a 'yaml' stream or concatenated 'json' values)")
4758
jk.AddCommand(transformCmd)
4859
}
4960

@@ -64,11 +75,18 @@ func transform(cmd *cobra.Command, args []string) {
6475
// now). This is in part to get around the limitations of
6576
// parameters (arrays are not supported as values), and partly in
6677
// anticipation of there being more information to pass about each
67-
// input.
78+
// input. Stdin is encoded as an empty string, since `"-"` could
79+
// be the name of a file.
6880
inputs := make(map[string]interface{})
6981
for _, f := range args[1:] {
70-
inputs[f] = f
82+
if f == "-" {
83+
inputs[""] = ""
84+
transformOptions.stdout = true
85+
} else {
86+
inputs[f] = f
87+
}
7188
}
89+
7290
vm.parameters.Set("jk.transform.input", inputs)
7391
vm.parameters.Set("jk.transform.stdout", transformOptions.stdout)
7492
vm.parameters.Set("jk.transform.overwrite", transformOptions.overwrite)
@@ -77,9 +95,8 @@ func transform(cmd *cobra.Command, args []string) {
7795
switch transformOptions.stdinFormat {
7896
case "json", "yaml":
7997
vm.parameters.Set("jk.transform.stdin.format", transformOptions.stdinFormat)
80-
vm.parameters.Set("jk.transform.stdout", true)
8198
default:
82-
break
99+
log.Fatal("--stdin-format must be 'json' or 'yaml'")
83100
}
84101

85102
var module string

0 commit comments

Comments
 (0)