woodpecker/pipeline/frontend/yaml/volume.go

44 lines
827 B
Go
Raw Normal View History

2017-03-05 07:56:08 +00:00
package yaml
import (
"fmt"
2019-11-14 19:16:03 +00:00
"gopkg.in/yaml.v3"
2017-03-05 07:56:08 +00:00
)
type (
// Volumes defines a collection of volumes.
Volumes struct {
Volumes []*Volume
}
// Volume defines a container volume.
Volume struct {
Name string `yaml:"name,omitempty"`
Driver string `yaml:"driver,omitempty"`
DriverOpts map[string]string `yaml:"driver_opts,omitempty"`
}
)
// UnmarshalYAML implements the Unmarshaller interface.
2019-11-14 19:16:03 +00:00
func (v *Volumes) UnmarshalYAML(value *yaml.Node) error {
y, _ := yaml.Marshal(value)
volumes := map[string]Volume{}
err := yaml.Unmarshal(y, &volumes)
2017-03-05 07:56:08 +00:00
if err != nil {
return err
}
2019-11-14 19:16:03 +00:00
for key, vv := range volumes {
2017-03-05 07:56:08 +00:00
if vv.Name == "" {
2019-11-14 19:16:03 +00:00
vv.Name = fmt.Sprintf("%v", key)
2017-03-05 07:56:08 +00:00
}
if vv.Driver == "" {
vv.Driver = "local"
}
v.Volumes = append(v.Volumes, &vv)
}
return err
}