2015-04-16 07:10:17 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/drone/drone/common"
|
|
|
|
"github.com/drone/drone/parser/inject"
|
|
|
|
"github.com/drone/drone/parser/matrix"
|
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2015-04-21 22:48:06 +00:00
|
|
|
var DefaultOpts = &Opts{
|
2015-04-16 07:10:17 +00:00
|
|
|
Volumes: false,
|
|
|
|
Network: false,
|
|
|
|
Privileged: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2015-04-21 22:48:06 +00:00
|
|
|
return ParseOpts(raw, DefaultOpts)
|
2015-04-16 07:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-04-21 22:48:06 +00:00
|
|
|
conf, err := ParseSingle(raw, opts)
|
2015-04-16 07:10:17 +00:00
|
|
|
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)
|
2015-04-21 22:48:06 +00:00
|
|
|
conf, err := ParseSingle(injected, opts)
|
2015-04-16 07:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conf.Axis = common.Axis(ax)
|
|
|
|
confs = append(confs, conf)
|
|
|
|
}
|
2015-04-21 22:48:06 +00:00
|
|
|
|
2015-04-16 07:10:17 +00:00
|
|
|
return confs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper funtion to parse a yaml configuration file.
|
2015-04-21 22:48:06 +00:00
|
|
|
func ParseSingle(raw string, opts *Opts) (*common.Config, error) {
|
2015-04-16 07:10:17 +00:00
|
|
|
conf := &common.Config{}
|
|
|
|
err := yaml.Unmarshal([]byte(raw), conf)
|
2015-04-21 22:48:06 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-04-16 07:10:17 +00:00
|
|
|
return conf, err
|
|
|
|
}
|