mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
* add new format pattern Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add syftjson format Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add internal formats helper Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add SBOM encode/decode to lib API Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * remove json presenter + update presenter tests to use common utils Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * remove presenter format enum type + add formats shim in presenter helper Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add MustCPE helper for tests Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * update usage of format enum Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add test fixtures for encode/decode tests Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix integration test Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * migrate format detection to use reader Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * address review comments Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package pkg
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/facebookincubator/nvdtools/wfn"
|
|
)
|
|
|
|
type CPE = wfn.Attributes
|
|
|
|
func NewCPE(cpeStr string) (CPE, error) {
|
|
value, err := wfn.Parse(cpeStr)
|
|
if err != nil {
|
|
return CPE{}, fmt.Errorf("failed to parse CPE=%q: %w", cpeStr, err)
|
|
}
|
|
|
|
if value == nil {
|
|
return CPE{}, fmt.Errorf("failed to parse CPE=%q", cpeStr)
|
|
}
|
|
|
|
// we need to compare the raw data since we are constructing CPEs in other locations
|
|
value.Vendor = normalizeCpeField(value.Vendor)
|
|
value.Product = normalizeCpeField(value.Product)
|
|
value.Language = normalizeCpeField(value.Language)
|
|
value.Version = normalizeCpeField(value.Version)
|
|
value.TargetSW = normalizeCpeField(value.TargetSW)
|
|
value.Part = normalizeCpeField(value.Part)
|
|
value.Edition = normalizeCpeField(value.Edition)
|
|
value.Other = normalizeCpeField(value.Other)
|
|
value.SWEdition = normalizeCpeField(value.SWEdition)
|
|
value.TargetHW = normalizeCpeField(value.TargetHW)
|
|
value.Update = normalizeCpeField(value.Update)
|
|
|
|
return *value, nil
|
|
}
|
|
|
|
func MustCPE(cpeStr string) CPE {
|
|
c, err := NewCPE(cpeStr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func normalizeCpeField(field string) string {
|
|
// keep dashes and forward slashes unescaped
|
|
return strings.ReplaceAll(wfn.StripSlashes(field), `\/`, "/")
|
|
}
|