forked from lastbackend/lastbackend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
226 lines (178 loc) · 4.04 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package utils
import (
"archive/tar"
"compress/gzip"
"crypto/sha1"
"encoding/base64"
"fmt"
"github.com/gorilla/mux"
"github.com/satori/go.uuid"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
func AppName(path string) string {
splittedPath := strings.Split(path, "/")
appName := splittedPath[len(splittedPath)-1]
return appName
}
// GenerateID - generate random id with 64 length
func GenerateID() string {
u1 := uuid.NewV4()
u2 := uuid.NewV4()
return strings.Replace(fmt.Sprintf("%s%s", u1.String(), u2.String()), "-", "", -1)
}
// TODO: for any exported function comments are necessary.
// For time saving reason just in 2 words put small information
// about what function do and why it is needed for
// Hash - create hash based on provided string
func Hash(data string) string {
hash := sha1.Sum([]byte(data))
var hashString string
for i := 0; i < len(hash); i++ {
hashString += base64.URLEncoding.EncodeToString(hash[:i])
}
return hashString
}
// Ungzip -
func Ungzip(source, target string) error {
reader, err := os.Open(source)
if err != nil {
return err
}
archive, err := gzip.NewReader(reader)
if err != nil {
return err
}
target = filepath.Join(target, archive.Name)
writer, err := os.Create(target)
if err != nil {
return err
}
_, err = io.Copy(writer, archive)
reader.Close()
archive.Close()
writer.Close()
return err
}
func Update(source, target, update string, excludes []string) error {
src_f, err := os.Open(source)
if err != nil {
return err
}
src_rd := tar.NewReader(src_f)
target_f, err := os.Create(target)
if err != nil {
return err
}
target_wr := tar.NewWriter(target_f)
updated := make(map[string]bool)
_, err = os.Stat(update);
// apply the update if there is file
if update != `` && !os.IsNotExist(err) {
update_f, err := os.Open(update)
if err != nil {
return err
}
update_rd := tar.NewReader(update_f)
// write new files
for {
header, err := update_rd.Next()
if err == io.EOF {
target_wr.Flush()
break
} else if err != nil {
return err
} else {
if _, ok := updated[header.Name]; !ok {
updated[header.Name] = true
}
// write the header to the tarball archive
if err := target_wr.WriteHeader(header); err != nil {
return err
}
// replicate the file/dir to the tarball
if _, err := io.Copy(target_wr, update_rd); err != nil {
return err
}
}
}
update_f.Close()
}
// merge new files with current layer and exclude remove files
for {
header, err := src_rd.Next()
if err == io.EOF {
target_wr.Flush()
break
} else if err != nil {
return err
} else {
if _, ok := updated[header.Name]; !ok {
path := header.Name
if header.Typeflag == tar.TypeDir {
path = trimSuffix(path, "/")
}
if !exists(excludes, path) {
// write the header to the tarball archive
if err := target_wr.WriteHeader(header); err != nil {
return err
}
// replicate the file/dir to the tarball
if _, err := io.Copy(target_wr, src_rd); err != nil {
return err
}
}
}
}
}
src_f.Close()
target_f.Close()
target_wr.Close()
return nil
}
func CreateDirs(paths []string) error {
fileMode := os.FileMode(666)
for _, path := range paths {
if err := os.MkdirAll(path, fileMode); err != nil {
return err
}
}
return nil
}
func RemoveDirs(paths []string) error {
for _, path := range paths {
if err := os.RemoveAll(path); err != nil {
return err
}
}
return nil
}
// Returns `true` if the target string t is in the
// slice.
func exists(vs []string, t string) bool {
return index(vs, t) >= 0
}
// Returns the first index of the target string `t`, or
// -1 if no match is found.
func index(vs []string, t string) int {
for i, v := range vs {
if v == t {
return i
}
}
return -1
}
func trimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
s = s[:len(s)-len(suffix)]
}
return s
}
func GetStringParamFromURL(param string, r *http.Request) string {
params := mux.Vars(r)
value := params[param]
return value
}