Automatically add "-" when piping and its missing

This commit is contained in:
Mike Farah
2022-02-07 09:09:11 +11:00
parent 75960c6484
commit 703418d0c4
5 changed files with 103 additions and 27 deletions
+29 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/mikefarah/yq/v4/pkg/yqlib"
"github.com/spf13/cobra"
"gopkg.in/op/go-logging.v1"
)
func initCommand(cmd *cobra.Command, args []string) (firstFileIndex int, err error) {
@@ -98,6 +99,33 @@ func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder {
// without this - yq detects there is stdin (thanks githubactions),
// then tries to parse the filename as an expression
func maybeFile(str string) bool {
yqlib.GetLogger().Debugf("checking '%v' is a file", str)
stat, err := os.Stat(str) // #nosec
return err == nil && !stat.IsDir()
result := err == nil && !stat.IsDir()
if yqlib.GetLogger().IsEnabledFor(logging.DEBUG) {
if err != nil {
yqlib.GetLogger().Debugf("error: %v", err)
} else {
yqlib.GetLogger().Debugf("error: %v, dir: %v", err, stat.IsDir())
}
yqlib.GetLogger().Debugf("result: %v", result)
}
return result
}
func processArgs(pipingStdin bool, args []string) []string {
if !pipingStdin {
return args
}
for _, arg := range args {
if arg == "-" {
return args
}
}
yqlib.GetLogger().Debugf("missing '-', adding it to the end")
// we're piping from stdin, but there's no '-' arg
// lets add one to the end
return append(args, "-")
}