-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor[validate]: move validations to cargo package
- Loading branch information
Showing
13 changed files
with
206 additions
and
247 deletions.
There are no files selected for viewing
5 changes: 0 additions & 5 deletions
5
internal/commands/testdata/validate/floating-release/Kilnfile
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
internal/commands/testdata/validate/floating-release/Kilnfile.lock
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
internal/commands/testdata/validate/invalid-constraint/Kilnfile
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
internal/commands/testdata/validate/invalid-constraint/Kilnfile.lock
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
internal/commands/testdata/validate/pinned-release/Kilnfile.lock
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
internal/commands/testdata/validate/wrong-version-type/Kilnfile
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
internal/commands/testdata/validate/wrong-version-type/Kilnfile.lock
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cargo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Masterminds/semver" | ||
) | ||
|
||
func Validate(spec Kilnfile, lock KilnfileLock) []error { | ||
var result []error | ||
|
||
for index, componentSpec := range spec.Releases { | ||
if componentSpec.Name == "" { | ||
result = append(result, fmt.Errorf("spec at index %d missing name", index)) | ||
continue | ||
} | ||
|
||
componentLock, err := lock.FindReleaseWithName(componentSpec.Name) | ||
if err != nil { | ||
result = append(result, | ||
fmt.Errorf("component spec for release %q not found in lock", componentSpec.Name)) | ||
continue | ||
} | ||
|
||
if err := checkComponentVersionsAndConstraint(componentSpec, componentLock, index); err != nil { | ||
result = append(result, err) | ||
} | ||
} | ||
|
||
if len(result) > 0 { | ||
return result | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkComponentVersionsAndConstraint(spec ComponentSpec, lock ComponentLock, index int) error { | ||
v, err := semver.NewVersion(lock.Version) | ||
if err != nil { | ||
return fmt.Errorf("spec %s (index %d in Kilnfile.lock) has invalid lock version %q: %w", | ||
spec.Name, index, lock.Version, err) | ||
} | ||
|
||
if spec.Version != "" { | ||
c, err := semver.NewConstraint(spec.Version) | ||
if err != nil { | ||
return fmt.Errorf("spec %s (index %d in Kilnfile) has invalid version constraint: %w", | ||
spec.Name, index, err) | ||
} | ||
|
||
matches, errs := c.Validate(v) | ||
if !matches { | ||
return fmt.Errorf("spec %s version in lock %q does not match constraint %q: %v", | ||
spec.Name, lock.Version, spec.Version, errs) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.