Skip to content

Commit

Permalink
bug: broken reading from stdin (-) (#704)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>
  • Loading branch information
janisz authored Jan 27, 2024
1 parent b9cd03d commit bf2231a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions e2etests/bats-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,7 @@ get_value_from() {
print_info "${status}" "${output}" "${cmd}" "${tmp}"
[ "$status" -eq 0 ]
}

@test "flag-read-from-stdin" {
echo "---" | ${KUBE_LINTER_BIN} lint -
}
6 changes: 5 additions & 1 deletion pkg/command/lint/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ func Command() *cobra.Command {
return err
}

absArgs := []string{}
absArgs := make([]string, 0, len(args))
for _, arg := range args {
if arg == lintcontext.ReadFromStdin {
absArgs = append(absArgs, lintcontext.ReadFromStdin)
continue
}
absArg, err := pathutil.GetAbsolutPath(arg)
if err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/lintcontext/create_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"
)

// ReadFromStdin is a path used to indicate reading from os.Stdin
const ReadFromStdin = "-"

var (
knownYAMLExtensions = set.NewFrozenStringSet(".yaml", ".yml")
)
Expand All @@ -41,16 +44,15 @@ func CreateContextsWithOptions(options Options, ignorePaths []string, filesOrDir
contextsByDir := make(map[string]*lintContextImpl)
fileOrDirsLoop:
for _, fileOrDir := range filesOrDirs {
// Stdin
if fileOrDir == "-" {
if _, alreadyExists := contextsByDir["-"]; alreadyExists {
if fileOrDir == ReadFromStdin {
if _, alreadyExists := contextsByDir[ReadFromStdin]; alreadyExists {
continue
}
ctx := newCtx(options)
if err := ctx.loadObjectsFromReader("<standard input>", os.Stdin); err != nil {
return nil, err
}
contextsByDir["-"] = ctx
contextsByDir[ReadFromStdin] = ctx
continue
}

Expand Down

0 comments on commit bf2231a

Please sign in to comment.