github.com/r3labs/libcompose@v0.4.1-0.20171123133234-495fe0619cc3/yaml/command.go (about)

     1  package yaml
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/docker/docker/api/types/strslice"
     8  	"github.com/flynn/go-shlex"
     9  )
    10  
    11  // Command represents a docker command, can be a string or an array of strings.
    12  type Command strslice.StrSlice
    13  
    14  // UnmarshalYAML implements the Unmarshaller interface.
    15  func (s *Command) UnmarshalYAML(unmarshal func(interface{}) error) error {
    16  	var stringType string
    17  	if err := unmarshal(&stringType); err == nil {
    18  		parts, err := shlex.Split(stringType)
    19  		if err != nil {
    20  			return err
    21  		}
    22  		*s = parts
    23  		return nil
    24  	}
    25  
    26  	var sliceType []interface{}
    27  	if err := unmarshal(&sliceType); err == nil {
    28  		parts, err := toStrings(sliceType)
    29  		if err != nil {
    30  			return err
    31  		}
    32  		*s = parts
    33  		return nil
    34  	}
    35  
    36  	var interfaceType interface{}
    37  	if err := unmarshal(&interfaceType); err == nil {
    38  		fmt.Println(interfaceType)
    39  	}
    40  
    41  	return errors.New("Failed to unmarshal Command")
    42  }