Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions processor/processdocuments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package processor

import "testing"

// processDocuments backs both ProcessFile and ProcessStdin. Previously stdin
// only ever processed the first document; this verifies the shared path
// collects images from every document in a multi-document stream.
func TestProcessDocumentsMultiple(t *testing.T) {
data := []byte(`
apiVersion: v1
kind: Pod
metadata:
name: first
spec:
containers:
- name: c
image: image-one
---
apiVersion: v1
kind: Pod
metadata:
name: second
spec:
containers:
- name: c
image: image-two
`)

images, err := processDocuments(data)
if err != nil {
t.Fatalf("processDocuments() error = %v", err)
}

expected := []string{"image-one", "image-two"}
if len(images) != len(expected) {
t.Fatalf("expected %d images, got %d: %v", len(expected), len(images), images)
}
for i, img := range images {
if img != expected[i] {
t.Errorf("expected image %q, got %q", expected[i], img)
}
}
}
23 changes: 10 additions & 13 deletions processor/processor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package processor

import (
"bufio"
"bytes"
"fmt"
"io"
Expand All @@ -11,27 +10,25 @@ import (
)

func ProcessStdin() ([]string, error) {
reader := bufio.NewReader(os.Stdin)
var data []byte
for {
line, err := reader.ReadBytes('\n')
if err != nil && err != io.EOF {
return nil, fmt.Errorf("error reading stdin: %v", err)
}
data = append(data, line...)
if err == io.EOF {
break
}
data, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf("error reading stdin: %v", err)
}
return yamlparser.ProcessData(data)
return processDocuments(data)
}

func ProcessFile(filePath string) ([]string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
return processDocuments(data)
}

// processDocuments splits a (possibly multi-document) YAML stream and collects
// the images from every document. Both the file and stdin paths go through it
// so they handle multi-document input identically.
func processDocuments(data []byte) ([]string, error) {
var images []string
docs := bytes.Split(data, []byte("\n---\n"))
for _, doc := range docs {
Expand Down
Loading