chore: update CLI to CLIO (#2001)

Signed-off-by: Keith Zantow <kzantow@gmail.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Keith Zantow
2023-08-29 15:52:26 -04:00
committed by GitHub
co-authored by Alex Goodman
parent b03e9c6868
commit 2b7a9d0be3
95 changed files with 1680 additions and 3192 deletions
+1 -6
View File
@@ -6,12 +6,10 @@ package event
import (
"github.com/wagoodman/go-partybus"
"github.com/anchore/syft/internal"
)
const (
typePrefix = internal.ApplicationName
typePrefix = "syft"
cliTypePrefix = typePrefix + "-cli"
// Events from the syft library
@@ -48,7 +46,4 @@ const (
// CLINotification is a partybus event that occurs when auxiliary information is ready for presentation to stderr
CLINotification partybus.EventType = cliTypePrefix + "-notification"
// CLIExit is a partybus event that occurs when an analysis result is ready for final presentation
CLIExit partybus.EventType = cliTypePrefix + "-exit-event"
)
+9 -17
View File
@@ -144,30 +144,22 @@ func ParseAttestationStartedEvent(e partybus.Event) (io.Reader, progress.Progres
// CLI event types
func ParseCLIExit(e partybus.Event) (func() error, error) {
if err := checkEventType(e.Type, event.CLIExit); err != nil {
type UpdateCheck struct {
New string
Current string
}
func ParseCLIAppUpdateAvailable(e partybus.Event) (*UpdateCheck, error) {
if err := checkEventType(e.Type, event.CLIAppUpdateAvailable); err != nil {
return nil, err
}
fn, ok := e.Value.(func() error)
updateCheck, ok := e.Value.(UpdateCheck)
if !ok {
return nil, newPayloadErr(e.Type, "Value", e.Value)
}
return fn, nil
}
func ParseCLIAppUpdateAvailable(e partybus.Event) (string, error) {
if err := checkEventType(e.Type, event.CLIAppUpdateAvailable); err != nil {
return "", err
}
newVersion, ok := e.Value.(string)
if !ok {
return "", newPayloadErr(e.Type, "Value", e.Value)
}
return newVersion, nil
return &updateCheck, nil
}
func ParseCLIReport(e partybus.Event) (string, string, error) {
@@ -7,7 +7,6 @@ import (
"github.com/google/uuid"
"golang.org/x/exp/slices"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/cpe"
@@ -24,7 +23,7 @@ func ToFormatModel(s sbom.SBOM) *cyclonedx.BOM {
// https://github.com/CycloneDX/specification/blob/master/schema/bom-1.3-strict.schema.json#L36
// "pattern": "^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
cdxBOM.SerialNumber = uuid.New().URN()
cdxBOM.Metadata = toBomDescriptor(internal.ApplicationName, s.Descriptor.Version, s.Source)
cdxBOM.Metadata = toBomDescriptor(s.Descriptor.Name, s.Descriptor.Version, s.Source)
packages := s.Artifacts.Packages.Sorted()
components := make([]cyclonedx.Component, len(packages))
@@ -8,7 +8,7 @@ import (
"github.com/google/uuid"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)
@@ -18,12 +18,12 @@ const (
inputFile = "file"
)
func DocumentNameAndNamespace(src source.Description) (string, string) {
func DocumentNameAndNamespace(src source.Description, desc sbom.Descriptor) (string, string) {
name := DocumentName(src)
return name, DocumentNamespace(name, src)
return name, DocumentNamespace(name, src, desc)
}
func DocumentNamespace(name string, src source.Description) string {
func DocumentNamespace(name string, src source.Description, desc sbom.Descriptor) string {
name = cleanName(name)
input := "unknown-source-type"
switch src.Metadata.(type) {
@@ -44,7 +44,7 @@ func DocumentNamespace(name string, src source.Description) string {
u := url.URL{
Scheme: "https",
Host: "anchore.com",
Path: path.Join(internal.ApplicationName, identifier),
Path: path.Join(desc.Name, identifier),
}
return u.String()
@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)
@@ -55,9 +56,12 @@ func Test_documentNamespace(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := DocumentNamespace(test.inputName, test.src)
actual := DocumentNamespace(test.inputName, test.src, sbom.Descriptor{
Name: "syft",
})
// note: since the namespace ends with a UUID we check the prefix
assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("actual namespace %q", actual))
assert.True(t, strings.HasPrefix(actual, test.expected), fmt.Sprintf("expected prefix: '%s' got: '%s'", test.expected, actual))
// track each scheme tested (passed or not)
tracker.Tested(t, test.src.Metadata)
@@ -44,7 +44,7 @@ const (
//
//nolint:funlen
func ToFormatModel(s sbom.SBOM) *spdx.Document {
name, namespace := DocumentNameAndNamespace(s.Source)
name, namespace := DocumentNameAndNamespace(s.Source, s.Descriptor)
packages := toPackages(s.Artifacts.Packages, s)
@@ -136,7 +136,7 @@ func ToFormatModel(s sbom.SBOM) *spdx.Document {
CreatorType: "Organization",
},
{
Creator: internal.ApplicationName + "-" + s.Descriptor.Version,
Creator: s.Descriptor.Name + "-" + s.Descriptor.Version,
CreatorType: "Tool",
},
},
+1 -2
View File
@@ -8,7 +8,6 @@ import (
"github.com/mholt/archiver/v3"
"github.com/anchore/packageurl-go"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sbom"
@@ -26,7 +25,7 @@ func toGithubModel(s *sbom.SBOM) DependencySnapshot {
Version: 0,
// TODO allow property input to specify the Job, Sha, and Ref
Detector: DetectorMetadata{
Name: internal.ApplicationName,
Name: s.Descriptor.Name,
URL: "https://github.com/anchore/syft",
Version: v,
},
+3
View File
@@ -18,6 +18,9 @@ import (
func sbomFixture() sbom.SBOM {
s := sbom.SBOM{
Descriptor: sbom.Descriptor{
Name: "syft",
},
Source: source.Description{
Metadata: source.StereoscopeImageSourceMetadata{
UserInput: "ubuntu:18.04",
+11 -7
View File
@@ -4,7 +4,7 @@ import (
"bytes"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -64,12 +64,16 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
if cfg.IsJSON {
require.JSONEq(t, string(expected), string(actual))
} else if !bytes.Equal(expected, actual) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(expected), string(actual), true)
t.Logf("len: %d\nexpected: %s", len(expected), expected)
t.Logf("len: %d\nactual: %s", len(actual), actual)
t.Errorf("mismatched output:\n%s", dmp.DiffPrettyText(diffs))
} else {
requireEqual(t, expected, actual)
}
}
func requireEqual(t *testing.T, expected any, actual any) {
if diff := cmp.Diff(expected, actual); diff != "" {
t.Logf("expected: %s", expected)
t.Logf("actual: %s", actual)
t.Fatalf("mismatched output: %s", diff)
}
}
+1 -1
View File
@@ -115,7 +115,7 @@ func redactor(values ...string) testutils.Redactor {
`Created: .*`: "Created: redacted",
// each SBOM reports a unique documentNamespace when generated, this is not useful for snapshot testing
`DocumentNamespace: https://anchore.com/syft/.*`: "DocumentNamespace: redacted",
`DocumentNamespace: https://anchore.com/.*`: "DocumentNamespace: redacted",
// the license list will be updated periodically, the value here should not be directly tested in snapshot tests
`LicenseListVersion: .*`: "LicenseListVersion: redacted",
+1 -2
View File
@@ -7,7 +7,6 @@ import (
rpmdb "github.com/knqyf263/go-rpmdb/pkg"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
@@ -18,7 +17,7 @@ import (
// parseRpmDb parses an "Packages" RPM DB and returns the Packages listed within it.
func parseRpmDB(resolver file.Resolver, env *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
f, err := os.CreateTemp("", internal.ApplicationName+"-rpmdb")
f, err := os.CreateTemp("", "rpmdb")
if err != nil {
return nil, nil, fmt.Errorf("failed to create temp rpmdb file: %w", err)
}