Skip to content

Commit

Permalink
Added SCORE spec validation (#33)
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Yarshevich <yarshevich@gmail.com>
  • Loading branch information
ghen committed Jun 1, 2023
1 parent 2243a47 commit 9bff21d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions e2e-tests/resources/outputs/run --help-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Flags:
-h, --help help for run
-o, --output string Output file
--overrides string Overrides SCORE file (default "./overrides.score.yaml")
--skip-validation DEPRECATED: Disables Score file schema validation
--verbose Enable diagnostic messages (written to STDERR)
1 change: 1 addition & 0 deletions e2e-tests/resources/outputs/run --verbose-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Flags:
-h, --help help for run
-o, --output string Output file
--overrides string Overrides SCORE file (default "./overrides.score.yaml")
--skip-validation DEPRECATED: Disables Score file schema validation
--verbose Enable diagnostic messages (written to STDERR)

open ./score.yaml: no such file or directory
1 change: 1 addition & 0 deletions e2e-tests/resources/outputs/run-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Flags:
-h, --help help for run
-o, --output string Output file
--overrides string Overrides SCORE file (default "./overrides.score.yaml")
--skip-validation DEPRECATED: Disables Score file schema validation
--verbose Enable diagnostic messages (written to STDERR)

open ./score.yaml: no such file or directory
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ require (
github.com/compose-spec/compose-go v1.6.0
github.com/imdario/mergo v0.3.13
github.com/mitchellh/mapstructure v1.5.0
github.com/score-spec/score-go v0.0.0-20221019054335-3510902b5f8b
github.com/score-spec/score-go v0.0.0-20230601114155-58fa99cb56f8
github.com/spf13/cobra v1.6.0
github.com/stretchr/testify v1.8.0
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -20,5 +21,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
)
11 changes: 9 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3I
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/score-spec/score-go v0.0.0-20221019054335-3510902b5f8b h1:Ws6TNwu+OuoR+K7C3fKLCuaeyAiGU0G7Xkaz9UUygWA=
github.com/score-spec/score-go v0.0.0-20221019054335-3510902b5f8b/go.mod h1:eNU0evgibNfV6ESUfRKjWcfGPmd92dI8dsUN/GBouZs=
github.com/score-spec/score-go v0.0.0-20230601114155-58fa99cb56f8 h1:gIOGix8DrqtGbEEBPimUON83Bk+AVnZRpanCKsWXa3s=
github.com/score-spec/score-go v0.0.0-20230601114155-58fa99cb56f8/go.mod h1:kqDzGrkDasa4D1A9MWgHPVPoRVa+zZgFijYOZNDLSpM=
github.com/spf13/cobra v1.6.0 h1:42a0n6jwCot1pUmomAp4T7DeMD+20LFv4Q54pxLf2LI=
github.com/spf13/cobra v1.6.0/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
25 changes: 23 additions & 2 deletions internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The Apache Software Foundation (http://www.apache.org/).
package command

import (
"errors"
"fmt"
"io"
"log"
Expand All @@ -17,10 +18,12 @@ import (
"github.com/compose-spec/compose-go/types"
"github.com/imdario/mergo"
"github.com/spf13/cobra"
"github.com/xeipuuv/gojsonschema"

"github.com/score-spec/score-compose/internal/compose"

loader "github.com/score-spec/score-go/loader"
schema "github.com/score-spec/score-go/schema"
score "github.com/score-spec/score-go/types"
)

Expand All @@ -36,7 +39,8 @@ var (
envFile string
buildCtx string

verbose bool
skipValidation bool
verbose bool
)

func init() {
Expand All @@ -46,6 +50,7 @@ func init() {
runCmd.Flags().StringVar(&envFile, "env-file", "", "Location to store sample .env file")
runCmd.Flags().StringVar(&buildCtx, "build", "", "Replaces 'image' name with compose 'build' instruction")

runCmd.Flags().BoolVar(&skipValidation, "skip-validation", false, "DEPRECATED: Disables Score file schema validation")
runCmd.Flags().BoolVar(&verbose, "verbose", false, "Enable diagnostic messages (written to STDERR)")

rootCmd.AddCommand(runCmd)
Expand Down Expand Up @@ -102,7 +107,23 @@ func run(cmd *cobra.Command, args []string) error {

// Validate SCORE spec
//
log.Print("Validating SCORE spec...\n")
if !skipValidation {
log.Print("Validating SCORE spec...\n")
if res, err := schema.Validate(gojsonschema.NewGoLoader(srcMap)); err != nil {
return fmt.Errorf("validating workload spec: %w", err)
} else if !res.Valid() {
for _, valErr := range res.Errors() {
log.Println(valErr.String())
if err == nil {
err = errors.New(valErr.String())
}
}
return fmt.Errorf("validating workload spec: %w", err)
}
}

// Convert SCORE spec
//
var spec score.WorkloadSpec
if err = loader.MapSpec(&spec, srcMap); err != nil {
return fmt.Errorf("validating workload spec: %w", err)
Expand Down

0 comments on commit 9bff21d

Please sign in to comment.