@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "errors"
2020 "fmt"
21+ "io"
2122 "os"
2223 "reflect"
2324
@@ -69,7 +70,9 @@ func ReadRequestFileOrLiteral[T any, P protoType[T]](pathOrLiteral string) (P, e
6970 var err error
7071
7172 // This allows us to read JSON from either CLI arg or FS
72- if _ , err = os .Stat (pathOrLiteral ); err == nil {
73+ if pathOrLiteral == "-" {
74+ reqBytes , err = io .ReadAll (os .Stdin )
75+ } else if _ , err = os .Stat (pathOrLiteral ); err == nil {
7376 reqBytes , err = os .ReadFile (pathOrLiteral )
7477 } else {
7578 reqBytes = []byte (pathOrLiteral )
@@ -96,7 +99,7 @@ func RequestFlag[T any, P protoType[T]]() *cli.StringFlag {
9699
97100func RequestDesc [T any , _ protoType [T ]]() string {
98101 typ := reflect .TypeFor [T ]().Name ()
99- return typ + " as JSON file"
102+ return typ + " as JSON file (or - for stdin) "
100103}
101104
102105func createAndPrintFile [T any , P protoTypeValidator [T ], R any ](
@@ -175,6 +178,17 @@ func createAndPrintReqs[T any, P protoTypeValidator[T], R any](
175178 var req P = new (T )
176179 return createAndPrintReq (ctx , cmd , req , fill , create , print )
177180 }
181+ if args .Len () > 1 {
182+ stdinCount := 0
183+ for _ , file := range args .Slice () {
184+ if file == "-" {
185+ stdinCount ++
186+ if stdinCount > 1 {
187+ return errors .New ("stdin can only be used once" )
188+ }
189+ }
190+ }
191+ }
178192 for _ , file := range args .Slice () {
179193 if err := createAndPrintFile (ctx , cmd , file , fill , create , print ); err != nil {
180194 return err
0 commit comments