Skip to content

Commit 2325a1f

Browse files
committed
cmd/cdi: add validate --schema command line flag.
Add a --schema command line flag to allow overriding the default/builtin validation JSON schema name. Also, stop printing registry errors twice for 'validate'. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent b60ae60 commit 2325a1f

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

cmd/cdi/cmd/cdi-api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func cdiPrintRegistryErrors() {
345345
for path, specErrors := range cdiErrors {
346346
fmt.Printf("Spec file %s:\n", path)
347347
for idx, err := range specErrors {
348-
fmt.Printf(" %d: %v", idx, err)
348+
fmt.Printf(" %d: %v\n", idx, strings.TrimRight(err.Error(), "\n"))
349349
}
350350
}
351351
}

cmd/cdi/cmd/root.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717
package cmd
1818

1919
import (
20+
"fmt"
21+
"os"
22+
2023
"github.com/spf13/cobra"
2124

2225
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
2326
)
2427

25-
var specDirs []string
28+
var (
29+
specDirs []string
30+
schemaName string
31+
)
2632

2733
// rootCmd represents the base command when called without any subcommands
2834
var rootCmd = &cobra.Command{
@@ -48,9 +54,16 @@ func Execute() {
4854
func init() {
4955
cobra.OnInitialize(initSpecDirs)
5056
rootCmd.PersistentFlags().StringSliceVarP(&specDirs, "spec-dirs", "d", nil, "directories to scan for CDI Spec files")
57+
rootCmd.PersistentFlags().StringVarP(&schemaName, "schema", "s", "builtin", "JSON schema to use for validation")
5158
}
5259

5360
func initSpecDirs() {
61+
err := cdi.SetSchema(schemaName)
62+
if err != nil {
63+
fmt.Printf("failed to load JSON schema %s: %v\n", schemaName, err)
64+
os.Exit(1)
65+
}
66+
5467
if len(specDirs) > 0 {
5568
cdi.GetRegistry(cdi.WithSpecDirs(specDirs...))
5669
if len(cdi.GetRegistry().GetErrors()) > 0 {

cmd/cdi/cmd/validate.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ were reported by the registry.`,
4040
return
4141
}
4242

43-
fmt.Printf("CDI Registry has errors:\n")
44-
cdiPrintRegistryErrors()
45-
4643
os.Exit(1)
4744
},
4845
}

0 commit comments

Comments
 (0)