Repository navigation
Expand file tree
/
Copy pathvalidator.go
More file actions
118 lines (91 loc) · 2.69 KB
/
Copy pathvalidator.go
File metadata and controls
118 lines (91 loc) · 2.69 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
111
112
113
114
115
116
117
118
package ast
import (
"fmt"
"strings"
"github.com/earthly/earthly/ast/spec"
"github.com/pkg/errors"
)
// List of valid Earthfile versions.
// At some point we might want to break out Earthfile versioning
// into it's own package with some helper functions that are
// consumable from other packages.
var validEarthfileVersions = []string{
"0.0", // Meant only for testing/debugging. Disables all feature flags.
"0.5",
"0.6",
}
var errUnexpectedVersionArgs = fmt.Errorf("unexpected VERSION arguments; should be VERSION [flags] <major-version>.<minor-version>")
type astValidator func(spec.Earthfile) []error
var astValidations = []astValidator{
noTargetsWithSameName,
noTargetsWithKeywords,
validVersion,
// TODO other checks go here
}
func validateAst(ef spec.Earthfile) error {
var errs []error
for _, v := range astValidations {
if err := v(ef); err != nil {
errs = append(errs, err...)
}
}
if len(errs) > 0 {
errorStrings := make([]string, len(errs))
for i, err := range errs {
errorStrings[i] = err.Error()
}
return errors.Errorf("%v validation issues.\n- %s", len(errs), strings.Join(errorStrings, "\n- "))
}
return nil
}
func validVersion(ef spec.Earthfile) []error {
var errs []error
// VERSION is not required in Earthfile for now
if ef.Version == nil {
return nil
}
// if VERSION is specified, it's invalid to have no args
if len(ef.Version.Args) == 0 {
errs = append(errs, errUnexpectedVersionArgs)
return errs
}
// version is always last in VERSION command
earthFileVersion := ef.Version.Args[len(ef.Version.Args)-1]
isVersionValid := false
for _, version := range validEarthfileVersions {
if version == earthFileVersion {
isVersionValid = true
break
}
}
if !isVersionValid {
errs = append(errs, errors.Errorf("Earthfile version is invalid, supported versions are %v", validEarthfileVersions))
}
return errs
}
func noTargetsWithSameName(ef spec.Earthfile) []error {
var errs []error
seenTargets := map[string]struct{}{}
for _, t := range ef.Targets {
if _, seen := seenTargets[t.Name]; seen {
errs = append(errs, errors.Errorf("%s line %v:%v duplicate target \"%s\"", t.SourceLocation.File, t.SourceLocation.StartLine, t.SourceLocation.StartColumn, t.Name))
}
seenTargets[t.Name] = struct{}{}
}
if len(errs) > 0 {
return errs
}
return nil
}
func noTargetsWithKeywords(ef spec.Earthfile) []error {
var errs []error
for _, t := range ef.Targets {
if t.Name == "base" {
errs = append(errs, errors.Errorf("%s line %v:%v invalid target \"%s\": %s is a reserved target name", t.SourceLocation.File, t.SourceLocation.StartLine, t.SourceLocation.StartColumn, t.Name, t.Name))
}
}
if len(errs) > 0 {
return errs
}
return nil
}