github.com/wolfi-dev/wolfictl@v0.16.11/pkg/advisory/import.go (about) 1 package advisory 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "os" 8 "path/filepath" 9 10 "gopkg.in/yaml.v3" 11 12 "github.com/wolfi-dev/wolfictl/pkg/configs" 13 v2 "github.com/wolfi-dev/wolfictl/pkg/configs/advisory/v2" 14 rwos "github.com/wolfi-dev/wolfictl/pkg/configs/rwfs/os" 15 ) 16 17 // ImporAdvisoriesYAML import and yaml Advisories data and present as a config index struct 18 func ImporAdvisoriesYAML(inputData []byte) (tempDir string, documents *configs.Index[v2.Document], err error) { 19 yamlDocs := bytes.Split(inputData, []byte("\n---\n")) 20 // Unmarshal YAML documents 21 var docs []v2.Document 22 for _, doc := range yamlDocs { 23 var pkg v2.Document 24 err = yaml.Unmarshal(doc, &pkg) 25 if err != nil { 26 return "", nil, fmt.Errorf("unable to unmarshall input file: %v", err) 27 } 28 29 docs = append(docs, pkg) 30 } 31 32 tempDir, err = os.MkdirTemp("", "adv-") 33 if err != nil { 34 return "", nil, fmt.Errorf("unable to create temporary directory: %v", err) 35 } 36 for _, doc := range docs { 37 f, err := os.Create(filepath.Join(tempDir, fmt.Sprintf("%s.advisories.yaml", doc.Name()))) 38 if err != nil { 39 return "", nil, fmt.Errorf("failed to create adv file: %v", err) 40 } 41 42 d, err := yaml.Marshal(doc) 43 if err != nil { 44 return "", nil, fmt.Errorf("failed to marshal package %q: %v", doc.Package.Name, err) 45 } 46 _, err = f.Write(d) 47 if err != nil { 48 return "", nil, fmt.Errorf("failed save data to file: %v", err) 49 } 50 51 f.Close() 52 } 53 54 advisoryFsys := rwos.DirFS(tempDir) 55 advisoryDocIndices, err := v2.NewIndex(context.Background(), advisoryFsys) 56 if err != nil { 57 return "", nil, fmt.Errorf("unable to index advisory configs for directory %q: %v", tempDir, err) 58 } 59 60 return tempDir, advisoryDocIndices, nil 61 }