only read from stdin when no other files/folders given

This commit is contained in:
Yann Hamon 2021-03-07 23:59:13 +01:00
parent b10927a052
commit 6308d55977
2 changed files with 20 additions and 8 deletions

View file

@ -170,7 +170,7 @@ resetCacheFolder() {
} }
@test "Pass when parsing a valid Kubernetes config YAML file explicitly on stdin" { @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 ] [ "$status" -eq 0 ]
[ "$output" = "Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ] [ "$output" = "Summary: 1 resource found parsing stdin - Valid: 1, Invalid: 0, Errors: 0, Skipped: 0" ]
} }
@ -180,6 +180,16 @@ resetCacheFolder() {
[ "$status" -eq 1 ] [ "$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" { @test "Skip when parsing a resource from a kind to skip" {
run bin/kubeconform -verbose -skip ReplicationController fixtures/valid.yaml run bin/kubeconform -verbose -skip ReplicationController fixtures/valid.yaml
[ "$status" -eq 0 ] [ "$status" -eq 0 ]

View file

@ -68,15 +68,17 @@ func realMain() int {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
// Detect whether we have data being piped through stdin useStdin := false
stat, _ := os.Stdin.Stat() if len(cfg.Files) == 0 || (len(cfg.Files) == 1 && cfg.Files[0] == "-") {
isStdin := (stat.Mode() & os.ModeCharDevice) == 0 stat, _ := os.Stdin.Stat()
if len(cfg.Files) == 1 && cfg.Files[0] == "-" { if (stat.Mode() & os.ModeCharDevice) != 0 {
isStdin = true log.Fatalf("failing to read data from stdin")
}
useStdin = true
} }
var o output.Output 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) fmt.Fprintln(os.Stderr, err)
return 1 return 1
} }
@ -101,7 +103,7 @@ func realMain() int {
var resourcesChan <-chan resource.Resource var resourcesChan <-chan resource.Resource
var errors <-chan error var errors <-chan error
if isStdin { if useStdin {
resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin) resourcesChan, errors = resource.FromStream(ctx, "stdin", os.Stdin)
} else { } else {
resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns) resourcesChan, errors = resource.FromFiles(ctx, cfg.Files, cfg.IgnoreFilenamePatterns)