fix 0.8 to 1.0 conversion when legacy merge keys

This commit is contained in:
Brad Rydzewski 2019-05-30 07:58:07 -07:00
parent 08056ba60a
commit c40f261f00
3 changed files with 36 additions and 0 deletions

1
go.sum
View File

@ -53,6 +53,7 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
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/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/vinzenz/yaml v0.0.0-20170920082545-91409cdd725d h1:3wDi6J5APMqaHBVPuVd7RmHD2gRTfqbdcVSpCNoUWtk=
github.com/vinzenz/yaml v0.0.0-20170920082545-91409cdd725d/go.mod h1:mb5taDqMnJiZNRQ3+02W2IFG+oEz1+dTuCXkp4jpkfo= github.com/vinzenz/yaml v0.0.0-20170920082545-91409cdd725d/go.mod h1:mb5taDqMnJiZNRQ3+02W2IFG+oEz1+dTuCXkp4jpkfo=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

View File

@ -39,6 +39,13 @@ type Config struct {
// Convert converts the yaml configuration file from // Convert converts the yaml configuration file from
// the legacy format to the 1.0+ format. // the legacy format to the 1.0+ format.
func Convert(d []byte) ([]byte, error) { func Convert(d []byte) ([]byte, error) {
// hack: this is a hack to support teams migrating
// from 0.8 to 1.0 that are using yaml merge keys.
// it can be removed in a future version.
if hasMergeKeys(d) {
d, _ = expandMergeKeys(d)
}
from := new(Config) from := new(Config)
err := yaml.Unmarshal(d, from) err := yaml.Unmarshal(d, from)

View File

@ -0,0 +1,28 @@
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package yaml
import (
"bytes"
"github.com/vinzenz/yaml"
)
// this is a helper function that expands merge keys
func expandMergeKeys(b []byte) ([]byte, error) {
v := map[interface{}]interface{}{}
if err := yaml.Unmarshal(b, v); err != nil {
return b, err
}
o, err := yaml.Marshal(v)
if err != nil {
return b, err
}
return o, nil
}
func hasMergeKeys(b []byte) bool {
return bytes.Contains(b, []byte("<<:"))
}