|
1 | 1 | package api |
2 | 2 |
|
3 | | -// Dependency describes a prerequisite binary or package that must be present. |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + |
| 6 | + "github.com/Masterminds/semver/v3" |
| 7 | + "github.com/samber/lo" |
| 8 | + "gopkg.in/yaml.v3" |
| 9 | + |
| 10 | + "github.com/gdt-dev/core/parse" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + ValidOSs = []string{ |
| 15 | + "linux", |
| 16 | + "darwin", |
| 17 | + "windows", |
| 18 | + } |
| 19 | +) |
| 20 | + |
| 21 | +// Dependency describes a prerequisite binary that must be present. |
4 | 22 | type Dependency struct { |
5 | | - // Name is the name of the binary or package |
| 23 | + // Name is the name of the binary that must be present. |
6 | 24 | Name string `yaml:"name"` |
7 | 25 | // When describes any constraining conditions that apply to this |
8 | 26 | // Dependency. |
9 | | - When *DependencyConstraints `yaml:"when,omitempty"` |
| 27 | + When *DependencyConditions `yaml:"when,omitempty"` |
| 28 | + // Version contains instructions for constraining and selecting the |
| 29 | + // dependency's version. |
| 30 | + Version *DependencyVersion `yaml:"version,omitempty"` |
10 | 31 | } |
11 | 32 |
|
12 | | -// DependencyConstraints describes constraining conditions that apply to a |
| 33 | +func (d *Dependency) UnmarshalYAML(node *yaml.Node) error { |
| 34 | + if node.Kind != yaml.MappingNode { |
| 35 | + return parse.ExpectedMapAt(node) |
| 36 | + } |
| 37 | + for i := 0; i < len(node.Content); i += 2 { |
| 38 | + keyNode := node.Content[i] |
| 39 | + if keyNode.Kind != yaml.ScalarNode { |
| 40 | + return parse.ExpectedScalarAt(keyNode) |
| 41 | + } |
| 42 | + key := keyNode.Value |
| 43 | + valNode := node.Content[i+1] |
| 44 | + switch key { |
| 45 | + case "name": |
| 46 | + if valNode.Kind != yaml.ScalarNode { |
| 47 | + return parse.ExpectedScalarAt(valNode) |
| 48 | + } |
| 49 | + d.Name = valNode.Value |
| 50 | + case "when": |
| 51 | + if valNode.Kind != yaml.MappingNode { |
| 52 | + return parse.ExpectedMapAt(valNode) |
| 53 | + } |
| 54 | + var when DependencyConditions |
| 55 | + if err := valNode.Decode(&when); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + d.When = &when |
| 59 | + case "version": |
| 60 | + if valNode.Kind != yaml.MappingNode { |
| 61 | + return parse.ExpectedMapAt(valNode) |
| 62 | + } |
| 63 | + var dv DependencyVersion |
| 64 | + if err := valNode.Decode(&dv); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + d.Version = &dv |
| 68 | + default: |
| 69 | + return parse.UnknownFieldAt(key, keyNode) |
| 70 | + } |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// DependencyConditions describes constraining conditions that apply to a |
13 | 76 | // Dependency, for instance whether the dependency is only required on a |
14 | | -// particular OS or whether a particular version constraint applies to the |
15 | | -// dependency. |
16 | | -type DependencyConstraints struct { |
| 77 | +// particular OS. |
| 78 | +type DependencyConditions struct { |
17 | 79 | // OS indicates that the dependency only applies when the tests are run on |
18 | 80 | // a particular operating system. |
19 | 81 | OS string `yaml:"os,omitempty"` |
20 | | - // Version indicates a version constraint to apply to the dependency, e.g. |
21 | | - // >= 1.2.3 |
22 | | - Version string `yaml:"version,omitempty"` |
| 82 | +} |
| 83 | + |
| 84 | +func (c *DependencyConditions) UnmarshalYAML(node *yaml.Node) error { |
| 85 | + if node.Kind != yaml.MappingNode { |
| 86 | + return parse.ExpectedMapAt(node) |
| 87 | + } |
| 88 | + for i := 0; i < len(node.Content); i += 2 { |
| 89 | + keyNode := node.Content[i] |
| 90 | + if keyNode.Kind != yaml.ScalarNode { |
| 91 | + return parse.ExpectedScalarAt(keyNode) |
| 92 | + } |
| 93 | + key := keyNode.Value |
| 94 | + valNode := node.Content[i+1] |
| 95 | + switch key { |
| 96 | + case "os": |
| 97 | + if valNode.Kind != yaml.ScalarNode { |
| 98 | + return parse.ExpectedScalarAt(valNode) |
| 99 | + } |
| 100 | + os := valNode.Value |
| 101 | + if os != "" { |
| 102 | + if !lo.Contains(ValidOSs, os) { |
| 103 | + return parse.InvalidOSAt(valNode, os, ValidOSs) |
| 104 | + } |
| 105 | + c.OS = os |
| 106 | + } |
| 107 | + default: |
| 108 | + return parse.UnknownFieldAt(key, keyNode) |
| 109 | + } |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// DependencyVersion expresses a version constraint that must be met for a |
| 115 | +// particular dependency and instructs gdt how to get the version for a |
| 116 | +// dependency from a binary or package manager. |
| 117 | +type DependencyVersion struct { |
| 118 | + // Constraint indicates a version constraint to apply to the dependency, |
| 119 | + // e.g. '>= 1.2.3' would indicate that a version of the dependency binary |
| 120 | + // after and including 1.2.3 must be present on the host. |
| 121 | + Constraint string `yaml:"constraint"` |
| 122 | + SemVerConstraints *semver.Constraints `yaml:"-"` |
| 123 | + // Selector provides instructions to select the version from the binary. |
| 124 | + Selector *DependencyVersionSelector `yaml:"selector,omitempty"` |
| 125 | +} |
| 126 | + |
| 127 | +func (v *DependencyVersion) UnmarshalYAML(node *yaml.Node) error { |
| 128 | + if node.Kind != yaml.MappingNode { |
| 129 | + return parse.ExpectedMapAt(node) |
| 130 | + } |
| 131 | + for i := 0; i < len(node.Content); i += 2 { |
| 132 | + keyNode := node.Content[i] |
| 133 | + if keyNode.Kind != yaml.ScalarNode { |
| 134 | + return parse.ExpectedScalarAt(keyNode) |
| 135 | + } |
| 136 | + key := keyNode.Value |
| 137 | + valNode := node.Content[i+1] |
| 138 | + switch key { |
| 139 | + case "constraint": |
| 140 | + if valNode.Kind != yaml.ScalarNode { |
| 141 | + return parse.ExpectedScalarAt(valNode) |
| 142 | + } |
| 143 | + conStr := valNode.Value |
| 144 | + if conStr != "" { |
| 145 | + con, err := semver.NewConstraint(conStr) |
| 146 | + if err != nil { |
| 147 | + return parse.InvalidVersionConstraintAt( |
| 148 | + valNode, conStr, err, |
| 149 | + ) |
| 150 | + } |
| 151 | + v.Constraint = conStr |
| 152 | + v.SemVerConstraints = con |
| 153 | + } |
| 154 | + case "selector": |
| 155 | + if valNode.Kind != yaml.MappingNode { |
| 156 | + return parse.ExpectedMapAt(valNode) |
| 157 | + } |
| 158 | + var selector DependencyVersionSelector |
| 159 | + if err := valNode.Decode(&selector); err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + v.Selector = &selector |
| 163 | + default: |
| 164 | + return parse.UnknownFieldAt(key, keyNode) |
| 165 | + } |
| 166 | + } |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +// DependencyVersionSelector instructs gdt how to get the version of a binary. |
| 171 | +type DependencyVersionSelector struct { |
| 172 | + // Args is the command-line to execute the dependency binary to output |
| 173 | + // version information, e.g. '-v' or '--version-json'. |
| 174 | + Args []string `yaml:"args,omitempty"` |
| 175 | + // Filter is an optional regex to run against the output returned by |
| 176 | + // Command, e.g. 'v?(\d)+\.(\d+)(\.(\d)+)?'. |
| 177 | + Filter string `yaml:"filter,omitempty"` |
| 178 | + FilterRegex *regexp.Regexp `yaml:"-"` |
| 179 | +} |
| 180 | + |
| 181 | +func (s *DependencyVersionSelector) UnmarshalYAML(node *yaml.Node) error { |
| 182 | + if node.Kind != yaml.MappingNode { |
| 183 | + return parse.ExpectedMapAt(node) |
| 184 | + } |
| 185 | + for i := 0; i < len(node.Content); i += 2 { |
| 186 | + keyNode := node.Content[i] |
| 187 | + if keyNode.Kind != yaml.ScalarNode { |
| 188 | + return parse.ExpectedScalarAt(keyNode) |
| 189 | + } |
| 190 | + key := keyNode.Value |
| 191 | + valNode := node.Content[i+1] |
| 192 | + switch key { |
| 193 | + case "args": |
| 194 | + if valNode.Kind != yaml.SequenceNode { |
| 195 | + return parse.ExpectedSequenceAt(valNode) |
| 196 | + } |
| 197 | + var args []string |
| 198 | + if err := valNode.Decode(&args); err != nil { |
| 199 | + return err |
| 200 | + } |
| 201 | + s.Args = args |
| 202 | + case "filter": |
| 203 | + if valNode.Kind != yaml.ScalarNode { |
| 204 | + return parse.ExpectedMapAt(valNode) |
| 205 | + } |
| 206 | + filter := valNode.Value |
| 207 | + if filter != "" { |
| 208 | + re, err := regexp.Compile(filter) |
| 209 | + if err != nil { |
| 210 | + return parse.InvalidRegexAt(valNode, filter, err) |
| 211 | + } |
| 212 | + s.Filter = filter |
| 213 | + s.FilterRegex = re |
| 214 | + } |
| 215 | + default: |
| 216 | + return parse.UnknownFieldAt(key, keyNode) |
| 217 | + } |
| 218 | + } |
| 219 | + return nil |
23 | 220 | } |
0 commit comments