harness-drone/pkg/yaml/parse.go

109 lines
2.4 KiB
Go
Raw Normal View History

package parser
import (
2015-05-17 20:51:42 +00:00
common "github.com/drone/drone/pkg/types"
2015-05-17 18:36:12 +00:00
"github.com/drone/drone/pkg/yaml/inject"
"github.com/drone/drone/pkg/yaml/matrix"
2015-05-22 18:37:40 +00:00
"github.com/drone/drone/Godeps/_workspace/src/gopkg.in/yaml.v2"
)
// Opts specifies parser options that will permit
// or deny certain Yaml settings.
type Opts struct {
Volumes bool
Network bool
Privileged bool
Whitelist []string
}
var DefaultOpts = &Opts{
Volumes: false,
Network: false,
Privileged: false,
Whitelist: []string{"plugins/*"},
}
// Parse parses a build matrix and returns
// a list of build configurations for each axis
// using the default parsing options.
func Parse(raw string) ([]*common.Config, error) {
return ParseOpts(raw, DefaultOpts)
}
// ParseOpts parses a build matrix and returns
// a list of build configurations for each axis
// using the provided parsing options.
func ParseOpts(raw string, opts *Opts) ([]*common.Config, error) {
axis, err := matrix.Parse(raw)
if err != nil {
return nil, err
}
confs := []*common.Config{}
// when no matrix values exist we should return
// a single config value with an empty label.
if len(axis) == 0 {
conf, err := ParseSingle(raw, opts)
if err != nil {
return nil, err
}
confs = append(confs, conf)
}
for _, ax := range axis {
// inject the matrix values into the raw script
injected := inject.Inject(raw, ax)
conf, err := ParseSingle(injected, opts)
if err != nil {
return nil, err
}
conf.Axis = common.Axis(ax)
confs = append(confs, conf)
}
return confs, nil
}
// helper funtion to parse a yaml configuration file.
func ParseSingle(raw string, opts *Opts) (*common.Config, error) {
conf := &common.Config{}
err := yaml.Unmarshal([]byte(raw), conf)
if err != nil {
return nil, err
}
// lint the yaml file
err = Lint(conf)
if err != nil {
return nil, err
}
// apply rules / transofms
transformSetup(conf)
transformClone(conf)
transformBuild(conf)
transformImages(conf)
transformDockerPlugin(conf)
if !opts.Network {
rmNetwork(conf)
}
if !opts.Volumes {
rmVolumes(conf)
}
if !opts.Privileged {
rmPrivileged(conf)
}
err = LintPlugins(conf, opts)
if err != nil {
return nil, err
}
return conf, err
}
2015-04-30 17:39:16 +00:00
func ParseCondition(raw string) (*common.Condition, error) {
c := struct {
Condition *common.Condition `yaml:"when"`
}{}
err := yaml.Unmarshal([]byte(raw), c)
return c.Condition, err
}