-
Notifications
You must be signed in to change notification settings - Fork 16
/
archive.go
224 lines (204 loc) · 4.85 KB
/
archive.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
package dynaml
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"github.com/mandelsoft/spiff/yaml"
"io"
"strings"
"time"
)
type FileEntry struct {
path string
mode int64
data []byte
}
func func_archive(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if len(arguments) < 1 || len(arguments) > 2 {
return info.Error("archive takes one or two arguments")
}
mode := "tar"
if len(arguments) == 2 {
str, ok := arguments[1].(string)
if !ok {
return info.Error("second argument for archive must be a string")
}
mode = str
}
files := []*FileEntry{}
var e *FileEntry
var err error
if arguments[0] != nil {
switch data := arguments[0].(type) {
case map[string]yaml.Node:
for _, file := range getSortedKeys(data) {
val := data[file]
if val == nil || val.Value() == nil {
continue
}
switch v := val.Value().(type) {
case map[string]yaml.Node:
e, err = getFileEntry(&file, v)
if err != nil {
return info.Error("%s", err)
}
default:
file, _, content, ok := getData(file, WriteOpts{}, file, val.Value(), true)
if !ok {
return info.Error("invalid file content type %s", ExpressionType(val))
}
e = &FileEntry{file, 0644, content}
}
files = append(files, e)
}
case []yaml.Node:
for _, val := range data {
switch v := val.Value().(type) {
case map[string]yaml.Node:
e, err = getFileEntry(nil, v)
if err != nil {
return info.Error("%s", err)
}
default:
return info.Error("invalid file content type %s", ExpressionType(val))
}
files = append(files, e)
}
default:
return info.Error("first argument for hash must be a file map or list, found %s", ExpressionType(arguments[0]))
}
}
var buf bytes.Buffer
switch mode {
case "targz":
zipper := gzip.NewWriter(&buf)
err = tar_archive(zipper, files)
zipper.Close()
if err != nil {
return info.Error("archiving %s failed: %s", mode, err)
}
case "tar":
err = tar_archive(&buf, files)
if err != nil {
return info.Error("archiving %s failed: %s", mode, err)
}
default:
return info.Error("invalid archive type '%s'", mode)
}
return Base64Encode(buf.Bytes(), 60), info, true
}
func getFileEntry(file *string, info map[string]yaml.Node) (*FileEntry, error) {
var e FileEntry
var err error
wopt := WriteOpts{
Permissions: 0644,
}
field, ok := info["path"]
if ok {
if field == nil || field.Value() == nil {
return nil, fmt.Errorf("path field must not be nil")
}
v, ok := field.Value().(string)
if !ok {
return nil, fmt.Errorf("path field must be string, found %s", ExpressionType(field))
}
e.path = v
} else {
if file == nil {
return nil, fmt.Errorf("path field required")
}
e.path = removeTags(*file)
}
field, ok = info["mode"]
if ok {
if field == nil || field.Value() == nil {
return nil, fmt.Errorf("mode field must not be nil")
}
wopt, err = getWriteOptions(field.Value(), wopt, false)
if err != nil {
return nil, err
}
}
e.mode = wopt.Permissions
field, ok = info["base64"]
if ok {
if field == nil || field.Value() == nil {
return nil, fmt.Errorf("base64 field must not be nil")
}
v, ok := field.Value().(string)
if !ok {
return nil, fmt.Errorf("base64 field must be string, found %s", ExpressionType(field))
}
e.data, err = base64.StdEncoding.DecodeString(v)
if err != nil {
return nil, err
}
} else {
field, ok = info["data"]
if ok {
if field == nil || field.Value() == nil {
return nil, fmt.Errorf("data field must not be nil")
}
_, _, content, ok := getData("", wopt, e.path, field.Value(), true)
if !ok {
return nil, fmt.Errorf("invalid data field")
}
e.data = content
}
}
if file != nil && hasTag(*file, "*") {
e.mode |= 0100
}
return &e, nil
}
func hasTag(file, tag string) bool {
for strings.HasPrefix(file, "*") || strings.HasPrefix(file, "#") || strings.HasPrefix(file, "-") {
if strings.HasPrefix(file, tag) {
return true
}
stop := strings.HasPrefix(file, "-")
file = file[1:]
if stop {
break
}
}
return false
}
func removeTags(file string) string {
for strings.HasPrefix(file, "*") || strings.HasPrefix(file, "#") || strings.HasPrefix(file, ":") {
stop := strings.HasPrefix(file, ":")
file = file[1:]
if stop {
break
}
}
return file
}
func tar_archive(w io.Writer, files []*FileEntry) error {
tw := tar.NewWriter(w)
defer tw.Close()
now := time.Now()
for _, file := range files {
header := &tar.Header{
Name: file.path,
Mode: file.mode,
Size: int64(len(file.data)),
ModTime: now,
ChangeTime: now,
AccessTime: now,
}
if err := tw.WriteHeader(header); err != nil {
return err
}
if _, err := tw.Write(file.data); err != nil {
return err
}
}
if err := tw.Close(); err != nil {
return err
}
return nil
}