Skip to content

Commit 19441d8

Browse files
authored
Merge pull request #42 from yannh/fix-stdin-behaviour
Only read from stdin when no other files/folders given
2 parents b10927a + 6308d55 commit 19441d8

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

acceptance.bats

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ resetCacheFolder() {
170170
}
171171

172172
@test "Pass when parsing a valid Kubernetes config YAML file explicitly on stdin" {
173-
run bash -c "cat fixtures/valid.yaml | bin/kubeconform -summary"
173+
run bash -c "cat fixtures/valid.yaml | bin/kubeconform -summary -"
174174
[ "$status" -eq 0 ]
175175
[ "$output" = "Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ]
176176
}
@@ -180,6 +180,16 @@ resetCacheFolder() {
180180
[ "$status" -eq 1 ]
181181
}
182182

183+
@test "Fail when not passing data to stdin, when implicitly configured to read from stdin" {
184+
run bash -c "bin/kubeconform -summary"
185+
[ "$status" -eq 1 ]
186+
}
187+
188+
@test "Fail when not passing data to stdin, when explicitly configured to read from stdin" {
189+
run bash -c "bin/kubeconform -summary -"
190+
[ "$status" -eq 1 ]
191+
}
192+
183193
@test "Skip when parsing a resource from a kind to skip" {
184194
run bin/kubeconform -verbose -skip ReplicationController fixtures/valid.yaml
185195
[ "$status" -eq 0 ]

cmd/kubeconform/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ func realMain() int {
6868
defer pprof.StopCPUProfile()
6969
}
7070

71-
// Detect whether we have data being piped through stdin
72-
stat, _ := os.Stdin.Stat()
73-
isStdin := (stat.Mode() & os.ModeCharDevice) == 0
74-
if len(cfg.Files) == 1 && cfg.Files[0] == "-" {
75-
isStdin = true
71+
useStdin := false
72+
if len(cfg.Files) == 0 || (len(cfg.Files) == 1 && cfg.Files[0] == "-") {
73+
stat, _ := os.Stdin.Stat()
74+
if (stat.Mode() & os.ModeCharDevice) != 0 {
75+
log.Fatalf("failing to read data from stdin")
76+
}
77+
useStdin = true
7678
}
7779

7880
var o output.Output
79-
if o, err = output.New(cfg.OutputFormat, cfg.Summary, isStdin, cfg.Verbose); err != nil {
81+
if o, err = output.New(cfg.OutputFormat, cfg.Summary, useStdin, cfg.Verbose); err != nil {
8082
fmt.Fprintln(os.Stderr, err)
8183
return 1
8284
}
@@ -101,7 +103,7 @@ func realMain() int {
101103

102104
var resourcesChan <-chan resource.Resource
103105
var errors <-chan error
104-
if isStdin {
106+
if useStdin {
105107
resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin)
106108
} else {
107109
resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns)

0 commit comments

Comments
 (0)