diff --git a/acceptance.bats b/acceptance.bats index f0a6c24..e310a9e 100755 --- a/acceptance.bats +++ b/acceptance.bats @@ -170,7 +170,7 @@ resetCacheFolder() { } @test "Pass when parsing a valid Kubernetes config YAML file explicitly on stdin" { - run bash -c "cat fixtures/valid.yaml | bin/kubeconform -summary" + run bash -c "cat fixtures/valid.yaml | bin/kubeconform -summary -" [ "$status" -eq 0 ] [ "$output" = "Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] } @@ -180,6 +180,16 @@ resetCacheFolder() { [ "$status" -eq 1 ] } +@test "Fail when not passing data to stdin, when implicitly configured to read from stdin" { + run bash -c "bin/kubeconform -summary" + [ "$status" -eq 1 ] +} + +@test "Fail when not passing data to stdin, when explicitly configured to read from stdin" { + run bash -c "bin/kubeconform -summary -" + [ "$status" -eq 1 ] +} + @test "Skip when parsing a resource from a kind to skip" { run bin/kubeconform -verbose -skip ReplicationController fixtures/valid.yaml [ "$status" -eq 0 ] diff --git a/cmd/kubeconform/main.go b/cmd/kubeconform/main.go index 8d5c7ad..0273682 100644 --- a/cmd/kubeconform/main.go +++ b/cmd/kubeconform/main.go @@ -68,15 +68,17 @@ func realMain() int { defer pprof.StopCPUProfile() } - // Detect whether we have data being piped through stdin - stat, _ := os.Stdin.Stat() - isStdin := (stat.Mode() & os.ModeCharDevice) == 0 - if len(cfg.Files) == 1 && cfg.Files[0] == "-" { - isStdin = true + useStdin := false + if len(cfg.Files) == 0 || (len(cfg.Files) == 1 && cfg.Files[0] == "-") { + stat, _ := os.Stdin.Stat() + if (stat.Mode() & os.ModeCharDevice) != 0 { + log.Fatalf("failing to read data from stdin") + } + useStdin = true } var o output.Output - if o, err = output.New(cfg.OutputFormat, cfg.Summary, isStdin, cfg.Verbose); err != nil { + if o, err = output.New(cfg.OutputFormat, cfg.Summary, useStdin, cfg.Verbose); err != nil { fmt.Fprintln(os.Stderr, err) return 1 } @@ -101,7 +103,7 @@ func realMain() int { var resourcesChan <-chan resource.Resource var errors <-chan error - if isStdin { + if useStdin { resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin) } else { resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns)