From 2889d3e3e6b3836377f46972fac2ddbea8d9471f Mon Sep 17 00:00:00 2001 From: Laszlo Fogas Date: Thu, 14 Nov 2019 20:16:03 +0100 Subject: [PATCH] Migrating to yaml.v3 --- .../pipeline/frontend/yaml/compiler/params.go | 2 +- .../pipeline/pipeline/frontend/yaml/config.go | 2 +- .../pipeline/frontend/yaml/config_test.go | 18 +++++++++++- .../pipeline/frontend/yaml/constraint.go | 14 +++++++-- .../pipeline/frontend/yaml/constraint_test.go | 2 +- .../pipeline/frontend/yaml/container.go | 29 ++++++++++--------- .../pipeline/frontend/yaml/container_test.go | 14 ++------- .../pipeline/frontend/yaml/matrix/matrix.go | 2 +- .../pipeline/frontend/yaml/network.go | 22 ++++---------- .../pipeline/frontend/yaml/network_test.go | 2 +- .../pipeline/pipeline/frontend/yaml/secret.go | 10 +++++-- .../pipeline/frontend/yaml/secret_test.go | 2 +- .../pipeline/frontend/yaml/types/bool.go | 20 ++++++++----- .../pipeline/frontend/yaml/types/bool_test.go | 4 +-- .../pipeline/pipeline/frontend/yaml/volume.go | 21 +++++--------- .../pipeline/frontend/yaml/volume_test.go | 2 +- go.mod | 2 +- go.sum | 4 +-- 18 files changed, 92 insertions(+), 80 deletions(-) diff --git a/cncd/pipeline/pipeline/frontend/yaml/compiler/params.go b/cncd/pipeline/pipeline/frontend/yaml/compiler/params.go index 163f0a48d..f823a68b0 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/compiler/params.go +++ b/cncd/pipeline/pipeline/frontend/yaml/compiler/params.go @@ -7,7 +7,7 @@ import ( "strings" json "github.com/ghodss/yaml" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) // paramsToEnv uses reflection to convert a map[string]interface to a list diff --git a/cncd/pipeline/pipeline/frontend/yaml/config.go b/cncd/pipeline/pipeline/frontend/yaml/config.go index 1f7aab43d..6879d3eaf 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/config.go +++ b/cncd/pipeline/pipeline/frontend/yaml/config.go @@ -6,7 +6,7 @@ import ( "os" libcompose "github.com/docker/libcompose/yaml" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) type ( diff --git a/cncd/pipeline/pipeline/frontend/yaml/config_test.go b/cncd/pipeline/pipeline/frontend/yaml/config_test.go index 3f9168fc1..86fb5b77a 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/config_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/config_test.go @@ -45,7 +45,15 @@ func TestParse(t *testing.T) { g.Assert(out.SkipClone).Equal(false) }) - // Check to make sure variable expansion works in yaml.MapSlice + g.It("Should handle simple yaml anchors", func() { + out, err := ParseString(simpleYamlAnchors) + if err != nil { + g.Fail(err) + } + g.Assert(out.Pipeline.Containers[0].Name).Equal("notify_success") + g.Assert(out.Pipeline.Containers[0].Image).Equal("plugins/slack") + }) + g.It("Should unmarshal variables", func() { out, err := ParseString(sampleVarYaml) if err != nil { @@ -108,6 +116,14 @@ runs_on: - failure ` +var simpleYamlAnchors = ` +vars: + image: &image plugins/slack +pipeline: + notify_success: + image: *image +` + var sampleVarYaml = ` _slack: &SLACK image: plugins/slack diff --git a/cncd/pipeline/pipeline/frontend/yaml/constraint.go b/cncd/pipeline/pipeline/frontend/yaml/constraint.go index cee0ca891..f6a46ee0a 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/constraint.go +++ b/cncd/pipeline/pipeline/frontend/yaml/constraint.go @@ -1,11 +1,13 @@ package yaml import ( + "fmt" "path/filepath" libcompose "github.com/docker/libcompose/yaml" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend" "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend/yaml/types" + "gopkg.in/yaml.v3" ) type ( @@ -85,7 +87,7 @@ func (c *Constraint) Excludes(v string) bool { } // UnmarshalYAML unmarshals the constraint. -func (c *Constraint) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (c *Constraint) UnmarshalYAML(value *yaml.Node) error { var out1 = struct { Include libcompose.Stringorslice Exclude libcompose.Stringorslice @@ -93,14 +95,20 @@ func (c *Constraint) UnmarshalYAML(unmarshal func(interface{}) error) error { var out2 libcompose.Stringorslice - unmarshal(&out1) - unmarshal(&out2) + err1 := value.Decode(&out1) + err2 := value.Decode(&out2) c.Exclude = out1.Exclude c.Include = append( out1.Include, out2..., ) + + if err1 != nil && err2 != nil { + y, _ := yaml.Marshal(value) + return fmt.Errorf("Could not parse condition: %s", y) + } + return nil } diff --git a/cncd/pipeline/pipeline/frontend/yaml/constraint_test.go b/cncd/pipeline/pipeline/frontend/yaml/constraint_test.go index 93bb5e9ba..77cfef800 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/constraint_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/constraint_test.go @@ -5,7 +5,7 @@ import ( "github.com/laszlocph/woodpecker/cncd/pipeline/pipeline/frontend" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) func TestConstraint(t *testing.T) { diff --git a/cncd/pipeline/pipeline/frontend/yaml/container.go b/cncd/pipeline/pipeline/frontend/yaml/container.go index 88c9029f9..6fb6b4ea2 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/container.go +++ b/cncd/pipeline/pipeline/frontend/yaml/container.go @@ -4,7 +4,7 @@ import ( "fmt" libcompose "github.com/docker/libcompose/yaml" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) type ( @@ -62,23 +62,26 @@ type ( ) // UnmarshalYAML implements the Unmarshaller interface. -func (c *Containers) UnmarshalYAML(unmarshal func(interface{}) error) error { - slice := yaml.MapSlice{} - if err := unmarshal(&slice); err != nil { +func (c *Containers) UnmarshalYAML(value *yaml.Node) error { + containers := map[string]Container{} + err := value.Decode(&containers) + if err != nil { return err } - for _, s := range slice { - container := Container{} - out, _ := yaml.Marshal(s.Value) + for i, n := range value.Content { + if i%2 == 1 { + container := Container{} + err := n.Decode(&container) + if err != nil { + return err + } - if err := yaml.Unmarshal(out, &container); err != nil { - return err + if container.Name == "" { + container.Name = fmt.Sprintf("%v", value.Content[i-1].Value) + } + c.Containers = append(c.Containers, &container) } - if container.Name == "" { - container.Name = fmt.Sprintf("%v", s.Key) - } - c.Containers = append(c.Containers, &container) } return nil } diff --git a/cncd/pipeline/pipeline/frontend/yaml/container_test.go b/cncd/pipeline/pipeline/frontend/yaml/container_test.go index 3b4fd1f8d..3e920728f 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/container_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/container_test.go @@ -6,7 +6,7 @@ import ( libcompose "github.com/docker/libcompose/yaml" "github.com/kr/pretty" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) var containerYaml = []byte(` @@ -54,10 +54,6 @@ volumes: - /var/lib/mysql - /opt/data:/var/lib/mysql - /etc/configs:/etc/configs/:ro -ulimits: - nofile: - soft: 20000 - hard: 40000 tmpfs: - /var/lib/test when: @@ -102,11 +98,6 @@ func TestUnmarshalContainer(t *testing.T) { Privileged: true, ShmSize: libcompose.MemStringorInt(1024), Tmpfs: libcompose.Stringorslice{"/var/lib/test"}, - Ulimits: libcompose.Ulimits{ - Elements: []libcompose.Ulimit{ - libcompose.NewUlimit("nofile", 20000, 40000), - }, - }, Volumes: libcompose.Volumes{ Volumes: []*libcompose.Volume{ {Source: "", Destination: "/var/lib/mysql"}, @@ -179,7 +170,8 @@ func TestUnmarshalContainersErr(t *testing.T) { } for _, test := range testdata { in := []byte(test) - err := yaml.Unmarshal(in, new(Containers)) + containers := new(Containers) + err := yaml.Unmarshal(in, &containers) if err == nil { t.Errorf("wanted error for containers %q", test) } diff --git a/cncd/pipeline/pipeline/frontend/yaml/matrix/matrix.go b/cncd/pipeline/pipeline/frontend/yaml/matrix/matrix.go index a2c8ae70e..bc5fb50c7 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/matrix/matrix.go +++ b/cncd/pipeline/pipeline/frontend/yaml/matrix/matrix.go @@ -3,7 +3,7 @@ package matrix import ( "strings" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) const ( diff --git a/cncd/pipeline/pipeline/frontend/yaml/network.go b/cncd/pipeline/pipeline/frontend/yaml/network.go index 9375fcaec..5e10c96fa 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/network.go +++ b/cncd/pipeline/pipeline/frontend/yaml/network.go @@ -3,7 +3,7 @@ package yaml import ( "fmt" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) type ( @@ -21,23 +21,13 @@ type ( ) // UnmarshalYAML implements the Unmarshaller interface. -func (n *Networks) UnmarshalYAML(unmarshal func(interface{}) error) error { - slice := yaml.MapSlice{} - err := unmarshal(&slice) - if err != nil { - return err - } +func (n *Networks) UnmarshalYAML(value *yaml.Node) error { + networks := map[string]Network{} + err := value.Decode(&networks) - for _, s := range slice { - nn := Network{} - out, _ := yaml.Marshal(s.Value) - - err = yaml.Unmarshal(out, &nn) - if err != nil { - return err - } + for key, nn := range networks { if nn.Name == "" { - nn.Name = fmt.Sprintf("%v", s.Key) + nn.Name = fmt.Sprintf("%v", key) } if nn.Driver == "" { nn.Driver = "bridge" diff --git a/cncd/pipeline/pipeline/frontend/yaml/network_test.go b/cncd/pipeline/pipeline/frontend/yaml/network_test.go index d1306756a..4f1b0b89d 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/network_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/network_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/kr/pretty" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) func TestUnmarshalNetwork(t *testing.T) { diff --git a/cncd/pipeline/pipeline/frontend/yaml/secret.go b/cncd/pipeline/pipeline/frontend/yaml/secret.go index 69ba73dd7..07e9dba92 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/secret.go +++ b/cncd/pipeline/pipeline/frontend/yaml/secret.go @@ -1,5 +1,7 @@ package yaml +import "gopkg.in/yaml.v3" + type ( // Secrets defines a collection of secrets. Secrets struct { @@ -14,9 +16,11 @@ type ( ) // UnmarshalYAML implements the Unmarshaller interface. -func (s *Secrets) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (s *Secrets) UnmarshalYAML(value *yaml.Node) error { + y, _ := yaml.Marshal(value) + var strslice []string - err := unmarshal(&strslice) + err := yaml.Unmarshal(y, &strslice) if err == nil { for _, str := range strslice { s.Secrets = append(s.Secrets, &Secret{ @@ -26,5 +30,5 @@ func (s *Secrets) UnmarshalYAML(unmarshal func(interface{}) error) error { } return nil } - return unmarshal(&s.Secrets) + return yaml.Unmarshal(y, &s.Secrets) } diff --git a/cncd/pipeline/pipeline/frontend/yaml/secret_test.go b/cncd/pipeline/pipeline/frontend/yaml/secret_test.go index be8a6bbcb..6f9e30c63 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/secret_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/secret_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/kr/pretty" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) func TestUnmarshalSecrets(t *testing.T) { diff --git a/cncd/pipeline/pipeline/frontend/yaml/types/bool.go b/cncd/pipeline/pipeline/frontend/yaml/types/bool.go index 17a7aa9af..20d77f65d 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/types/bool.go +++ b/cncd/pipeline/pipeline/frontend/yaml/types/bool.go @@ -1,6 +1,10 @@ package types -import "strconv" +import ( + "strconv" + + "gopkg.in/yaml.v3" +) // BoolTrue is a custom Yaml boolean type that defaults to true. type BoolTrue struct { @@ -8,16 +12,16 @@ type BoolTrue struct { } // UnmarshalYAML implements custom Yaml unmarshaling. -func (b *BoolTrue) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (b *BoolTrue) UnmarshalYAML(value *yaml.Node) error { var s string - err := unmarshal(&s) - if err != nil { - return err - } + value.Decode(&s) - value, err := strconv.ParseBool(s) + v, err := strconv.ParseBool(s) if err == nil { - b.value = !value + b.value = !v + } + if s != "" && err != nil { + return err } return nil } diff --git a/cncd/pipeline/pipeline/frontend/yaml/types/bool_test.go b/cncd/pipeline/pipeline/frontend/yaml/types/bool_test.go index 74ea75418..1a50cd7eb 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/types/bool_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/types/bool_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/franela/goblin" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) func TestBoolTrue(t *testing.T) { @@ -44,7 +44,7 @@ func TestBoolTrue(t *testing.T) { }) g.It("should throw error when invalid", func() { - in := []byte("{ }") // string value should fail parse + in := []byte("abc") // string value should fail parse out := BoolTrue{} err := yaml.Unmarshal(in, &out) g.Assert(err != nil).IsTrue("expects error") diff --git a/cncd/pipeline/pipeline/frontend/yaml/volume.go b/cncd/pipeline/pipeline/frontend/yaml/volume.go index 01dac31b3..b4b1b1ab7 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/volume.go +++ b/cncd/pipeline/pipeline/frontend/yaml/volume.go @@ -3,7 +3,7 @@ package yaml import ( "fmt" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) type ( @@ -21,23 +21,18 @@ type ( ) // UnmarshalYAML implements the Unmarshaller interface. -func (v *Volumes) UnmarshalYAML(unmarshal func(interface{}) error) error { - slice := yaml.MapSlice{} - err := unmarshal(&slice) +func (v *Volumes) UnmarshalYAML(value *yaml.Node) error { + y, _ := yaml.Marshal(value) + + volumes := map[string]Volume{} + err := yaml.Unmarshal(y, &volumes) if err != nil { return err } - for _, s := range slice { - vv := Volume{} - out, _ := yaml.Marshal(s.Value) - - err = yaml.Unmarshal(out, &vv) - if err != nil { - return err - } + for key, vv := range volumes { if vv.Name == "" { - vv.Name = fmt.Sprintf("%v", s.Key) + vv.Name = fmt.Sprintf("%v", key) } if vv.Driver == "" { vv.Driver = "local" diff --git a/cncd/pipeline/pipeline/frontend/yaml/volume_test.go b/cncd/pipeline/pipeline/frontend/yaml/volume_test.go index d21fce087..03a229033 100644 --- a/cncd/pipeline/pipeline/frontend/yaml/volume_test.go +++ b/cncd/pipeline/pipeline/frontend/yaml/volume_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/kr/pretty" - "github.com/laszlocph/yaml" + "gopkg.in/yaml.v3" ) func TestUnmarshalVolume(t *testing.T) { diff --git a/go.mod b/go.mod index e8e88143c..61124bb7f 100644 --- a/go.mod +++ b/go.mod @@ -34,7 +34,6 @@ require ( github.com/joho/godotenv v0.0.0-20150907010228-4ed13390c0ac github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b github.com/kr/text v0.0.0-20160504234017-7cafcd837844 // indirect - github.com/laszlocph/yaml v0.0.0-20191114195230-2ec4ce7a1d34 github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect github.com/mattn/go-sqlite3 v0.0.0-20170901084005-05548ff55570 @@ -59,4 +58,5 @@ require ( google.golang.org/grpc v0.0.0-20170626232044-9cb02b885b41 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/validator.v8 v8.17.1 // indirect + gopkg.in/yaml.v3 v3.0.0-20191107175235-0b070bb63a18 ) diff --git a/go.sum b/go.sum index 733c331ff..7d39e3e42 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,6 @@ github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b h1:LJ9zj3Zit+pLjAQtA1gxl github.com/kr/pretty v0.0.0-20160708215748-737b74a46c4b/go.mod h1:Bvhd+E3laJ0AVkG0c9rmtZcnhV0HQ3+c3YxxqTvc/gA= github.com/kr/text v0.0.0-20160504234017-7cafcd837844 h1:kpzneEBeC0dMewP3gr/fADv1OlblH9r1goWVwpOt3TU= github.com/kr/text v0.0.0-20160504234017-7cafcd837844/go.mod h1:sjUstKUATFIcff4qlB53Kml0wQPtJVc/3fWrmuUmcfA= -github.com/laszlocph/yaml v0.0.0-20191114195230-2ec4ce7a1d34 h1:+4tKButWtRq7Xw8EUpabOmZYAk2gtinHF585AmWu2Qk= -github.com/laszlocph/yaml v0.0.0-20191114195230-2ec4ce7a1d34/go.mod h1:E1nYupUAMCOPyW4ZX78x63SP3/nKFQ5aj8tlwzMdYuo= github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae h1:rBqRT7VqVLePKGtyV6xDFLXeqD56CvZKEqI0XWzVTxM= github.com/lib/pq v0.0.0-20151015211310-83c4f410d0ae/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE= @@ -158,3 +156,5 @@ gopkg.in/go-playground/validator.v8 v8.17.1 h1:W1Q1z7rfiJiNhoBkHYqb9TJAdOqPXsyNe gopkg.in/go-playground/validator.v8 v8.17.1/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20191107175235-0b070bb63a18 h1:VaaR1yHVgJQaTGM1DXum4OU6He6gaZXAPII85hHgSzQ= +gopkg.in/yaml.v3 v3.0.0-20191107175235-0b070bb63a18/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=