-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
174 lines (134 loc) · 3.65 KB
/
utils.go
File metadata and controls
174 lines (134 loc) · 3.65 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package pkg
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"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
}
// 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
}
// Extract opens sourcefile and, if it has a '.gz' extension, unzips it using a gzip.Reader.
// then, it untars it using a tar.Reader
func Extract(sourcefile string) (err error) {
file, err := os.Open(sourcefile)
if err != nil {
fmt.Println(err)
return err
}
defer file.Close()
var fileReader io.ReadCloser = file
// just in case we are reading a tar.gz file, add a filter to handle gzipped file
if strings.HasSuffix(sourcefile, ".gz") {
if fileReader, err = gzip.NewReader(file); err != nil {
fmt.Println(err)
return err
}
defer fileReader.Close()
}
tarBallReader := tar.NewReader(fileReader)
// Extracting tarred files
for {
header, err := tarBallReader.Next()
if err != nil {
if err == io.EOF {
break
}
fmt.Println(err)
return err
}
// get the individual filename and extract to the current directory
filename := header.Name
switch header.Typeflag {
case tar.TypeDir:
// handle directory
fmt.Println("Creating directory :", filename)
err = os.MkdirAll(filename, os.FileMode(header.Mode)) // or use 0755 if you prefer
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case tar.TypeReg:
// handle normal file
fmt.Println("Untarring :", filename)
writer, err := os.Create(filename)
if err != nil {
fmt.Println(err)
return err
}
io.Copy(writer, tarBallReader)
err = os.Chmod(filename, os.FileMode(header.Mode))
if err != nil {
fmt.Println(err)
return err
}
writer.Close()
default:
fmt.Printf("Unable to untar type : %c in file %s", header.Typeflag, filename)
}
}
return nil
}