File: directives.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (173 lines) | stat: -rw-r--r-- 4,020 bytes parent folder | download
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
package parser

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"regexp"
	"strings"

	"github.com/pkg/errors"
)

const (
	keySyntax = "syntax"
	keyCheck  = "check"
	keyEscape = "escape"
)

var validDirectives = map[string]struct{}{
	keySyntax: {},
	keyEscape: {},
	keyCheck:  {},
}

type Directive struct {
	Name     string
	Value    string
	Location []Range
}

// DirectiveParser is a parser for Dockerfile directives that enforces the
// quirks of the directive parser.
type DirectiveParser struct {
	line   int
	regexp *regexp.Regexp
	seen   map[string]struct{}
	done   bool
}

func (d *DirectiveParser) setComment(comment string) {
	d.regexp = regexp.MustCompile(fmt.Sprintf(`^%s\s*([a-zA-Z][a-zA-Z0-9]*)\s*=\s*(.+?)\s*$`, comment))
}

func (d *DirectiveParser) ParseLine(line []byte) (*Directive, error) {
	d.line++
	if d.done {
		return nil, nil
	}
	if d.regexp == nil {
		d.setComment("#")
	}

	match := d.regexp.FindSubmatch(line)
	if len(match) == 0 {
		d.done = true
		return nil, nil
	}

	k := strings.ToLower(string(match[1]))
	if _, ok := validDirectives[k]; !ok {
		d.done = true
		return nil, nil
	}
	if d.seen == nil {
		d.seen = map[string]struct{}{}
	}
	if _, ok := d.seen[k]; ok {
		return nil, errors.Errorf("only one %s parser directive can be used", k)
	}
	d.seen[k] = struct{}{}

	v := string(match[2])

	directive := Directive{
		Name:  k,
		Value: v,
		Location: []Range{{
			Start: Position{Line: d.line},
			End:   Position{Line: d.line},
		}},
	}
	return &directive, nil
}

func (d *DirectiveParser) ParseAll(data []byte) ([]*Directive, error) {
	scanner := bufio.NewScanner(bytes.NewReader(data))
	var directives []*Directive
	for scanner.Scan() {
		if d.done {
			break
		}

		d, err := d.ParseLine(scanner.Bytes())
		if err != nil {
			return directives, err
		}
		if d != nil {
			directives = append(directives, d)
		}
	}
	return directives, nil
}

// DetectSyntax returns the syntax of provided input.
//
// The traditional dockerfile directives '# syntax = ...' are used by default,
// however, the function will also fallback to c-style directives '// syntax = ...'
// and json-encoded directives '{ "syntax": "..." }'. Finally, starting lines
// with '#!' are treated as shebangs and ignored.
//
// This allows for a flexible range of input formats, and appropriate syntax
// selection.
func DetectSyntax(dt []byte) (string, string, []Range, bool) {
	return ParseDirective(keySyntax, dt)
}

func ParseDirective(key string, dt []byte) (string, string, []Range, bool) {
	dt, hadShebang, err := discardShebang(dt)
	if err != nil {
		return "", "", nil, false
	}
	line := 0
	if hadShebang {
		line++
	}

	// use default directive parser, and search for #key=
	directiveParser := DirectiveParser{line: line}
	if syntax, cmdline, loc, ok := detectDirectiveFromParser(key, dt, directiveParser); ok {
		return syntax, cmdline, loc, true
	}

	// use directive with different comment prefix, and search for //key=
	directiveParser = DirectiveParser{line: line}
	directiveParser.setComment("//")
	if syntax, cmdline, loc, ok := detectDirectiveFromParser(key, dt, directiveParser); ok {
		return syntax, cmdline, loc, true
	}

	// use json directive, and search for { "key": "..." }
	jsonDirective := map[string]string{}
	if err := json.Unmarshal(dt, &jsonDirective); err == nil {
		if v, ok := jsonDirective[key]; ok {
			loc := []Range{{
				Start: Position{Line: line},
				End:   Position{Line: line},
			}}
			return v, v, loc, true
		}
	}

	return "", "", nil, false
}

func detectDirectiveFromParser(key string, dt []byte, parser DirectiveParser) (string, string, []Range, bool) {
	directives, _ := parser.ParseAll(dt)
	for _, d := range directives {
		if d.Name == key {
			p, _, _ := strings.Cut(d.Value, " ")
			return p, d.Value, d.Location, true
		}
	}
	return "", "", nil, false
}

func discardShebang(dt []byte) ([]byte, bool, error) {
	line, rest, _ := bytes.Cut(dt, []byte("\n"))
	if bytes.HasPrefix(line, []byte("#!")) {
		return rest, true, nil
	}
	return dt, false, nil
}