Repository navigation
Expand file tree
/
Copy pathversion.go
More file actions
110 lines (92 loc) · 1.91 KB
/
Copy pathversion.go
File metadata and controls
110 lines (92 loc) · 1.91 KB
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
package ast
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/earthly/earthly/ast/spec"
"github.com/pkg/errors"
)
// ParseVersion reads the VERSION command for an Earthfile and returns spec.Version
func ParseVersion(filePath string, enableSourceMap bool) (*spec.Version, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, errors.Wrapf(err, "unable to open %q", filePath)
}
defer file.Close()
var version spec.Version
foundVersion := false
scanner := bufio.NewScanner(file)
i := 0
var startLine int
var endLine int
args := []string{}
outer:
for scanner.Scan() {
i++
l := scanner.Text()
lineWidth := len(l)
l = strings.TrimSpace(l)
if len(l) == 0 {
continue
}
if l[0] == '#' {
continue
}
fields := strings.Fields(l)
if len(fields) == 0 {
continue
}
foundComment := false
trailingLine := false
for _, f := range fields {
if foundComment {
continue // ignore rest of line
}
if trailingLine {
if strings.HasPrefix(f, "#") {
foundComment = true
continue
}
// found something other than a '#' after a '\'
// e.g. VERSION \ UNEXPECTED
return nil, fmt.Errorf("malformed trailing line on %s:%d", filePath, i)
}
if f == "VERSION" && !foundVersion {
foundVersion = true
startLine = i
continue
}
if f == "\\" {
trailingLine = true
continue
}
if strings.HasPrefix(f, "#") {
foundComment = true
continue
}
if !foundVersion {
// received a keyword other than VERSION
break outer
}
args = append(args, f)
}
if trailingLine {
continue
}
endLine = i
version.Args = args
if enableSourceMap {
version.SourceLocation = &spec.SourceLocation{
File: filePath,
StartLine: startLine,
StartColumn: 0,
EndLine: endLine,
EndColumn: lineWidth,
}
}
return &version, nil
}
// No version was found
return nil, nil
}