mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
* [wip] try to reflect metadata types... probably wont work Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * refactor to add unit test to ensure there is coverage in the schema Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * [wip] generate metadata container Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add generation of metadata container struct for JSON schema generation Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix linting Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * update linter script to account for code generation Signed-off-by: Alex Goodman <alex.goodman@anchore.com> --------- Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/dave/jennifer/jen"
|
|
|
|
"github.com/anchore/syft/schema/json/internal"
|
|
)
|
|
|
|
// This program generates internal/generated.go.
|
|
|
|
const (
|
|
pkgImport = "github.com/anchore/syft/syft/pkg"
|
|
path = "internal/generated.go"
|
|
)
|
|
|
|
func main() {
|
|
typeNames, err := internal.AllSyftMetadataTypeNames()
|
|
if err != nil {
|
|
panic(fmt.Errorf("unable to get all metadata type names: %w", err))
|
|
}
|
|
|
|
fmt.Printf("updating metadata container object with %+v types\n", len(typeNames))
|
|
|
|
f := jen.NewFile("internal")
|
|
f.HeaderComment("DO NOT EDIT: generated by schema/json/generate/main.go")
|
|
f.ImportName(pkgImport, "pkg")
|
|
f.Comment("ArtifactMetadataContainer is a struct that contains all the metadata types for a package, as represented in the pkg.Package.Metadata field.")
|
|
f.Type().Id("ArtifactMetadataContainer").StructFunc(func(g *jen.Group) {
|
|
for _, typeName := range typeNames {
|
|
g.Id(typeName).Qual(pkgImport, typeName)
|
|
}
|
|
})
|
|
|
|
rendered := fmt.Sprintf("%#v", f)
|
|
|
|
fh, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
|
if err != nil {
|
|
panic(fmt.Errorf("unable to open file: %w", err))
|
|
}
|
|
_, err = fh.WriteString(rendered)
|
|
if err != nil {
|
|
panic(fmt.Errorf("unable to write file: %w", err))
|
|
}
|
|
if err := fh.Close(); err != nil {
|
|
panic(fmt.Errorf("unable to close file: %w", err))
|
|
}
|
|
}
|