github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/internal/sourcemetadata/generate/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/dave/jennifer/jen"
     8  
     9  	"github.com/anchore/syft/syft/internal/sourcemetadata"
    10  )
    11  
    12  // This program is invoked from syft/internal and generates sourcemetadata/generated.go
    13  
    14  const (
    15  	srcImport = "github.com/anchore/syft/syft/source"
    16  	path      = "sourcemetadata/generated.go"
    17  )
    18  
    19  func main() {
    20  	typeNames, err := sourcemetadata.DiscoverTypeNames()
    21  	if err != nil {
    22  		panic(fmt.Errorf("unable to get all metadata type names: %w", err))
    23  	}
    24  
    25  	fmt.Printf("updating source metadata type list with %+v types\n", len(typeNames))
    26  
    27  	f := jen.NewFile("sourcemetadata")
    28  	f.HeaderComment("DO NOT EDIT: generated by syft/internal/sourcemetadata/generate/main.go")
    29  	f.ImportName(srcImport, "source")
    30  	f.Comment("AllTypes returns a list of all source metadata types that syft supports (that are represented in the source.Description.Metadata field).")
    31  
    32  	f.Func().Id("AllTypes").Params().Index().Any().BlockFunc(func(g *jen.Group) {
    33  		g.ReturnFunc(func(g *jen.Group) {
    34  			g.Index().Any().ValuesFunc(func(g *jen.Group) {
    35  				for _, typeName := range typeNames {
    36  					g.Qual(srcImport, typeName).Values()
    37  				}
    38  			})
    39  		})
    40  	})
    41  
    42  	rendered := fmt.Sprintf("%#v", f)
    43  
    44  	fh, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
    45  	if err != nil {
    46  		panic(fmt.Errorf("unable to open file: %w", err))
    47  	}
    48  	_, err = fh.WriteString(rendered)
    49  	if err != nil {
    50  		panic(fmt.Errorf("unable to write file: %w", err))
    51  	}
    52  	if err := fh.Close(); err != nil {
    53  		panic(fmt.Errorf("unable to close file: %w", err))
    54  	}
    55  }