-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generalize parser for all format globs
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
8 changed files
with
206 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,18 @@ | ||
package syft | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/anchore/syft/syft/formats" | ||
"github.com/anchore/syft/syft/sbom" | ||
) | ||
|
||
// Encode takes all SBOM elements and a format option and encodes an SBOM document. | ||
// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 | ||
func Encode(s sbom.SBOM, f sbom.Format) ([]byte, error) { | ||
buff := bytes.Buffer{} | ||
|
||
if err := f.Encode(&buff, s); err != nil { | ||
return nil, fmt.Errorf("unable to encode sbom: %w", err) | ||
} | ||
|
||
return buff.Bytes(), nil | ||
return formats.Encode(s, f) | ||
} | ||
|
||
// Decode takes a reader for an SBOM and generates all internal SBOM elements. | ||
// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 | ||
func Decode(reader io.Reader) (*sbom.SBOM, sbom.Format, error) { | ||
by, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to read sbom: %w", err) | ||
} | ||
|
||
f := IdentifyFormat(by) | ||
if f == nil { | ||
return nil, nil, fmt.Errorf("unable to identify format") | ||
} | ||
|
||
s, err := f.Decode(bytes.NewReader(by)) | ||
return s, f, err | ||
return formats.Decode(reader) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package formats | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/anchore/syft/syft/formats/cyclonedxjson" | ||
"github.com/anchore/syft/syft/formats/cyclonedxxml" | ||
"github.com/anchore/syft/syft/formats/github" | ||
"github.com/anchore/syft/syft/formats/spdx22json" | ||
"github.com/anchore/syft/syft/formats/spdx22tagvalue" | ||
"github.com/anchore/syft/syft/formats/syftjson" | ||
"github.com/anchore/syft/syft/formats/table" | ||
"github.com/anchore/syft/syft/formats/template" | ||
"github.com/anchore/syft/syft/formats/text" | ||
"github.com/anchore/syft/syft/sbom" | ||
) | ||
|
||
func Formats() []sbom.Format { | ||
return []sbom.Format{ | ||
syftjson.Format(), | ||
cyclonedxxml.Format(), | ||
cyclonedxjson.Format(), | ||
github.Format(), | ||
spdx22tagvalue.Format(), | ||
spdx22json.Format(), | ||
table.Format(), | ||
text.Format(), | ||
template.Format(), | ||
} | ||
} | ||
|
||
func Identify(by []byte) sbom.Format { | ||
for _, f := range Formats() { | ||
if err := f.Validate(bytes.NewReader(by)); err != nil { | ||
continue | ||
} | ||
return f | ||
} | ||
return nil | ||
} | ||
|
||
func ByName(name string) sbom.Format { | ||
cleanName := cleanFormatName(name) | ||
for _, f := range Formats() { | ||
if cleanFormatName(string(f.ID())) == cleanName { | ||
return f | ||
} | ||
} | ||
|
||
// handle any aliases for any supported format | ||
switch cleanName { | ||
case "json", "syftjson": | ||
return ByID(syftjson.ID) | ||
case "cyclonedx", "cyclone", "cyclonedxxml": | ||
return ByID(cyclonedxxml.ID) | ||
case "cyclonedxjson": | ||
return ByID(cyclonedxjson.ID) | ||
case "github", "githubjson": | ||
return ByID(github.ID) | ||
case "spdx", "spdxtv", "spdxtagvalue": | ||
return ByID(spdx22tagvalue.ID) | ||
case "spdxjson": | ||
return ByID(spdx22json.ID) | ||
case "table": | ||
return ByID(table.ID) | ||
case "text": | ||
return ByID(text.ID) | ||
case "template": | ||
ByID(template.ID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IDs() (ids []sbom.FormatID) { | ||
for _, f := range Formats() { | ||
ids = append(ids, f.ID()) | ||
} | ||
return ids | ||
} | ||
|
||
func ByID(id sbom.FormatID) sbom.Format { | ||
for _, f := range Formats() { | ||
if f.ID() == id { | ||
return f | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func cleanFormatName(name string) string { | ||
r := strings.NewReplacer("-", "", "_", "") | ||
return strings.ToLower(r.Replace(name)) | ||
} | ||
|
||
// Encode takes all SBOM elements and a format option and encodes an SBOM document. | ||
func Encode(s sbom.SBOM, f sbom.Format) ([]byte, error) { | ||
buff := bytes.Buffer{} | ||
|
||
if err := f.Encode(&buff, s); err != nil { | ||
return nil, fmt.Errorf("unable to encode sbom: %w", err) | ||
} | ||
|
||
return buff.Bytes(), nil | ||
} | ||
|
||
// Decode takes a reader for an SBOM and generates all internal SBOM elements. | ||
func Decode(reader io.Reader) (*sbom.SBOM, sbom.Format, error) { | ||
by, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("unable to read sbom: %w", err) | ||
} | ||
|
||
f := Identify(by) | ||
if f == nil { | ||
return nil, nil, fmt.Errorf("unable to identify format") | ||
} | ||
|
||
s, err := f.Decode(bytes.NewReader(by)) | ||
return s, f, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.