Skip to content

Commit

Permalink
Add vervet.LoadVersions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmars committed Jan 14, 2022
1 parent bf75b03 commit 6a8108b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/fs"

"github.com/getkin/kin-openapi/openapi3"
"github.com/ghodss/yaml"
)

Expand Down Expand Up @@ -40,3 +42,35 @@ func WithGeneratedComment(yamlBuf []byte) ([]byte, error) {
}
return buf.Bytes(), nil
}

// LoadVersions loads all Vervet-compiled and versioned API specs from a
// filesystem root and returns them.
func LoadVersions(root fs.FS) ([]*openapi3.T, error) {
var versions []*openapi3.T
specFiles, err := fs.Glob(root, "*/spec.json")
if err != nil {
return nil, err
}
for _, specFile := range specFiles {
specData, err := fs.ReadFile(root, specFile)
if err != nil {
return nil, err
}
l := openapi3.NewLoader()
t, err := l.LoadFromData(specData)
if err != nil {
return nil, err
}
if _, err := ExtensionString(t.ExtensionProps, ExtSnykApiVersion); IsExtensionNotFound(err) {
// Not a versioned OpenAPI spec, skip it
continue
} else if err != nil {
return nil, err
}
versions = append(versions, t)
}
if len(versions) == 0 {
return nil, ErrNoMatchingVersion
}
return versions, nil
}

0 comments on commit 6a8108b

Please sign in to comment.