2017-02-07 21:33:23 +00:00
|
|
|
package yaml
|
|
|
|
|
|
|
|
import (
|
2017-07-06 14:28:13 +00:00
|
|
|
"errors"
|
2017-02-07 21:33:23 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Build represents a build element in compose file.
|
|
|
|
// It can take multiple form in the compose file, hence this special type
|
|
|
|
type Build struct {
|
|
|
|
Context string
|
|
|
|
Dockerfile string
|
2017-07-06 14:28:13 +00:00
|
|
|
Args map[string]*string
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements the Marshaller interface.
|
2017-07-06 14:28:13 +00:00
|
|
|
func (b Build) MarshalYAML() (interface{}, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
m := map[string]interface{}{}
|
|
|
|
if b.Context != "" {
|
|
|
|
m["context"] = b.Context
|
|
|
|
}
|
|
|
|
if b.Dockerfile != "" {
|
|
|
|
m["dockerfile"] = b.Dockerfile
|
|
|
|
}
|
|
|
|
if len(b.Args) > 0 {
|
|
|
|
m["args"] = b.Args
|
|
|
|
}
|
2017-07-06 14:28:13 +00:00
|
|
|
return m, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the Unmarshaller interface.
|
2017-07-06 14:28:13 +00:00
|
|
|
func (b *Build) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var stringType string
|
|
|
|
if err := unmarshal(&stringType); err == nil {
|
|
|
|
b.Context = stringType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapType map[interface{}]interface{}
|
|
|
|
if err := unmarshal(&mapType); err == nil {
|
|
|
|
for mapKey, mapValue := range mapType {
|
2017-02-07 21:33:23 +00:00
|
|
|
switch mapKey {
|
|
|
|
case "context":
|
|
|
|
b.Context = mapValue.(string)
|
|
|
|
case "dockerfile":
|
|
|
|
b.Dockerfile = mapValue.(string)
|
|
|
|
case "args":
|
|
|
|
args, err := handleBuildArgs(mapValue)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Args = args
|
|
|
|
default:
|
|
|
|
// Ignore unknown keys
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 14:28:13 +00:00
|
|
|
return nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2017-07-06 14:28:13 +00:00
|
|
|
|
|
|
|
return errors.New("Failed to unmarshal Build")
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:28:13 +00:00
|
|
|
func handleBuildArgs(value interface{}) (map[string]*string, error) {
|
|
|
|
var args map[string]*string
|
2017-02-07 21:33:23 +00:00
|
|
|
switch v := value.(type) {
|
|
|
|
case map[interface{}]interface{}:
|
|
|
|
return handleBuildArgMap(v)
|
|
|
|
case []interface{}:
|
|
|
|
return handleBuildArgSlice(v)
|
|
|
|
default:
|
|
|
|
return args, fmt.Errorf("Failed to unmarshal Build args: %#v", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:28:13 +00:00
|
|
|
func handleBuildArgSlice(s []interface{}) (map[string]*string, error) {
|
|
|
|
var args = map[string]*string{}
|
2017-02-07 21:33:23 +00:00
|
|
|
for _, arg := range s {
|
|
|
|
// check if a value is provided
|
|
|
|
switch v := strings.SplitN(arg.(string), "=", 2); len(v) {
|
|
|
|
case 1:
|
|
|
|
// if we have not specified a a value for this build arg, we assign it an ascii null value and query the environment
|
|
|
|
// later when we build the service
|
2017-07-06 14:28:13 +00:00
|
|
|
str := "\x00"
|
|
|
|
args[v[0]] = &str
|
2017-02-07 21:33:23 +00:00
|
|
|
case 2:
|
|
|
|
// if we do have a value provided, we use it
|
2017-07-06 14:28:13 +00:00
|
|
|
args[v[0]] = &v[1]
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:28:13 +00:00
|
|
|
func handleBuildArgMap(m map[interface{}]interface{}) (map[string]*string, error) {
|
|
|
|
args := map[string]*string{}
|
2017-02-07 21:33:23 +00:00
|
|
|
for mapKey, mapValue := range m {
|
|
|
|
var argValue string
|
|
|
|
name, ok := mapKey.(string)
|
|
|
|
if !ok {
|
|
|
|
return args, fmt.Errorf("Cannot unmarshal '%v' to type %T into a string value", name, name)
|
|
|
|
}
|
|
|
|
switch a := mapValue.(type) {
|
|
|
|
case string:
|
|
|
|
argValue = a
|
2017-07-06 14:28:13 +00:00
|
|
|
case int:
|
|
|
|
argValue = strconv.Itoa(a)
|
2017-02-07 21:33:23 +00:00
|
|
|
case int64:
|
|
|
|
argValue = strconv.Itoa(int(a))
|
|
|
|
default:
|
|
|
|
return args, fmt.Errorf("Cannot unmarshal '%v' to type %T into a string value", mapValue, mapValue)
|
|
|
|
}
|
2017-07-06 14:28:13 +00:00
|
|
|
args[name] = &argValue
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|