Skip to content

Commit 1f0e9bd

Browse files
committed
move main package into main/main.go
minor refactor to that dfpp could be used as a library
1 parent 5047079 commit 1f0e9bd

File tree

3 files changed

+172
-142
lines changed

3 files changed

+172
-142
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ PLATFORMS= \
1515

1616
DIST=$(shell pwd)/dist
1717
export GOPATH=$(shell pwd)
18+
GOBIN ?= $(shell pwd)
1819

1920
build:
20-
cd src/github.com/coryb/dfpp; \
21-
go get -v
21+
go get -v github.com/coryb/dfpp
22+
go build -o $(GOBIN)/dfpp main/main.go
2223

2324
install:
2425
export GOBIN=~/bin && ${MAKE} build
@@ -32,22 +33,21 @@ cross-setup:
3233
all:
3334
rm -rf $(DIST); \
3435
mkdir -p $(DIST); \
35-
cd src/github.com/coryb/dfpp; \
3636
go get -d; \
3737
for p in $(PLATFORMS); do \
3838
echo "Building for $$p"; \
39-
GOOS=$${p/-*/} GOARCH=$${p/*-/} go build -v -ldflags -s -o $(DIST)/dfpp-$$p; \
39+
GOOS=$${p/-*/} GOARCH=$${p/*-/} go build -v -ldflags -s -o $(DIST)/dfpp-$$p main/main.go; \
4040
done
4141

4242
fmt:
43-
gofmt -s -w *.go
43+
gofmt -s -w *.go main/*.go
4444

4545
CURVER ?= $(shell git fetch --tags && git tag | tail -1)
4646
NEWVER ?= $(shell echo $(CURVER) | awk -F. '{print $$1"."$$2"."$$3+1}')
4747
TODAY := $(shell date +%Y-%m-%d)
4848

4949
changes:
50-
@git log --pretty=format:"* %s [%cn] [%h]" --no-merges ^$(CURVER) HEAD *.go | grep -vE 'gofmt|go fmt'
50+
@git log --pretty=format:"* %s [%cn] [%h]" --no-merges ^$(CURVER) HEAD *.go main/*.go | grep -vE 'gofmt|go fmt'
5151

5252
update-changelog:
5353
@echo "# Changelog" > CHANGELOG.md.new; \

dfpp.go

Lines changed: 23 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
package main
1+
package dfpp
22

33
import (
44
"bufio"
55
"fmt"
6-
"github.com/andrew-d/go-termutil"
76
"github.com/op/go-logging"
87
"io"
98
"io/ioutil"
@@ -14,43 +13,33 @@ import (
1413
)
1514

1615
var log = logging.MustGetLogger("dfpp")
17-
var format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
1816

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+
}
2421

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,
3325
}
26+
pp.Processors = map[string]func(string, []string){
27+
"INCLUDE": pp.ProcessInclude,
28+
}
29+
return pp
30+
}
3431

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) {
4534
parts := strings.Fields(line)
4635
if len(parts) > 0 {
4736
instruction := parts[0]
48-
if instruction == "INCLUDE" {
49-
ProcessInclude(line, parts)
37+
if fn, ok := pp.Processors[instruction]; ok {
38+
fn(line, parts)
5039
continue
5140
}
5241
}
53-
fmt.Println(line)
42+
fmt.Fprintf(pp.Output, "%s\n", line)
5443
}
5544
}
5645

@@ -71,7 +60,7 @@ func InstructionScanner(input io.Reader) chan string {
7160
return ch
7261
}
7362

74-
func ProcessInclude(line string, fields []string) {
63+
func (pp *Dfpp) ProcessInclude(line string, fields []string) {
7564
merge := false
7665
exclude := make(map[string]bool)
7766
include := make(map[string]bool)
@@ -103,7 +92,7 @@ func ProcessInclude(line string, fields []string) {
10392
fallthrough
10493
case "ENTRYPOINT":
10594
fallthrough
106-
case "EVN":
95+
case "ENV":
10796
fallthrough
10897
case "EXPOSE":
10998
fallthrough
@@ -156,10 +145,10 @@ func ProcessInclude(line string, fields []string) {
156145
}
157146
}
158147
}
159-
Merge(merge, docs, include, exclude)
148+
pp.Merge(merge, docs, include, exclude)
160149
}
161150

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) {
163152
result := make([]*string, 0)
164153
ops := make(map[string]*string)
165154
for _, doc := range docs {
@@ -201,108 +190,6 @@ func Merge(merge bool, docs []string, include, exclude map[string]bool) {
201190
}
202191
}
203192
for _, line := range result {
204-
fmt.Println(*line)
193+
fmt.Fprintf(pp.Output, "%s\n", *line)
205194
}
206195
}
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-
}

main/main.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/andrew-d/go-termutil"
6+
"github.com/coryb/dfpp"
7+
"github.com/op/go-logging"
8+
"os"
9+
)
10+
11+
var log = logging.MustGetLogger("dfpp")
12+
var format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
13+
14+
func main() {
15+
if len(os.Args) == 1 && termutil.Isatty(os.Stdin.Fd()) {
16+
Usage()
17+
os.Exit(0)
18+
}
19+
20+
var err error
21+
inputFile := os.Stdin
22+
if len(os.Args) > 1 {
23+
inputFile, err = os.Open(os.Args[1])
24+
if err != nil {
25+
fmt.Printf("Failed to read %s: %s\n", os.Args[1], err)
26+
os.Exit(1)
27+
}
28+
}
29+
30+
logBackend := logging.NewLogBackend(os.Stderr, "", 0)
31+
logging.SetBackend(
32+
logging.NewBackendFormatter(
33+
logBackend,
34+
logging.MustStringFormatter(format),
35+
),
36+
)
37+
logging.SetLevel(logging.DEBUG, "")
38+
39+
pp := dfpp.NewDfpp()
40+
pp.ProcessDockerfile(inputFile)
41+
}
42+
43+
func Usage() {
44+
fmt.Print(`
45+
NAME
46+
dfpp - Dockerfile preprocessor
47+
48+
SYNOPSIS
49+
$ dfpp Dockerfile.pre > Dockerfile
50+
51+
# Dockerfile Syntax:
52+
INCLUDE ./Dockerfile.inc
53+
INCLUDE http://path/to/Dockerfile.inc
54+
INCLUDE ./Dockerfile.inc http://path/to/Dockerfile.inc
55+
56+
INCLUDE MERGE a.inc b.inc
57+
58+
# include only RUN instructions
59+
INCLUDE RUN a.inc b.inc
60+
61+
# include only RUN and ENV instructions
62+
INCLUDE RUN ENV a.inc b.inc
63+
64+
# include only RUN and ENV instructions but merge them
65+
INCLUDE MERGE RUN ENV a.inc b.inc
66+
67+
# exclude FROM instructions
68+
INCLUDE -FROM a.inc b.inc
69+
70+
DESCRIPTION
71+
"dfpp" was written to allow simple pre-processing of Dockerfiles to add
72+
capabilities currently unsupported by docker build.
73+
74+
INSTRUCTIONS
75+
INCLUDE [MERGE] [FILTERS] [file|uri] ...
76+
This will inline a file or uri into the Dockerfile being generated.
77+
78+
MERGE
79+
When including multiple Dockerfile snippets this will attempt to merge
80+
common instructions. Currently only ENV, LABEL and RUN are merged,
81+
otherwise multiple instructions will be repeated. RUN instructions are
82+
merged with "&&" while other instructions are merged with a space.
83+
84+
FILTERS
85+
[-]ADD
86+
Include or Exclude ADD instructions from inlined Dockerfile snippets
87+
88+
[-]CMD
89+
Include or Exclude CMD instructions from inlined Dockerfile snippets
90+
91+
[-]COPY
92+
Include or Exclude COPY instructions from inlined Dockerfile snippets
93+
94+
[-]ENTRYPOINT
95+
Include or Exclude ENTRYPOINT instructions from inlined Dockerfile
96+
snippets
97+
98+
[-]ENV
99+
Include or Exclude ENV instructions from inlined Dockerfile snippets
100+
101+
[-]EXPOSE
102+
Include or Exclude EXPOSE instructions from inlined Dockerfile snippets
103+
104+
[-]FROM
105+
Include or Exclude FROM instructions from inlined Dockerfile snippets
106+
107+
[-]INCLUDE
108+
Include or Exclude INCLUDE instructions from inlined Dockerfile snippets
109+
110+
[-]LABEL
111+
Include or Exclude LABEL instructions from inlined Dockerfile snippets
112+
113+
[-]MAINTAINER
114+
Include or Exclude MAINTAINER instructions from inlined Dockerfile
115+
snippets
116+
117+
[-]ONBUILD
118+
Include or Exclude ONBUILD instructions from inlined Dockerfile snippets
119+
120+
[-]RUN
121+
Include or Exclude RUN instructions from inlined Dockerfile snippets
122+
123+
[-]USER
124+
Include or Exclude USER instructions from inlined Dockerfile snippets
125+
126+
[-]VOLUME
127+
Include or Exclude VOLUME instructions from inlined Dockerfile snippets
128+
129+
[-]WORKDIR
130+
Include or Exclude WORKDIR instructions from inlined Dockerfile snippets
131+
132+
AUTHOR
133+
2015, Cory Bennett <github@corybennett.org>
134+
135+
SOURCE
136+
The Source is available at github:
137+
https://github.com/coryb/dfpp
138+
139+
COPYRIGHT and LICENSE
140+
Copyright (c) 2015 Netflix Inc. All rights reserved. The copyrights to
141+
the contents of this file are licensed under the Apache License, Version 2.0
142+
`)
143+
}

0 commit comments

Comments
 (0)