Skip to content

Commit b1b0369

Browse files
authored
feat: add stdin request support (#760)
1 parent 5d2c980 commit b1b0369

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

cmd/lk/proto.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

97100
func 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

102105
func 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

Comments
 (0)