Fixed linting

This commit is contained in:
Mike Farah 2020-11-13 14:07:11 +11:00
parent f8a700e400
commit af39fc737d
18 changed files with 135 additions and 101 deletions

View File

@ -21,3 +21,5 @@ ARG VERSION=none
LABEL version=${VERSION} LABEL version=${VERSION}
WORKDIR /workdir WORKDIR /workdir
ENTRYPOINT [/usr/bin/yq]

View File

@ -1,13 +1,11 @@
package cmd package cmd
import (
logging "gopkg.in/op/go-logging.v1"
)
var unwrapScalar = true var unwrapScalar = true
var writeInplace = false
// var writeInplace = false
var outputToJSON = false var outputToJSON = false
var exitStatus = false
// var exitStatus = false
var forceColor = false var forceColor = false
var forceNoColor = false var forceNoColor = false
var colorsEnabled = false var colorsEnabled = false
@ -17,4 +15,5 @@ var nullInput = false
var verbose = false var verbose = false
var version = false var version = false
var shellCompletion = "" var shellCompletion = ""
var log = logging.MustGetLogger("yq")
// var log = logging.MustGetLogger("yq")

View File

@ -11,13 +11,13 @@ func createEvaluateAllCommand() *cobra.Command {
var cmdEvalAll = &cobra.Command{ var cmdEvalAll = &cobra.Command{
Use: "eval-all [expression] [yaml_file1]...", Use: "eval-all [expression] [yaml_file1]...",
Aliases: []string{"ea"}, Aliases: []string{"ea"},
Short: "Loads all yaml documents of all yaml files and runs expression once", Short: "Loads _all_ yaml documents of _all_ yaml files and runs expression once",
Example: ` Example: `
yq es '.a.b | length' file1.yml file2.yml yq es '.a.b | length' file1.yml file2.yml
yq es < sample.yaml yq es < sample.yaml
yq es -n '{"a": "b"}' yq es -n '{"a": "b"}'
`, `,
Long: "Evaluate All:\nUseful when you need to run an expression across several yaml documents or files. Consumes more memory than eval-seq", Long: "Evaluate All:\nUseful when you need to run an expression across several yaml documents or files. Consumes more memory than eval",
RunE: evaluateAll, RunE: evaluateAll,
} }
return cmdEvalAll return cmdEvalAll
@ -56,7 +56,7 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
err = yqlib.EvaluateAllFileStreams("", []string{args[0]}, printer) err = yqlib.EvaluateAllFileStreams("", []string{args[0]}, printer)
} }
default: default:
err = yqlib.EvaluateAllFileStreams(args[0], args[1:len(args)], printer) err = yqlib.EvaluateAllFileStreams(args[0], args[1:], printer)
} }
cmd.SilenceUsage = true cmd.SilenceUsage = true

View File

@ -9,8 +9,8 @@ import (
func createEvaluateSequenceCommand() *cobra.Command { func createEvaluateSequenceCommand() *cobra.Command {
var cmdEvalSequence = &cobra.Command{ var cmdEvalSequence = &cobra.Command{
Use: "eval-seq [expression] [yaml_file1]...", Use: "eval [expression] [yaml_file1]...",
Aliases: []string{"es"}, Aliases: []string{"e"},
Short: "Apply expression to each document in each yaml file given in sequence", Short: "Apply expression to each document in each yaml file given in sequence",
Example: ` Example: `
yq es '.a.b | length' file1.yml file2.yml yq es '.a.b | length' file1.yml file2.yml
@ -56,7 +56,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
err = yqlib.EvaluateFileStreamsSequence("", []string{args[0]}, printer) err = yqlib.EvaluateFileStreamsSequence("", []string{args[0]}, printer)
} }
default: default:
err = yqlib.EvaluateFileStreamsSequence(args[0], args[1:len(args)], printer) err = yqlib.EvaluateFileStreamsSequence(args[0], args[1:], printer)
} }
cmd.SilenceUsage = true cmd.SilenceUsage = true

4
go.mod
View File

@ -5,14 +5,12 @@ require (
github.com/fatih/color v1.9.0 github.com/fatih/color v1.9.0
github.com/goccy/go-yaml v1.8.1 github.com/goccy/go-yaml v1.8.1
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.7 // indirect github.com/mattn/go-colorable v0.1.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/timtadh/data-structures v0.5.3 // indirect github.com/timtadh/data-structures v0.5.3 // indirect
github.com/timtadh/lexmachine v0.2.2 github.com/timtadh/lexmachine v0.2.2
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f // indirect golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776

15
go.sum
View File

@ -15,6 +15,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
@ -62,8 +63,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
@ -77,18 +76,13 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mikefarah/yq v1.15.0 h1:ViMYNRG5UB7hzm8olxMFqPtkpMXXKO4g32/v9JUa62o=
github.com/mikefarah/yq v2.4.0+incompatible h1:oBxbWy8R9hI3BIUUxEf0CzikWa2AgnGrGhvGQt5jgjk=
github.com/mikefarah/yq/v3 v3.0.0-20201020025845-ccb718cd0f59 h1:6nvF+EEFIVD4KT64CgvAzWaMVC283Huno59khWs9r1A=
github.com/mikefarah/yq/v3 v3.0.0-20201020025845-ccb718cd0f59/go.mod h1:7eVjFf5bgozMuHk+oKpyxR2zCN3iEN1tF0/bM5jvtKo=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
@ -118,6 +112,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/timtadh/data-structures v0.5.3 h1:F2tEjoG9qWIyUjbvXVgJqEOGJPMIiYn7U5W5mE+i/vQ= github.com/timtadh/data-structures v0.5.3 h1:F2tEjoG9qWIyUjbvXVgJqEOGJPMIiYn7U5W5mE+i/vQ=
github.com/timtadh/data-structures v0.5.3/go.mod h1:9R4XODhJ8JdWFEI8P/HJKqxuJctfBQw6fDibMQny2oU= github.com/timtadh/data-structures v0.5.3/go.mod h1:9R4XODhJ8JdWFEI8P/HJKqxuJctfBQw6fDibMQny2oU=
@ -156,8 +151,8 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f h1:Fqb3ao1hUmOR3GkUOg/Y+BadLwykBIzs5q8Ez2SbHyc= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

View File

@ -20,10 +20,13 @@ func (n *CandidateNode) GetKey() string {
return fmt.Sprintf("%v - %v", n.Document, n.Path) return fmt.Sprintf("%v - %v", n.Document, n.Path)
} }
func (n *CandidateNode) Copy() *CandidateNode { func (n *CandidateNode) Copy() (*CandidateNode, error) {
clone := &CandidateNode{} clone := &CandidateNode{}
copier.Copy(clone, n) err := copier.Copy(clone, n)
return clone if err != nil {
return nil, err
}
return clone, nil
} }
// updates this candidate from the given candidate node // updates this candidate from the given candidate node

View File

@ -83,10 +83,14 @@ func collect(d *dataTreeNavigator, aggregate *list.List, remainingMatches *list.
aggCandidate := el.Value.(*CandidateNode) aggCandidate := el.Value.(*CandidateNode)
for splatEl := splatted.Front(); splatEl != nil; splatEl = splatEl.Next() { for splatEl := splatted.Front(); splatEl != nil; splatEl = splatEl.Next() {
splatCandidate := splatEl.Value.(*CandidateNode) splatCandidate := splatEl.Value.(*CandidateNode)
newCandidate := aggCandidate.Copy() newCandidate, err := aggCandidate.Copy()
if err != nil {
return nil, err
}
newCandidate.Path = nil newCandidate.Path = nil
newCandidate, err := multiply(d, newCandidate, splatCandidate) newCandidate, err = multiply(d, newCandidate, splatCandidate)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,7 +18,10 @@ func ExplodeOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTr
return nil, err return nil, err
} }
for childEl := rhs.Front(); childEl != nil; childEl = childEl.Next() { for childEl := rhs.Front(); childEl != nil; childEl = childEl.Next() {
explodeNode(childEl.Value.(*CandidateNode).Node) err = explodeNode(childEl.Value.(*CandidateNode).Node)
if err != nil {
return nil, err
}
} }
} }
@ -65,11 +68,17 @@ func explodeNode(node *yaml.Node) error {
log.Debugf("an alias merge list!") log.Debugf("an alias merge list!")
for index := 0; index < len(valueNode.Content); index = index + 1 { for index := 0; index < len(valueNode.Content); index = index + 1 {
aliasNode := valueNode.Content[index] aliasNode := valueNode.Content[index]
applyAlias(node, aliasNode.Alias, index, newContent) err := applyAlias(node, aliasNode.Alias, index, newContent)
if err != nil {
return err
}
} }
} else { } else {
log.Debugf("an alias merge!") log.Debugf("an alias merge!")
applyAlias(node, valueNode.Alias, index, newContent) err := applyAlias(node, valueNode.Alias, index, newContent)
if err != nil {
return err
}
} }
} }
} }

View File

@ -72,7 +72,10 @@ func multiply(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*Ca
func mergeObjects(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { func mergeObjects(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
var results = list.New() var results = list.New()
recursiveDecent(d, results, nodeToMap(rhs)) err := recursiveDecent(d, results, nodeToMap(rhs))
if err != nil {
return nil, err
}
var pathIndexToStartFrom int = 0 var pathIndexToStartFrom int = 0
if results.Front() != nil { if results.Front() != nil {

View File

@ -136,7 +136,10 @@ func traverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode, op
//skip the 'merge' tag, find a direct match first //skip the 'merge' tag, find a direct match first
if key.Tag == "!!merge" && followAlias { if key.Tag == "!!merge" && followAlias {
log.Debug("Merge anchor") log.Debug("Merge anchor")
traverseMergeAnchor(newMatches, candidate, value, operation) err := traverseMergeAnchor(newMatches, candidate, value, operation)
if err != nil {
return err
}
} else if keyMatches(key, operation) { } else if keyMatches(key, operation) {
log.Debug("MATCHED") log.Debug("MATCHED")
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
@ -151,7 +154,7 @@ func traverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode, op
return nil return nil
} }
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, operation *Operation) { func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, operation *Operation) error {
switch value.Kind { switch value.Kind {
case yaml.AliasNode: case yaml.AliasNode:
candidateNode := &CandidateNode{ candidateNode := &CandidateNode{
@ -159,13 +162,16 @@ func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *C
Path: originalCandidate.Path, Path: originalCandidate.Path,
Document: originalCandidate.Document, Document: originalCandidate.Document,
} }
traverseMap(newMatches, candidateNode, operation) return traverseMap(newMatches, candidateNode, operation)
case yaml.SequenceNode: case yaml.SequenceNode:
for _, childValue := range value.Content { for _, childValue := range value.Content {
traverseMergeAnchor(newMatches, originalCandidate, childValue, operation) err := traverseMergeAnchor(newMatches, originalCandidate, childValue, operation)
if err != nil {
return err
}
} }
} }
return return nil
} }
func traverseArray(candidate *CandidateNode, operation *Operation) ([]*CandidateNode, error) { func traverseArray(candidate *CandidateNode, operation *Operation) ([]*CandidateNode, error) {

View File

@ -48,6 +48,13 @@ func testScenario(t *testing.T, s *expressionScenario) {
test.AssertResultComplexWithContext(t, s.expected, resultsToString(results), fmt.Sprintf("exp: %v\ndoc: %v", s.expression, s.document)) test.AssertResultComplexWithContext(t, s.expected, resultsToString(results), fmt.Sprintf("exp: %v\ndoc: %v", s.expression, s.document))
} }
func writeOrPanic(w *bufio.Writer, text string) {
_, err := w.WriteString(text)
if err != nil {
panic(err)
}
}
func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) { func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) {
f, err := os.Create(fmt.Sprintf("doc/%v.md", title)) f, err := os.Create(fmt.Sprintf("doc/%v.md", title))
@ -56,27 +63,27 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
} }
defer f.Close() defer f.Close()
w := bufio.NewWriter(f) w := bufio.NewWriter(f)
w.WriteString(fmt.Sprintf("# %v\n", title)) writeOrPanic(w, fmt.Sprintf("# %v\n", title))
w.WriteString(fmt.Sprintf("## Examples\n")) writeOrPanic(w, "## Examples\n")
for index, s := range scenarios { for index, s := range scenarios {
if !s.skipDoc { if !s.skipDoc {
if s.description != "" { if s.description != "" {
w.WriteString(fmt.Sprintf("### %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("### %v\n", s.description))
} else { } else {
w.WriteString(fmt.Sprintf("### Example %v\n", index)) writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
} }
if s.document != "" { if s.document != "" {
w.WriteString(fmt.Sprintf("sample.yml:\n")) writeOrPanic(w, "sample.yml:\n")
w.WriteString(fmt.Sprintf("```yaml\n%v\n```\n", s.document)) writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.document))
} }
if s.expression != "" { if s.expression != "" {
w.WriteString(fmt.Sprintf("Expression\n")) writeOrPanic(w, "Expression\n")
w.WriteString(fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression)) writeOrPanic(w, fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression))
} }
w.WriteString(fmt.Sprintf("Result\n")) writeOrPanic(w, "Result\n")
var output bytes.Buffer var output bytes.Buffer
var err error var err error
@ -88,15 +95,18 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
t.Error(err) t.Error(err)
} }
err = EvaluateStream("sample.yaml", strings.NewReader(s.document), node, printer) err = EvaluateStream("sample.yaml", strings.NewReader(s.document), node, printer)
if err != nil {
t.Error(err)
}
} else { } else {
err = EvaluateAllFileStreams(s.expression, []string{}, printer) err = EvaluateAllFileStreams(s.expression, []string{}, printer)
if err != nil {
t.Error(err)
}
} }
w.WriteString(fmt.Sprintf("```yaml\n%v```\n", output.String())) writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", output.String()))
if err != nil {
t.Error(err)
}
} }
} }

View File

@ -59,7 +59,7 @@ func GetStyleOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *
for el := matchingNodes.Front(); el != nil; el = el.Next() { for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
var style = "" var style string
switch candidate.Node.Style { switch candidate.Node.Style {
case yaml.TaggedStyle: case yaml.TaggedStyle:
style = "tagged" style = "tagged"

View File

@ -1,7 +1,6 @@
package yqlib package yqlib
import ( import (
"fmt"
"strconv" "strconv"
lex "github.com/timtadh/lexmachine" lex "github.com/timtadh/lexmachine"
@ -47,14 +46,14 @@ func (t *Token) toString() string {
} else if t.TokenType == CloseCollectObject { } else if t.TokenType == CloseCollectObject {
return "}" return "}"
} else { } else {
return fmt.Sprintf("NFI") return "NFI"
} }
} }
func pathToken(wrapped bool) lex.Action { func pathToken(wrapped bool) lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) { return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
value := string(m.Bytes) value := string(m.Bytes)
value = value[1:len(value)] value = value[1:]
if wrapped { if wrapped {
value = unwrap(value) value = unwrap(value)
} }
@ -73,7 +72,7 @@ func literalPathToken(value string) lex.Action {
func documentToken() lex.Action { func documentToken() lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) { return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
var numberString = string(m.Bytes) var numberString = string(m.Bytes)
numberString = numberString[1:len(numberString)] numberString = numberString[1:]
var number, errParsingInt = strconv.ParseInt(numberString, 10, 64) // nolint var number, errParsingInt = strconv.ParseInt(numberString, 10, 64) // nolint
if errParsingInt != nil { if errParsingInt != nil {
return nil, errParsingInt return nil, errParsingInt

View File

@ -7,8 +7,8 @@ var myPathPostfixer = NewPathPostFixer()
type PathTreeNode struct { type PathTreeNode struct {
Operation *Operation Operation *Operation
Lhs *PathTreeNode Lhs *PathTreeNode
Rhs *PathTreeNode Rhs *PathTreeNode
} }
type PathTreeCreator interface { type PathTreeCreator interface {

View File

@ -77,7 +77,10 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
mappedDoc := el.Value.(*CandidateNode) mappedDoc := el.Value.(*CandidateNode)
if (!p.firstTimePrinting || (previousDocIndex != mappedDoc.Document)) && p.printDocSeparators { if (!p.firstTimePrinting || (previousDocIndex != mappedDoc.Document)) && p.printDocSeparators {
p.writeString(bufferedWriter, "---\n") if err := p.writeString(bufferedWriter, "---\n"); err != nil {
return err
}
} }
if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil { if err := p.printNode(mappedDoc.Node, bufferedWriter); err != nil {

View File

@ -46,7 +46,10 @@ func EvaluateStream(filename string, reader io.Reader, node *PathTreeNode, print
if errorParsing != nil { if errorParsing != nil {
return errorParsing return errorParsing
} }
printer.PrintResults(matches) err := printer.PrintResults(matches)
if err != nil {
return err
}
currentIndex = currentIndex + 1 currentIndex = currentIndex + 1
} }
} }
@ -61,9 +64,9 @@ func readDocuments(reader io.Reader, filename string) (*list.List, error) {
errorReading := decoder.Decode(&dataBucket) errorReading := decoder.Decode(&dataBucket)
if errorReading == io.EOF { if errorReading == io.EOF {
switch reader.(type) { switch reader := reader.(type) {
case *os.File: case *os.File:
safelyCloseFile(reader.(*os.File)) safelyCloseFile(reader)
} }
return inputList, nil return inputList, nil
} else if errorReading != nil { } else if errorReading != nil {
@ -122,49 +125,49 @@ func EvaluateFileStreamsSequence(expression string, filenames []string, printer
return err return err
} }
switch reader.(type) { switch reader := reader.(type) {
case *os.File: case *os.File:
safelyCloseFile(reader.(*os.File)) safelyCloseFile(reader)
} }
} }
return nil return nil
} }
func safelyRenameFile(from string, to string) { // func safelyRenameFile(from string, to string) {
if renameError := os.Rename(from, to); renameError != nil { // if renameError := os.Rename(from, to); renameError != nil {
log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to) // log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)
log.Debug(renameError.Error()) // log.Debug(renameError.Error())
// can't do this rename when running in docker to a file targeted in a mounted volume, // // can't do this rename when running in docker to a file targeted in a mounted volume,
// so gracefully degrade to copying the entire contents. // // so gracefully degrade to copying the entire contents.
if copyError := copyFileContents(from, to); copyError != nil { // if copyError := copyFileContents(from, to); copyError != nil {
log.Errorf("Failed copying from %v to %v", from, to) // log.Errorf("Failed copying from %v to %v", from, to)
log.Error(copyError.Error()) // log.Error(copyError.Error())
} else { // } else {
removeErr := os.Remove(from) // removeErr := os.Remove(from)
if removeErr != nil { // if removeErr != nil {
log.Errorf("failed removing original file: %s", from) // log.Errorf("failed removing original file: %s", from)
} // }
} // }
} // }
} // }
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang // // thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
func copyFileContents(src, dst string) (err error) { // func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src) // nolint gosec // in, err := os.Open(src) // nolint gosec
if err != nil { // if err != nil {
return err // return err
} // }
defer safelyCloseFile(in) // defer safelyCloseFile(in)
out, err := os.Create(dst) // out, err := os.Create(dst)
if err != nil { // if err != nil {
return err // return err
} // }
defer safelyCloseFile(out) // defer safelyCloseFile(out)
if _, err = io.Copy(out, in); err != nil { // if _, err = io.Copy(out, in); err != nil {
return err // return err
} // }
return out.Sync() // return out.Sync()
} // }
func safelyFlush(writer *bufio.Writer) { func safelyFlush(writer *bufio.Writer) {
if err := writer.Flush(); err != nil { if err := writer.Flush(); err != nil {

View File

@ -3,7 +3,7 @@
set -e set -e
# acceptance test # acceptance test
X=$(./yq w ./examples/sample.yaml b.c 3 | ./yq r - b.c) X=$(./yq e '.b.c |= 3' ./examples/sample.yaml | ./yq e '.b.c' -)
if [[ $X != 3 ]]; then if [[ $X != 3 ]]; then
echo "Failed acceptance test: expected 3 but was $X" echo "Failed acceptance test: expected 3 but was $X"