Skip to content

Commit

Permalink
epss: update Fetch
Browse files Browse the repository at this point in the history
Signed-off-by: daynewlee <[email protected]>
  • Loading branch information
daynewlee committed Nov 19, 2024
1 parent a01fa33 commit 08bb4f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
11 changes: 4 additions & 7 deletions enricher/epss/epss.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,16 @@ func (e *Enricher) FetchEnrichment(ctx context.Context, _ driver.Fingerprint) (i
enc := json.NewEncoder(out)
totalCVEs := 0

// get headers
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())

if strings.HasPrefix(line, "#") || line == "" {
continue // Skip comment or empty lines
}
headers = strings.Split(line, ",")
break
}

for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") || line == "" {
if headers == nil {
// Store headers on first data line
headers = strings.Split(line, ",")
continue
}

Expand Down
51 changes: 51 additions & 0 deletions enricher/epss/epss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package epss
import (
"compress/gzip"
"context"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/zlog"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"strings"
"testing"
)

Expand Down Expand Up @@ -291,3 +294,51 @@ func (f *fakeGetter) GetEnrichment(ctx context.Context, tags []string) ([]driver
}
return f.res, nil
}

func parseCSV(filePath string) ([]map[string]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

reader := csv.NewReader(file)
reader.Comma = ',' // Set comma as the delimiter (can be customized)

var items []map[string]string
var headers []string // Declare headers outside the if block

for {
record, err := reader.Read()
if err == io.EOF {
break // Reached end of file
}
if err != nil {
return nil, err // Handle other errors
}

if len(record) == 0 || strings.HasPrefix(record[0], "#") {
continue // Skip comment or empty lines
}

if items == nil {
// Store headers on first data line and initialize items slice
items = make([]map[string]string, 0)
headers = record
continue
}

if len(record) != len(headers) {
log.Printf("warning: skipping line with mismatched fields: %s\n", record)
continue // Skip lines with mismatched number of fields
}

item := make(map[string]string)
for i, value := range record {
item[headers[i]] = value // Use headers as map keys
}
items = append(items, item)
}

return items, nil
}

0 comments on commit 08bb4f5

Please sign in to comment.