1
- package main
1
+ package dfpp
2
2
3
3
import (
4
4
"bufio"
5
5
"fmt"
6
- "github.com/andrew-d/go-termutil"
7
6
"github.com/op/go-logging"
8
7
"io"
9
8
"io/ioutil"
@@ -14,43 +13,33 @@ import (
14
13
)
15
14
16
15
var log = logging .MustGetLogger ("dfpp" )
17
- var format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
18
16
19
- func main () {
20
- if len (os .Args ) == 1 && termutil .Isatty (os .Stdin .Fd ()) {
21
- Usage ()
22
- os .Exit (0 )
23
- }
17
+ type Dfpp struct {
18
+ Processors map [string ]func (string , []string )
19
+ Output io.Writer
20
+ }
24
21
25
- var err error
26
- inputFile := os .Stdin
27
- if len (os .Args ) > 1 {
28
- inputFile , err = os .Open (os .Args [1 ])
29
- if err != nil {
30
- fmt .Println ("Failed to read %s: %s" , os .Args [1 ], err )
31
- os .Exit (1 )
32
- }
22
+ func NewDfpp () * Dfpp {
23
+ pp := & Dfpp {
24
+ Output : os .Stdout ,
33
25
}
26
+ pp .Processors = map [string ]func (string , []string ){
27
+ "INCLUDE" : pp .ProcessInclude ,
28
+ }
29
+ return pp
30
+ }
34
31
35
- logBackend := logging .NewLogBackend (os .Stderr , "" , 0 )
36
- logging .SetBackend (
37
- logging .NewBackendFormatter (
38
- logBackend ,
39
- logging .MustStringFormatter (format ),
40
- ),
41
- )
42
- logging .SetLevel (logging .DEBUG , "" )
43
-
44
- for line := range InstructionScanner (inputFile ) {
32
+ func (pp * Dfpp ) ProcessDockerfile (input io.Reader ) {
33
+ for line := range InstructionScanner (input ) {
45
34
parts := strings .Fields (line )
46
35
if len (parts ) > 0 {
47
36
instruction := parts [0 ]
48
- if instruction == "INCLUDE" {
49
- ProcessInclude (line , parts )
37
+ if fn , ok := pp . Processors [ instruction ]; ok {
38
+ fn (line , parts )
50
39
continue
51
40
}
52
41
}
53
- fmt .Println ( line )
42
+ fmt .Fprintf ( pp . Output , "%s \n " , line )
54
43
}
55
44
}
56
45
@@ -71,7 +60,7 @@ func InstructionScanner(input io.Reader) chan string {
71
60
return ch
72
61
}
73
62
74
- func ProcessInclude (line string , fields []string ) {
63
+ func ( pp * Dfpp ) ProcessInclude (line string , fields []string ) {
75
64
merge := false
76
65
exclude := make (map [string ]bool )
77
66
include := make (map [string ]bool )
@@ -103,7 +92,7 @@ func ProcessInclude(line string, fields []string) {
103
92
fallthrough
104
93
case "ENTRYPOINT" :
105
94
fallthrough
106
- case "EVN " :
95
+ case "ENV " :
107
96
fallthrough
108
97
case "EXPOSE" :
109
98
fallthrough
@@ -156,10 +145,10 @@ func ProcessInclude(line string, fields []string) {
156
145
}
157
146
}
158
147
}
159
- Merge (merge , docs , include , exclude )
148
+ pp . Merge (merge , docs , include , exclude )
160
149
}
161
150
162
- func Merge (merge bool , docs []string , include , exclude map [string ]bool ) {
151
+ func ( pp * Dfpp ) Merge (merge bool , docs []string , include , exclude map [string ]bool ) {
163
152
result := make ([]* string , 0 )
164
153
ops := make (map [string ]* string )
165
154
for _ , doc := range docs {
@@ -201,108 +190,6 @@ func Merge(merge bool, docs []string, include, exclude map[string]bool) {
201
190
}
202
191
}
203
192
for _ , line := range result {
204
- fmt .Println ( * line )
193
+ fmt .Fprintf ( pp . Output , "%s \n " , * line )
205
194
}
206
195
}
207
-
208
- func Usage () {
209
- fmt .Print (`
210
- NAME
211
- dfpp - Dockerfile preprocessor
212
-
213
- SYNOPSIS
214
- $ dfpp Dockerfile.pre > Dockerfile
215
-
216
- # Dockerfile Syntax:
217
- INCLUDE ./Dockerfile.inc
218
- INCLUDE http://path/to/Dockerfile.inc
219
- INCLUDE ./Dockerfile.inc http://path/to/Dockerfile.inc
220
-
221
- INCLUDE MERGE a.inc b.inc
222
-
223
- # include only RUN instructions
224
- INCLUDE RUN a.inc b.inc
225
-
226
- # include only RUN and ENV instructions
227
- INCLUDE RUN ENV a.inc b.inc
228
-
229
- # include only RUN and ENV instructions but merge them
230
- INCLUDE MERGE RUN ENV a.inc b.inc
231
-
232
- # exclude FROM instructions
233
- INCLUDE -FROM a.inc b.inc
234
-
235
- DESCRIPTION
236
- "dfpp" was written to allow simple pre-processing of Dockerfiles to add
237
- capabilities currently unsupported by docker build.
238
-
239
- INSTRUCTIONS
240
- INCLUDE [MERGE] [FILTERS] [file|uri] ...
241
- This will inline a file or uri into the Dockerfile being generated.
242
-
243
- MERGE
244
- When including multiple Dockerfile snippets this will attempt to merge
245
- common instructions. Currently only ENV, LABEL and RUN are merged,
246
- otherwise multiple instructions will be repeated. RUN instructions are
247
- merged with "&&" while other instructions are merged with a space.
248
-
249
- FILTERS
250
- [-]ADD
251
- Include or Exclude ADD instructions from inlined Dockerfile snippets
252
-
253
- [-]CMD
254
- Include or Exclude CMD instructions from inlined Dockerfile snippets
255
-
256
- [-]COPY
257
- Include or Exclude COPY instructions from inlined Dockerfile snippets
258
-
259
- [-]ENTRYPOINT
260
- Include or Exclude ENTRYPOINT instructions from inlined Dockerfile
261
- snippets
262
-
263
- [-]ENV
264
- Include or Exclude ENV instructions from inlined Dockerfile snippets
265
-
266
- [-]EXPOSE
267
- Include or Exclude EXPOSE instructions from inlined Dockerfile snippets
268
-
269
- [-]FROM
270
- Include or Exclude FROM instructions from inlined Dockerfile snippets
271
-
272
- [-]INCLUDE
273
- Include or Exclude INCLUDE instructions from inlined Dockerfile snippets
274
-
275
- [-]LABEL
276
- Include or Exclude LABEL instructions from inlined Dockerfile snippets
277
-
278
- [-]MAINTAINER
279
- Include or Exclude MAINTAINER instructions from inlined Dockerfile
280
- snippets
281
-
282
- [-]ONBUILD
283
- Include or Exclude ONBUILD instructions from inlined Dockerfile snippets
284
-
285
- [-]RUN
286
- Include or Exclude RUN instructions from inlined Dockerfile snippets
287
-
288
- [-]USER
289
- Include or Exclude USER instructions from inlined Dockerfile snippets
290
-
291
- [-]VOLUME
292
- Include or Exclude VOLUME instructions from inlined Dockerfile snippets
293
-
294
- [-]WORKDIR
295
- Include or Exclude WORKDIR instructions from inlined Dockerfile snippets
296
-
297
- AUTHOR
298
- 2015, Cory Bennett <github@corybennett.org>
299
-
300
- SOURCE
301
- The Source is available at github:
302
- https://github.com/coryb/dfpp
303
-
304
- COPYRIGHT and LICENSE
305
- Copyright (c) 2015 Netflix Inc. All rights reserved. The copyrights to
306
- the contents of this file are licensed under the Apache License, Version 2.0
307
- ` )
308
- }
0 commit comments