mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
Automatically add "-" when piping and its missing
This commit is contained in:
parent
75960c6484
commit
703418d0c4
@ -16,14 +16,31 @@ testBasicPipeWithDot() {
|
||||
assertEquals "a: 123" "$X"
|
||||
}
|
||||
|
||||
testBasicAllFiles() {
|
||||
testBasicGitHubAction() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
./yq -n ".a = 124" > test2.yml
|
||||
X=$(./yq test.yml test2.yml)
|
||||
Y=$(./yq e '.' test.yml test2.yml)
|
||||
assertEquals "$Y" "$X"
|
||||
X=$(cat /dev/null | ./yq test.yml)
|
||||
assertEquals "a: 123" "$X"
|
||||
|
||||
X=$(cat /dev/null | ./yq e test.yml)
|
||||
assertEquals "a: 123" "$X"
|
||||
|
||||
X=$(cat /dev/null | ./yq ea test.yml)
|
||||
assertEquals "a: 123" "$X"
|
||||
}
|
||||
|
||||
testBasicGitHubActionWithExpression() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
X=$(cat /dev/null | ./yq '.a' test.yml)
|
||||
assertEquals "123" "$X"
|
||||
|
||||
X=$(cat /dev/null | ./yq e '.a' test.yml)
|
||||
assertEquals "123" "$X"
|
||||
|
||||
X=$(cat /dev/null | ./yq ea '.a' test.yml)
|
||||
assertEquals "123" "$X"
|
||||
}
|
||||
|
||||
|
||||
testBasicEvalAllAllFiles() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
./yq -n ".a = 124" > test2.yml
|
||||
@ -32,6 +49,38 @@ testBasicEvalAllAllFiles() {
|
||||
assertEquals "$Y" "$X"
|
||||
}
|
||||
|
||||
testBasicCatWithFilesNoDash() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
./yq -n ".a = 124" > test2.yml
|
||||
X=$(cat test.yml | ./yq test2.yml)
|
||||
Y=$(./yq e '.' test2.yml test.yml)
|
||||
assertEquals "$Y" "$X"
|
||||
}
|
||||
|
||||
testBasicEvalAllCatWithFilesNoDash() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
./yq -n ".a = 124" > test2.yml
|
||||
X=$(cat test.yml | ./yq ea test2.yml)
|
||||
Y=$(./yq e '.' test2.yml test.yml)
|
||||
assertEquals "$Y" "$X"
|
||||
}
|
||||
|
||||
testBasicCatWithFilesNoDashWithExp() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
./yq -n ".a = 124" > test2.yml
|
||||
X=$(cat test.yml | ./yq '.a' test2.yml)
|
||||
Y=$(./yq e '.a' test2.yml test.yml)
|
||||
assertEquals "$Y" "$X"
|
||||
}
|
||||
|
||||
testBasicEvalAllCatWithFilesNoDashWithExp() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
./yq -n ".a = 124" > test2.yml
|
||||
X=$(cat test.yml | ./yq ea '.a' test2.yml)
|
||||
Y=$(./yq e '.a' test2.yml test.yml)
|
||||
assertEquals "$Y" "$X"
|
||||
}
|
||||
|
||||
|
||||
testBasicStdInWithFiles() {
|
||||
./yq -n ".a = 123" > test.yml
|
||||
|
@ -125,21 +125,17 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
|
||||
}
|
||||
|
||||
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
|
||||
|
||||
args = processArgs(pipingStdIn, args)
|
||||
yqlib.GetLogger().Debugf("processed args: %v", args)
|
||||
|
||||
switch len(args) {
|
||||
case 0:
|
||||
if pipingStdIn {
|
||||
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{"-"}, printer, leadingContentPreProcessing, decoder)
|
||||
} else {
|
||||
cmd.Println(cmd.UsageString())
|
||||
return nil
|
||||
}
|
||||
cmd.Println(cmd.UsageString())
|
||||
return nil
|
||||
case 1:
|
||||
if nullInput {
|
||||
err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(args[0]), printer, "")
|
||||
} else if pipingStdIn && args[0] != "-" && !maybeFile(args[0]) {
|
||||
// must have given a single expression and piping input from stdin
|
||||
err = allAtOnceEvaluator.EvaluateFiles(processExpression(args[0]), []string{"-"}, printer, leadingContentPreProcessing, decoder)
|
||||
|
||||
} else {
|
||||
err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
|
||||
}
|
||||
|
@ -125,10 +125,10 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
streamEvaluator := yqlib.NewStreamEvaluator()
|
||||
|
||||
if frontMatter != "" {
|
||||
yqlib.GetLogger().Debug("using front matter handler")
|
||||
frontMatterHandler := yqlib.NewFrontMatterHandler(args[firstFileIndex])
|
||||
err = frontMatterHandler.Split()
|
||||
if err != nil {
|
||||
@ -143,21 +143,16 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
||||
}
|
||||
defer frontMatterHandler.CleanUp()
|
||||
}
|
||||
args = processArgs(pipingStdIn, args)
|
||||
yqlib.GetLogger().Debugf("processed args: %v", args)
|
||||
|
||||
switch len(args) {
|
||||
case 0:
|
||||
if pipingStdIn {
|
||||
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{"-"}, printer, leadingContentPreProcessing, decoder)
|
||||
} else {
|
||||
cmd.Println(cmd.UsageString())
|
||||
return nil
|
||||
}
|
||||
cmd.Println(cmd.UsageString())
|
||||
return nil
|
||||
case 1:
|
||||
if nullInput {
|
||||
err = streamEvaluator.EvaluateNew(processExpression(args[0]), printer, "")
|
||||
} else if pipingStdIn && args[0] != "-" && !maybeFile(args[0]) {
|
||||
// must have given a single expression and piping input from stdin
|
||||
err = streamEvaluator.EvaluateFiles(processExpression(args[0]), []string{"-"}, printer, leadingContentPreProcessing, decoder)
|
||||
} else {
|
||||
err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer, leadingContentPreProcessing, decoder)
|
||||
}
|
||||
|
30
cmd/utils.go
30
cmd/utils.go
@ -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, "-")
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -237,7 +238,14 @@ func documentInput(w *bufio.Writer, s expressionScenario) (string, string) {
|
||||
|
||||
envCommand := ""
|
||||
|
||||
for name, value := range s.environmentVariables {
|
||||
envKeys := make([]string, 0, len(s.environmentVariables))
|
||||
for k := range s.environmentVariables {
|
||||
envKeys = append(envKeys, k)
|
||||
}
|
||||
sort.Strings(envKeys)
|
||||
|
||||
for _, name := range envKeys {
|
||||
value := s.environmentVariables[name]
|
||||
if envCommand == "" {
|
||||
envCommand = fmt.Sprintf("%v=\"%v\" ", name, value)
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user