forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
120 lines (104 loc) · 2.58 KB
/
parser.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
package dockerfile
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strings"
"unicode"
dparser "github.com/docker/docker/builder/parser"
)
type dockerfile [][]string
// Parser is a Dockerfile parser
type Parser interface {
Parse(input io.Reader) (Dockerfile, error)
}
type parser struct{}
// NewParser creates a new Dockerfile parser
func NewParser() Parser {
return &parser{}
}
// Dockerfile represents a parsed Dockerfile
type Dockerfile interface {
GetDirective(name string) ([]string, bool)
}
// Parse parses an input Dockerfile
func (_ *parser) Parse(input io.Reader) (Dockerfile, error) {
buf := bufio.NewReader(input)
bts, err := buf.Peek(buf.Buffered())
if err != nil {
return nil, err
}
parsedByDocker := bytes.NewBuffer(bts)
// Add one more level of validation by using the Docker parser
if _, err := dparser.Parse(parsedByDocker); err != nil {
return nil, fmt.Errorf("cannot parse Dockerfile: %v", err)
}
d := dockerfile{}
scanner := bufio.NewScanner(input)
for {
line, ok := nextLine(scanner, true)
if !ok {
break
}
parts, err := parseLine(line)
if err != nil {
return nil, err
}
d = append(d, parts)
}
return d, nil
}
// GetDirective returns a list of lines that begin with the given directive
// and a flag that is true if the directive was found in the Dockerfile
func (d dockerfile) GetDirective(s string) ([]string, bool) {
values := []string{}
s = strings.ToLower(s)
for _, line := range d {
if strings.ToLower(line[0]) == s {
values = append(values, line[1])
}
}
return values, len(values) > 0
}
func isComment(line string) bool {
return strings.HasPrefix(line, "#")
}
func hasContinuation(line string) bool {
return strings.HasSuffix(strings.TrimRightFunc(line, unicode.IsSpace), "\\")
}
func stripContinuation(line string) string {
line = strings.TrimRightFunc(line, unicode.IsSpace)
return line[:len(line)-1]
}
func nextLine(scanner *bufio.Scanner, trimLeft bool) (string, bool) {
if scanner.Scan() {
line := scanner.Text()
if trimLeft {
line = strings.TrimLeftFunc(line, unicode.IsSpace)
}
if line == "" || isComment(line) {
return nextLine(scanner, true)
}
if hasContinuation(line) {
line := stripContinuation(line)
next, ok := nextLine(scanner, false)
if ok {
return line + next, true
} else {
return line, true
}
}
return line, true
}
return "", false
}
var dockerLineDelim = regexp.MustCompile(`[\t\v\f\r ]+`)
func parseLine(line string) ([]string, error) {
parts := dockerLineDelim.Split(line, 2)
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid Dockerfile")
}
return parts, nil
}