From bf2231aee072ed6130f4102b7f024ffda0b0a981 Mon Sep 17 00:00:00 2001 From: Tomasz Janiszewski Date: Sat, 27 Jan 2024 09:55:22 +0100 Subject: [PATCH] bug: broken reading from stdin (-) (#704) Signed-off-by: Tomasz Janiszewski --- e2etests/bats-tests.sh | 4 ++++ pkg/command/lint/command.go | 6 +++++- pkg/lintcontext/create_contexts.go | 10 ++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/e2etests/bats-tests.sh b/e2etests/bats-tests.sh index cbf54700d..87efe7c8f 100755 --- a/e2etests/bats-tests.sh +++ b/e2etests/bats-tests.sh @@ -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 - +} diff --git a/pkg/command/lint/command.go b/pkg/command/lint/command.go index 56c79694d..634b79cd6 100644 --- a/pkg/command/lint/command.go +++ b/pkg/command/lint/command.go @@ -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 diff --git a/pkg/lintcontext/create_contexts.go b/pkg/lintcontext/create_contexts.go index 64b9b32d9..6dd888f21 100644 --- a/pkg/lintcontext/create_contexts.go +++ b/pkg/lintcontext/create_contexts.go @@ -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") ) @@ -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("", os.Stdin); err != nil { return nil, err } - contextsByDir["-"] = ctx + contextsByDir[ReadFromStdin] = ctx continue }