Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

organize imports #670

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 54 additions & 1 deletion pkg/advisory/export.go
Expand Up @@ -2,14 +2,19 @@ package advisory

import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"os"
"path/filepath"

"github.com/samber/lo"
"gopkg.in/yaml.v3"

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

type ExportOptions struct {
Expand Down Expand Up @@ -112,3 +117,51 @@ func ExportYAML(opts ExportOptions) (io.Reader, error) {

return buf, nil
}

// 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
}
23 changes: 23 additions & 0 deletions pkg/advisory/export_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"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"
Expand Down Expand Up @@ -62,3 +63,25 @@ func Test_ExportFuncs(t *testing.T) {
})
}
}

func TestImportAdvisoriesYAML(t *testing.T) {
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) {
tempDir, importedDocuments, err := ImporAdvisoriesYAML(tt.pathToInputData)
require.NoError(t, err)
require.Equal(t, 3, importedDocuments.Select().Len())

defer os.RemoveAll(tempDir)
})
}
}