github.com/wolfi-dev/wolfictl@v0.16.11/pkg/configs/advisory/v1/advisory.go (about)

     1  package v1
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/fs"
     7  	"time"
     8  
     9  	"github.com/openvex/go-vex/pkg/vex"
    10  	"github.com/wolfi-dev/wolfictl/pkg/configs"
    11  	"github.com/wolfi-dev/wolfictl/pkg/configs/rwfs"
    12  	"gopkg.in/yaml.v3"
    13  )
    14  
    15  func NewIndex(ctx context.Context, fsys rwfs.FS) (*configs.Index[Document], error) {
    16  	return configs.NewIndex[Document](ctx, fsys, newConfigurationDecodeFunc(fsys))
    17  }
    18  
    19  func NewIndexFromPaths(ctx context.Context, fsys rwfs.FS, paths ...string) (*configs.Index[Document], error) {
    20  	return configs.NewIndexFromPaths[Document](ctx, fsys, newConfigurationDecodeFunc(fsys), paths...)
    21  }
    22  
    23  func newConfigurationDecodeFunc(fsys fs.FS) func(context.Context, string) (*Document, error) {
    24  	return func(_ context.Context, path string) (*Document, error) {
    25  		file, err := fsys.Open(path)
    26  		if err != nil {
    27  			return nil, err
    28  		}
    29  
    30  		return DecodeDocument(file)
    31  	}
    32  }
    33  
    34  func DecodeDocument(r io.Reader) (*Document, error) {
    35  	doc := &Document{}
    36  	decoder := yaml.NewDecoder(r)
    37  	decoder.KnownFields(true)
    38  	err := decoder.Decode(doc)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return doc, nil
    44  }
    45  
    46  type Document struct {
    47  	Package Package `yaml:"package"`
    48  
    49  	Advisories Advisories `yaml:"advisories,omitempty"`
    50  }
    51  
    52  func (d Document) Name() string {
    53  	return d.Package.Name
    54  }
    55  
    56  type Package struct {
    57  	Name string `yaml:"name"`
    58  }
    59  
    60  type Advisories map[string][]Entry
    61  
    62  type Entry struct {
    63  	Timestamp       time.Time         `yaml:"timestamp"`
    64  	Status          vex.Status        `yaml:"status"`
    65  	Justification   vex.Justification `yaml:"justification,omitempty"`
    66  	ImpactStatement string            `yaml:"impact,omitempty"`
    67  	ActionStatement string            `yaml:"action,omitempty"`
    68  	FixedVersion    string            `yaml:"fixed-version,omitempty"`
    69  }