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