|
7 | 7 | "os"
|
8 | 8 | "path"
|
9 | 9 | "path/filepath"
|
| 10 | + "regexp" |
10 | 11 | "runtime"
|
11 | 12 | "strings"
|
12 | 13 |
|
@@ -252,7 +253,7 @@ func Validate(y *LimaYAML, warn bool) error {
|
252 | 253 | }
|
253 | 254 | if rule.GuestSocket != "" {
|
254 | 255 | if !path.IsAbs(rule.GuestSocket) {
|
255 |
| - return fmt.Errorf("field `%s.guestSocket` must be an absolute path", field) |
| 256 | + return fmt.Errorf("field `%s.guestSocket` must be an absolute path, but is %q", field, rule.GuestSocket) |
256 | 257 | }
|
257 | 258 | if rule.HostSocket == "" && rule.HostPortRange[1]-rule.HostPortRange[0] > 0 {
|
258 | 259 | return fmt.Errorf("field `%s.guestSocket` can only be mapped to a single port or socket. not a range", field)
|
@@ -287,7 +288,7 @@ func Validate(y *LimaYAML, warn bool) error {
|
287 | 288 | field := fmt.Sprintf("CopyToHost[%d]", i)
|
288 | 289 | if rule.GuestFile != "" {
|
289 | 290 | if !path.IsAbs(rule.GuestFile) {
|
290 |
| - return fmt.Errorf("field `%s.guest` must be an absolute path", field) |
| 291 | + return fmt.Errorf("field `%s.guest` must be an absolute path, but is %q", field, rule.GuestFile) |
291 | 292 | }
|
292 | 293 | }
|
293 | 294 | if rule.HostFile != "" {
|
@@ -385,6 +386,46 @@ func validateNetwork(y *LimaYAML) error {
|
385 | 386 | return nil
|
386 | 387 | }
|
387 | 388 |
|
| 389 | +// ValidateParamIsUsed checks if the keys in the `param` field are used in any script, probe, copyToHost, or portForward. |
| 390 | +// It should be called before the `y` parameter is passed to FillDefault() that execute template. |
| 391 | +func ValidateParamIsUsed(y *LimaYAML) error { |
| 392 | + for key := range y.Param { |
| 393 | + re, err := regexp.Compile(`{{[^}]*\.Param\.` + key + `[^}]*}}`) |
| 394 | + if err != nil { |
| 395 | + return fmt.Errorf("field to compile regexp for key %q: %w", key, err) |
| 396 | + } |
| 397 | + keyIsUsed := false |
| 398 | + for _, p := range y.Provision { |
| 399 | + if re.MatchString(p.Script) { |
| 400 | + keyIsUsed = true |
| 401 | + break |
| 402 | + } |
| 403 | + } |
| 404 | + for _, p := range y.Probes { |
| 405 | + if re.MatchString(p.Script) { |
| 406 | + keyIsUsed = true |
| 407 | + break |
| 408 | + } |
| 409 | + } |
| 410 | + for _, p := range y.CopyToHost { |
| 411 | + if re.MatchString(p.GuestFile) || re.MatchString(p.HostFile) { |
| 412 | + keyIsUsed = true |
| 413 | + break |
| 414 | + } |
| 415 | + } |
| 416 | + for _, p := range y.PortForwards { |
| 417 | + if re.MatchString(p.GuestSocket) || re.MatchString(p.HostSocket) { |
| 418 | + keyIsUsed = true |
| 419 | + break |
| 420 | + } |
| 421 | + } |
| 422 | + if !keyIsUsed { |
| 423 | + return fmt.Errorf("field `param` key %q is not used in any provision, probe, copyToHost, or portForward", key) |
| 424 | + } |
| 425 | + } |
| 426 | + return nil |
| 427 | +} |
| 428 | + |
388 | 429 | func validatePort(field string, port int) error {
|
389 | 430 | switch {
|
390 | 431 | case port < 0:
|
|
0 commit comments