mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 00:43:20 +01:00
add encode and decode to the syft lib interface
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
parent
bcdebcadaf
commit
12b44af471
33
syft/lib.go
33
syft/lib.go
@ -17,11 +17,15 @@ Similar to the cataloging process, Linux distribution identification is also per
|
|||||||
package syft
|
package syft
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/anchore/syft/internal/bus"
|
"github.com/anchore/syft/internal/bus"
|
||||||
|
"github.com/anchore/syft/internal/formats"
|
||||||
"github.com/anchore/syft/internal/log"
|
"github.com/anchore/syft/internal/log"
|
||||||
"github.com/anchore/syft/syft/distro"
|
"github.com/anchore/syft/syft/distro"
|
||||||
|
"github.com/anchore/syft/syft/format"
|
||||||
"github.com/anchore/syft/syft/logger"
|
"github.com/anchore/syft/syft/logger"
|
||||||
"github.com/anchore/syft/syft/pkg"
|
"github.com/anchore/syft/syft/pkg"
|
||||||
"github.com/anchore/syft/syft/pkg/cataloger"
|
"github.com/anchore/syft/syft/pkg/cataloger"
|
||||||
@ -67,6 +71,35 @@ func CatalogPackages(src *source.Source, scope source.Scope) (*pkg.Catalog, *dis
|
|||||||
return catalog, theDistro, nil
|
return catalog, theDistro, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: encapsulate input data into common sbom document object
|
||||||
|
func Encode(catalog *pkg.Catalog, metadata *source.Metadata, dist *distro.Distro, option format.Option) ([]byte, error) {
|
||||||
|
f := formats.ByOption(option)
|
||||||
|
if f == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
buff := bytes.Buffer{}
|
||||||
|
|
||||||
|
return buff.Bytes(), f.Encode(&buff, catalog, dist, metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: encapsulate return data into common sbom document object
|
||||||
|
func Decode(reader io.Reader) (*pkg.Catalog, *source.Metadata, *distro.Distro, format.Option, error) {
|
||||||
|
by, err := io.ReadAll(reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, nil, format.UnknownOption, fmt.Errorf("unable to read sbom: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := formats.Identify(by)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, nil, format.UnknownOption, fmt.Errorf("unable to detect format: %w", err)
|
||||||
|
}
|
||||||
|
if f == nil {
|
||||||
|
return nil, nil, nil, format.UnknownOption, fmt.Errorf("unable to identify format")
|
||||||
|
}
|
||||||
|
c, m, d, err := f.Decode(bytes.NewReader(by))
|
||||||
|
return c, m, d, f.Option, err
|
||||||
|
}
|
||||||
|
|
||||||
// SetLogger sets the logger object used for all syft logging calls.
|
// SetLogger sets the logger object used for all syft logging calls.
|
||||||
func SetLogger(logger logger.Logger) {
|
func SetLogger(logger logger.Logger) {
|
||||||
log.Log = logger
|
log.Log = logger
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user