Skip to content

Commit

Permalink
only read from stdin when no other files/folders given
Browse files Browse the repository at this point in the history
  • Loading branch information
yannh committed Mar 7, 2021
1 parent b10927a commit 6308d55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 11 additions & 1 deletion acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
}
Expand All @@ -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 ]
Expand Down
16 changes: 9 additions & 7 deletions cmd/kubeconform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 6308d55

Please sign in to comment.