Skip to content

Commit

Permalink
Merge pull request #665 from wolfi-dev/add-import
Browse files Browse the repository at this point in the history
add import yaml function
  • Loading branch information
rawlingsj committed Mar 4, 2024
2 parents 941b78a + 685e483 commit 2281dff
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
4 changes: 1 addition & 3 deletions go.mod
@@ -1,8 +1,6 @@
module github.com/wolfi-dev/wolfictl

go 1.21.3

toolchain go1.21.5
go 1.21.7

require (
chainguard.dev/apko v0.14.1-0.20240227175428-53cb29ff83c9
Expand Down
63 changes: 63 additions & 0 deletions pkg/advisory/import.go
@@ -0,0 +1,63 @@
package advisory

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v3"

"github.com/wolfi-dev/wolfictl/pkg/configs"
v2 "github.com/wolfi-dev/wolfictl/pkg/configs/advisory/v2"
rwos "github.com/wolfi-dev/wolfictl/pkg/configs/rwfs/os"
)

// ImporAdvisoriesYAML import and yaml Advisories data and present as a config index struct
func ImporAdvisoriesYAML(inputData string) (tempDir string, documents *configs.Index[v2.Document], err error) {
inputAdv, err := os.ReadFile(inputData)
if err != nil {
return "", nil, fmt.Errorf("unable to create output file: %v", err)
}

yamlDocs := bytes.Split(inputAdv, []byte("\n---\n"))
// Unmarshal YAML documents
var docs []v2.Document
for _, doc := range yamlDocs {
var pkg v2.Document
err = yaml.Unmarshal(doc, &pkg)
if err != nil {
return "", nil, fmt.Errorf("unable to unmarshall input file: %v", err)
}

docs = append(docs, pkg)
}

tempDir = os.TempDir()
for _, doc := range docs {
f, err := os.Create(filepath.Join(tempDir, fmt.Sprintf("%s.advisories.yaml", doc.Name())))
if err != nil {
return "", nil, fmt.Errorf("failed to create adv file: %v", err)
}

d, err := yaml.Marshal(doc)
if err != nil {
return "", nil, fmt.Errorf("failed to marshal package %q: %v", doc.Package.Name, err)
}
_, err = f.Write(d)
if err != nil {
return "", nil, fmt.Errorf("failed save data to file: %v", err)
}

f.Close()
}

advisoryFsys := rwos.DirFS(tempDir)
advisoryDocIndices, err := v2.NewIndex(context.Background(), advisoryFsys)
if err != nil {
return "", nil, fmt.Errorf("unable to index advisory configs for directory %q: %v", tempDir, err)
}

return tempDir, advisoryDocIndices, nil
}
37 changes: 37 additions & 0 deletions pkg/advisory/import_test.go
@@ -0,0 +1,37 @@
package advisory

import (
"context"
"testing"

"github.com/stretchr/testify/require"

v2 "github.com/wolfi-dev/wolfictl/pkg/configs/advisory/v2"
rwos "github.com/wolfi-dev/wolfictl/pkg/configs/rwfs/os"
)

func Test_ImportAdvisoriesYAML(t *testing.T) {
const testdataDir = "./testdata/export/advisories"

cases := []struct {
name string
pathToInputData string
}{
{
name: "test-yaml",
pathToInputData: "./testdata/export/expected.yaml",
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
advisoryFsys := rwos.DirFS(testdataDir)
advisoryDocs, err := v2.NewIndex(context.Background(), advisoryFsys)
require.NoError(t, err)

_, importedDocuments, err := ImporAdvisoriesYAML(tt.pathToInputData)
require.NoError(t, err)
require.Equal(t, advisoryDocs.Select().Len(), importedDocuments.Select().Len())
})
}
}

0 comments on commit 2281dff

Please sign in to comment.