github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/builder/utils.go (about)

     1  package builder
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  // YamlToJSON takes an input yaml string, parses it and returns a string formatted as json.
    11  func YamlToJSON(bytes []byte) (string, error) {
    12  	var anomaly map[string]string
    13  
    14  	if err := yaml.Unmarshal(bytes, &anomaly); err != nil {
    15  		return "", err
    16  	}
    17  
    18  	retVal, err := json.Marshal(&anomaly)
    19  
    20  	if err != nil {
    21  		return "", err
    22  	}
    23  
    24  	return string(retVal), nil
    25  }
    26  
    27  // ParseConfig takes a response body from the controller and returns a Config object.
    28  func ParseConfig(body []byte) (*Config, error) {
    29  	var config Config
    30  	err := json.Unmarshal(body, &config)
    31  	return &config, err
    32  }
    33  
    34  // ParseDomain returns the domain field from the bytes of a build hook response.
    35  func ParseDomain(bytes []byte) (string, error) {
    36  	var hook BuildHookResponse
    37  	if err := json.Unmarshal(bytes, &hook); err != nil {
    38  		return "", err
    39  	}
    40  
    41  	if hook.Domains == nil {
    42  		return "", fmt.Errorf("invalid application domain")
    43  	}
    44  
    45  	if len(hook.Domains) < 1 {
    46  		return "", fmt.Errorf("invalid application domain")
    47  	}
    48  
    49  	return hook.Domains[0], nil
    50  }
    51  
    52  // ParseReleaseVersion returns the version field from the bytes of a build hook response.
    53  func ParseReleaseVersion(bytes []byte) (int, error) {
    54  	var hook BuildHookResponse
    55  	if err := json.Unmarshal(bytes, &hook); err != nil {
    56  		return 0, fmt.Errorf("invalid application json configuration")
    57  	}
    58  
    59  	if hook.Release == nil {
    60  		return 0, fmt.Errorf("invalid application version")
    61  	}
    62  
    63  	return hook.Release["version"], nil
    64  }
    65  
    66  // GetDefaultType returns the default process types given a YAML byte array.
    67  func GetDefaultType(bytes []byte) (string, error) {
    68  	type YamlTypeMap struct {
    69  		DefaultProcessTypes ProcessType `yaml:"default_process_types"`
    70  	}
    71  
    72  	var p YamlTypeMap
    73  
    74  	if err := yaml.Unmarshal(bytes, &p); err != nil {
    75  		return "", err
    76  	}
    77  
    78  	retVal, err := json.Marshal(&p.DefaultProcessTypes)
    79  
    80  	if err != nil {
    81  		return "", err
    82  	}
    83  
    84  	if len(p.DefaultProcessTypes) == 0 {
    85  		return "{}", nil
    86  	}
    87  
    88  	return string(retVal), nil
    89  }
    90  
    91  // ParseControllerConfig returns configuration key/value pair strings from a config.
    92  func ParseControllerConfig(bytes []byte) ([]string, error) {
    93  	var controllerConfig Config
    94  	if err := json.Unmarshal(bytes, &controllerConfig); err != nil {
    95  		return []string{}, err
    96  	}
    97  
    98  	if controllerConfig.Values == nil {
    99  		return []string{""}, nil
   100  	}
   101  
   102  	retVal := []string{}
   103  	for k, v := range controllerConfig.Values {
   104  		retVal = append(retVal, fmt.Sprintf(" -e %s=\"%v\"", k, v))
   105  	}
   106  	return retVal, nil
   107  }