mirror of
https://github.com/anchore/syft.git
synced 2026-02-12 02:26:42 +01:00
use the new DpkgMetadata struct in the parser
Signed-off-by: Alfredo Deza <adeza@anchore.com>
This commit is contained in:
parent
888cee3336
commit
ada11973b2
@ -6,66 +6,15 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom/pkg"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// TODO: consider keeping the remaining values as an embedded map
|
||||
type Entry struct {
|
||||
Package string `mapstructure:"Package"`
|
||||
Architecture string `mapstructure:"Architecture"`
|
||||
DependsPkgs string `mapstructure:"Depends"`
|
||||
InstalledSize string `mapstructure:"Installed-Size"`
|
||||
Maintainer string `mapstructure:"Maintainer"`
|
||||
Priority string `mapstructure:"Priority"`
|
||||
ProvidesPkgs string `mapstructure:"Provides"`
|
||||
RecommendsPkgs string `mapstructure:"Recommends"`
|
||||
ReplacesPkgs string `mapstructure:"Replaces"`
|
||||
Status string `mapstructure:"Status"`
|
||||
SuggestsPkgs string `mapstructure:"Suggests"`
|
||||
Version string `mapstructure:"Version"`
|
||||
ConfigFiles string `mapstructure:"Conffiles"`
|
||||
}
|
||||
|
||||
// dpkg-query recognized fields
|
||||
// Architecture
|
||||
// Bugs
|
||||
// Conffiles (internal)
|
||||
// Config-Version (internal)
|
||||
// Conflicts
|
||||
// Breaks
|
||||
// Depends
|
||||
// Description
|
||||
// Enhances
|
||||
// Essential
|
||||
// Filename (internal, front-end related)
|
||||
// Homepage
|
||||
// Installed-Size
|
||||
// MD5sum (internal, front-end related)
|
||||
// MSDOS-Filename (internal, front-end related)
|
||||
// Maintainer
|
||||
// Origin
|
||||
// Package
|
||||
// Pre-Depends
|
||||
// Priority
|
||||
// Provides
|
||||
// Recommends
|
||||
// Replaces
|
||||
// Revision (obsolete)
|
||||
// Section
|
||||
// Size (internal, front-end related)
|
||||
// Source
|
||||
// Status (internal)
|
||||
// Suggests
|
||||
// Tag (usually not in .deb but in repository Packages files)
|
||||
// Triggers-Awaited (internal)
|
||||
// Triggers-Pending (internal)
|
||||
// Version
|
||||
|
||||
var endOfPackages = fmt.Errorf("no more packages to read")
|
||||
|
||||
func ParseEntries(reader io.Reader) ([]Entry, error) {
|
||||
func ParseEntries(reader io.Reader) ([]pkg.DpkgMetadata, error) {
|
||||
buffedReader := bufio.NewReader(reader)
|
||||
var entries = make([]Entry, 0)
|
||||
var entries = make([]pkg.DpkgMetadata, 0)
|
||||
|
||||
for {
|
||||
entry, err := parseEntry(buffedReader)
|
||||
@ -81,8 +30,7 @@ func ParseEntries(reader io.Reader) ([]Entry, error) {
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
|
||||
func parseEntry(reader *bufio.Reader) (entry Entry, err error) {
|
||||
func parseEntry(reader *bufio.Reader) (entry pkg.DpkgMetadata, err error) {
|
||||
dpkgFields := make(map[string]string)
|
||||
var key string
|
||||
|
||||
@ -90,9 +38,9 @@ func parseEntry(reader *bufio.Reader) (entry Entry, err error) {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return Entry{}, endOfPackages
|
||||
return pkg.DpkgMetadata{}, endOfPackages
|
||||
}
|
||||
return Entry{}, err
|
||||
return pkg.DpkgMetadata{}, err
|
||||
}
|
||||
|
||||
line = strings.TrimRight(line, "\n")
|
||||
@ -100,7 +48,7 @@ func parseEntry(reader *bufio.Reader) (entry Entry, err error) {
|
||||
// empty line indicates end of entry
|
||||
if len(line) == 0 {
|
||||
// if the entry has not started, keep parsing lines
|
||||
if len(dpkgFields) == 0{
|
||||
if len(dpkgFields) == 0 {
|
||||
continue
|
||||
}
|
||||
break
|
||||
@ -110,12 +58,12 @@ func parseEntry(reader *bufio.Reader) (entry Entry, err error) {
|
||||
case strings.HasPrefix(line, " "):
|
||||
// a field-body continuation
|
||||
if len(key) == 0 {
|
||||
return Entry{}, fmt.Errorf("no match for continuation: line: '%s'", line)
|
||||
return pkg.DpkgMetadata{}, fmt.Errorf("no match for continuation: line: '%s'", line)
|
||||
}
|
||||
|
||||
val, ok := dpkgFields[key]
|
||||
if !ok {
|
||||
return Entry{}, fmt.Errorf("no previous key exists, expecting: %s", key)
|
||||
return pkg.DpkgMetadata{}, fmt.Errorf("no previous key exists, expecting: %s", key)
|
||||
}
|
||||
// concatenate onto previous value
|
||||
val = fmt.Sprintf("%s\n %s", val, strings.TrimSpace(line))
|
||||
@ -127,21 +75,20 @@ func parseEntry(reader *bufio.Reader) (entry Entry, err error) {
|
||||
val := strings.TrimSpace(line[i+1:])
|
||||
|
||||
if _, ok := dpkgFields[key]; ok {
|
||||
return Entry{}, fmt.Errorf("duplicate key discovered: %s", key)
|
||||
return pkg.DpkgMetadata{}, fmt.Errorf("duplicate key discovered: %s", key)
|
||||
}
|
||||
|
||||
dpkgFields[key] = val
|
||||
} else {
|
||||
return Entry{}, fmt.Errorf("cannot parse field from line: '%s'", line)
|
||||
return pkg.DpkgMetadata{}, fmt.Errorf("cannot parse field from line: '%s'", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = mapstructure.Decode(dpkgFields, &entry)
|
||||
if err != nil {
|
||||
return Entry{}, err
|
||||
return pkg.DpkgMetadata{}, err
|
||||
}
|
||||
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user