diff --git a/docs/conventions.md b/docs/conventions.md index 870005c..2a3c489 100644 --- a/docs/conventions.md +++ b/docs/conventions.md @@ -117,6 +117,12 @@ Everything else spelled out: `reference` not `ref`, `description` not `desc`, `p - JSON struct tags: `snake_case` - Imports grouped: stdlib, third-party, internal (blank line between groups) +### CLI stdin handling + +- Centralize CLI stdin parsing in `internal/cli/stdin.go` +- New stdin / JSON / JSONL handling must extend the shared helpers there instead of adding command-local parsing logic +- Keep limits, pipe detection, and invalid-input error shaping consistent across commands + ### Dependencies | Package | Purpose | diff --git a/internal/cli/rm.go b/internal/cli/rm.go index 2b98067..e6c259b 100644 --- a/internal/cli/rm.go +++ b/internal/cli/rm.go @@ -1,8 +1,6 @@ package cli import ( - "bufio" - "encoding/json" "fmt" "os" @@ -52,28 +50,16 @@ OUTPUT // Read IDs from stdin pipe if available const maxBatchIDs = 10000 - stat, err := os.Stdin.Stat() - if err == nil && (stat.Mode()&os.ModeCharDevice) == 0 { - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - line := scanner.Bytes() - if len(line) == 0 { - continue - } - var obj map[string]any // JSONL objects are untyped - if err := json.Unmarshal(line, &obj); err != nil { - continue - } - if id, ok := obj["id"].(string); ok && id != "" { - ids = append(ids, id) - } - if len(ids) > maxBatchIDs { - return fmt.Errorf("%w: too many IDs (max %d)", model.ErrInvalidInput, maxBatchIDs) - } - } - if err := scanner.Err(); err != nil { - return fmt.Errorf("%w: read stdin: %s", model.ErrInvalidInput, err) - } + if len(ids) > maxBatchIDs { + return fmt.Errorf("%w: too many IDs (max %d)", model.ErrInvalidInput, maxBatchIDs) + } + + stdinIDs, err := readOptionalIDsFromJSONLStdin(os.Stdin, maxBatchIDs-len(ids)) + if err != nil { + return err + } + if len(stdinIDs) > 0 { + ids = append(ids, stdinIDs...) } if len(ids) == 0 { diff --git a/internal/cli/stdin.go b/internal/cli/stdin.go index 268a03b..c89db12 100644 --- a/internal/cli/stdin.go +++ b/internal/cli/stdin.go @@ -1,7 +1,9 @@ package cli import ( + "bufio" "encoding/json" + "errors" "fmt" "io" "os" @@ -11,12 +13,20 @@ import ( const maxJSONStdinBytes int64 = 10 << 20 -func readOptionalStdinBytes(stdin *os.File, maxBytes int64) ([]byte, error) { +func hasPipedStdin(stdin *os.File) (bool, error) { stat, err := stdin.Stat() if err != nil { - return nil, fmt.Errorf("inspect stdin: %w", err) + return false, fmt.Errorf("inspect stdin: %w", err) + } + return stat.Mode()&os.ModeCharDevice == 0, nil +} + +func readOptionalStdinBytes(stdin *os.File, maxBytes int64) ([]byte, error) { + hasInput, err := hasPipedStdin(stdin) + if err != nil { + return nil, err } - if stat.Mode()&os.ModeCharDevice != 0 { + if !hasInput { return nil, nil } return readBoundedStdinBytes(stdin, maxBytes) @@ -48,3 +58,48 @@ func readOptionalJSONObjectFromStdin(stdin *os.File) (payload map[string]any, ha } return payload, true, nil } + +func readOptionalIDsFromJSONLStdin(stdin *os.File, maxIDs int) ([]string, error) { + hasInput, err := hasPipedStdin(stdin) + if err != nil { + return nil, err + } + if !hasInput { + return nil, nil + } + + maxLineBytes := int(maxJSONStdinBytes) + scanner := bufio.NewScanner(stdin) + scanner.Buffer(make([]byte, 0, 64*1024), maxLineBytes) + + ids := make([]string, 0) + for scanner.Scan() { + line := scanner.Bytes() + if len(line) == 0 { + continue + } + + var payload struct { + ID string `json:"id"` + } + if err := json.Unmarshal(line, &payload); err != nil { + continue + } + if payload.ID == "" { + continue + } + + if len(ids) >= maxIDs { + return nil, fmt.Errorf("%w: too many IDs (max %d)", model.ErrInvalidInput, maxIDs) + } + ids = append(ids, payload.ID) + } + if err := scanner.Err(); err != nil { + if errors.Is(err, bufio.ErrTooLong) { + return nil, fmt.Errorf("%w: JSONL line exceeds %d MB limit", model.ErrInvalidInput, maxLineBytes/(1<<20)) + } + return nil, fmt.Errorf("%w: read stdin: %s", model.ErrInvalidInput, err) + } + + return ids, nil +} diff --git a/internal/cli/stdin_test.go b/internal/cli/stdin_test.go index a211de5..88d2d14 100644 --- a/internal/cli/stdin_test.go +++ b/internal/cli/stdin_test.go @@ -92,6 +92,51 @@ func TestReadOptionalJSONObjectFromStdinRejectsOversizedInput(t *testing.T) { } } +func TestReadOptionalIDsFromJSONLStdinParsesIDs(t *testing.T) { + file := mustTempInputFile(t, strings.Join([]string{ + `{"id":"019abc","title":"First"}`, + `{"title":"missing id"}`, + `not-json`, + `{"id":"019def"}`, + "", + }, "\n")) + + got, err := readOptionalIDsFromJSONLStdin(file, 10) + if err != nil { + t.Fatalf("read optional JSONL ids: %v", err) + } + if len(got) != 2 { + t.Fatalf("ids = %v, want 2 entries", got) + } + if got[0] != "019abc" || got[1] != "019def" { + t.Fatalf("ids = %v, want [019abc 019def]", got) + } +} + +func TestReadOptionalIDsFromJSONLStdinRejectsOversizedLine(t *testing.T) { + file := mustTempInputFile(t, strings.Repeat("x", int(maxJSONStdinBytes)+1)) + + _, err := readOptionalIDsFromJSONLStdin(file, 10) + if !errors.Is(err, model.ErrInvalidInput) { + t.Fatalf("err = %v, want invalid input", err) + } + if !strings.Contains(err.Error(), "JSONL line exceeds 10 MB limit") { + t.Fatalf("err = %v, want JSONL line limit error", err) + } +} + +func TestReadOptionalIDsFromJSONLStdinRejectsTooManyIDs(t *testing.T) { + file := mustTempInputFile(t, `{"id":"019abc"}`+"\n"+`{"id":"019def"}`) + + _, err := readOptionalIDsFromJSONLStdin(file, 1) + if !errors.Is(err, model.ErrInvalidInput) { + t.Fatalf("err = %v, want invalid input", err) + } + if !strings.Contains(err.Error(), "too many IDs") { + t.Fatalf("err = %v, want too many IDs", err) + } +} + func mustTempInputFile(t *testing.T, content string) *os.File { t.Helper()