-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfpp.go
277 lines (254 loc) · 6 KB
/
dfpp.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package dfpp
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
)
type Logger interface {
Errorf(format string, args ...interface{})
}
type defaultLogger struct{}
func (l *defaultLogger) Errorf(format string, args ...interface{}) {
log.Printf(format, args...)
}
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
}
type Dfpp struct {
Processors map[string]func(string, []string) bool
Output io.Writer
client HttpClient
logger Logger
}
type Option func(pp *Dfpp)
func WithClient(c HttpClient) Option {
return func(pp *Dfpp) {
pp.client = c
}
}
func WithLogger(l Logger) Option {
return func(pp *Dfpp) {
pp.logger = l
}
}
func NewDfpp(opts ...Option) *Dfpp {
pp := &Dfpp{
Output: os.Stdout,
client: &http.Client{},
logger: &defaultLogger{},
}
pp.Processors = map[string]func(string, []string) bool{
"INCLUDE": pp.ProcessInclude,
}
for _, optFunc := range opts {
optFunc(pp)
}
return pp
}
func (pp *Dfpp) ProcessDockerfile(input io.Reader) {
for line := range InstructionScanner(input) {
parts := strings.Fields(line)
if len(parts) > 0 {
instruction := parts[0]
if fn, ok := pp.Processors[instruction]; ok {
if fn(line, parts) {
continue
}
}
}
fmt.Fprintf(pp.Output, "%s\n", line)
}
}
func InstructionScanner(input io.Reader) chan string {
ch := make(chan string)
go func() {
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
for len(line) > 0 && line[len(line)-1] == '\\' {
scanner.Scan()
line += "\n" + scanner.Text()
}
ch <- line
}
close(ch)
}()
return ch
}
func (pp *Dfpp) ProcessInclude(line string, fields []string) bool {
merge := false
exclude := make(map[string]bool)
include := make(map[string]bool)
uris := make([]string, 0, len(fields)-1)
for _, field := range fields {
if _, err := os.Stat(field); err == nil {
uris = append(uris, field)
continue
}
clude := include
if field[0] == '-' {
clude = exclude
field = field[1:]
}
switch field {
case "\\":
continue
case "INCLUDE":
continue
case "MERGE":
merge = true
case "ADD":
fallthrough
case "ARG":
fallthrough
case "CMD":
fallthrough
case "COPY":
fallthrough
case "ENTRYPOINT":
fallthrough
case "ENV":
fallthrough
case "EXPOSE":
fallthrough
case "FROM":
fallthrough
case "LABEL":
fallthrough
case "MAINTAINER":
fallthrough
case "ONBUILD":
fallthrough
case "RUN":
fallthrough
case "USER":
fallthrough
case "VOLUME":
fallthrough
case "WORKDIR":
clude[field] = true
default:
uris = append(uris, field)
}
}
docs := make([]string, 0, len(uris))
for _, uri := range uris {
if _, err := os.Stat(uri); err == nil {
content, err := ioutil.ReadFile(uri)
if err != nil {
pp.logger.Errorf("Failed to read %s: %s", uri, err)
os.Exit(1)
}
docs = append(docs, string(content))
} else {
req, _ := http.NewRequest("GET", uri, nil)
if resp, err := pp.client.Do(req); err != nil {
pp.logger.Errorf("Failed to %s %s: %s", req.Method, req.URL.String(), err)
os.Exit(1)
} else {
if resp.StatusCode < 200 || resp.StatusCode >= 300 && resp.StatusCode != 401 {
pp.logger.Errorf("response status: %s", resp.Status)
}
runtime.SetFinalizer(resp, func(r *http.Response) {
r.Body.Close()
})
if buf, err := ioutil.ReadAll(resp.Body); err == nil {
docs = append(docs, string(buf))
}
}
}
}
pp.Merge(merge, docs, include, exclude)
return true
}
type stringListReader struct {
lines []*string
lineIndex int
offset int
}
func (s *stringListReader) Read(p []byte) (n int, err error) {
want := cap(p)
readBytes := 0
for want > 0 {
if s.lineIndex >= len(s.lines) {
return readBytes, io.EOF
}
line := s.lines[s.lineIndex]
remainder := len(*line) - s.offset
if remainder > want {
copy(p[readBytes:], (*line)[s.offset:want])
s.offset += want
readBytes += want - s.offset
want = 0
} else {
copy(p[readBytes:], (*line)[s.offset:])
readBytes += remainder
s.offset = 0
s.lineIndex++
want -= remainder
// memory buffer was split on new lines, so make sure to add
// the newlines back on the output merged document.
copy(p[readBytes:], "\n")
readBytes++
}
}
return readBytes, nil
}
func (pp *Dfpp) Merge(merge bool, docs []string, include, exclude map[string]bool) {
result := make([]*string, 0)
ops := make(map[string]*string)
for _, doc := range docs {
for line := range InstructionScanner(strings.NewReader(doc)) {
fields := strings.Fields(line)
if len(fields) > 0 {
op := fields[0]
if _, ok := exclude[op]; ok {
continue
}
if _, ok := include[op]; len(include) > 0 && !ok {
continue
}
details := strings.TrimPrefix(line, fields[0]+" ")
if sref, ok := ops[op]; merge && ok {
if op == "ENV" {
if !strings.Contains(details, "=") {
fields := strings.Fields(details)
details = strings.Join(fields, "=")
}
*sref += " \\\n" + strings.Repeat(" ", len(op)+1) + details
} else if op == "LABEL" {
*sref += " \\\n" + strings.Repeat(" ", len(op)+1) + details
} else if op == "RUN" {
*sref += " && \\\n " + details
// squash redundent apt-get updates
squash := "apt-get update"
if ix := strings.Index(*sref, squash); ix >= 0 {
rest := strings.Replace((*sref)[ix+len(squash):], squash, "echo skipping redundent apt-get-update", -1)
*sref = (*sref)[0:ix+len(squash)] + rest
}
} else {
dup := string(line)
result = append(result, &dup)
}
} else {
if op == "ENV" && !strings.Contains(line, "=") {
line = strings.TrimPrefix(line, "ENV")
line = strings.TrimSpace(line)
fields := strings.Fields(line)
line = fmt.Sprintf("ENV %s", strings.Join(fields, "="))
}
dup := string(line)
result = append(result, &dup)
ops[op] = result[len(result)-1]
}
}
}
}
pp.ProcessDockerfile(&stringListReader{lines: result})
}