Add example how to use kubeconform as a library

This commit is contained in:
Yann Hamon 2020-11-15 10:01:36 +01:00
parent 4672ded043
commit 4e96b44a8b
7 changed files with 93 additions and 18 deletions

26
examples/main.go Normal file
View file

@ -0,0 +1,26 @@
package main
// WARNING: API of Kubeconform is still under development and not yet
// considered stable
import (
"github.com/yannh/kubeconform/pkg/validator"
"log"
"os"
)
func main() {
filepath := "../fixtures/valid.yaml"
f, err := os.Open(filepath)
if err != nil {
log.Fatalf("failed opening %s: %s", filepath, err)
}
v := validator.New(nil, validator.Opts{Strict: true})
for i, res := range v.Validate(filepath, f) { // A file might contain multiple resources
// File starts with ---, the parser assumes a first empty resource
if res.Status != validator.Valid && res.Status != validator.Empty {
log.Fatalf("resource %d in file %s is not valid: %d, %s", i, filepath, res.Status, res.Err)
}
}
}