File: directives.go

package info (click to toggle)
docker.io 18.09.1%2Bdfsg1-7.1%2Bdeb10u3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 66,144 kB
  • sloc: sh: 9,753; makefile: 827; ansic: 239; python: 162; asm: 10
file content (38 lines) | stat: -rw-r--r-- 723 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
package dockerfile2llb

import (
	"bufio"
	"io"
	"regexp"
	"strings"
)

const keySyntax = "syntax"

var reDirective = regexp.MustCompile(`^#\s*([a-zA-Z][a-zA-Z0-9]*)\s*=\s*(.+?)\s*$`)

func DetectSyntax(r io.Reader) (string, string, bool) {
	directives := ParseDirectives(r)
	if len(directives) == 0 {
		return "", "", false
	}
	v, ok := directives[keySyntax]
	if !ok {
		return "", "", false
	}
	p := strings.SplitN(v, " ", 2)
	return p[0], v, true
}

func ParseDirectives(r io.Reader) map[string]string {
	m := map[string]string{}
	s := bufio.NewScanner(r)
	for s.Scan() {
		match := reDirective.FindStringSubmatch(s.Text())
		if len(match) == 0 {
			return m
		}
		m[strings.ToLower(match[1])] = match[2]
	}
	return m
}