Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rchincha committed Apr 25, 2024
1 parent d67bad0 commit aed4e54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
46 changes: 25 additions & 21 deletions pkg/bom/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"

"github.com/rs/zerolog/log"
"sigs.k8s.io/bom/pkg/serialize"
Expand Down Expand Up @@ -53,66 +52,73 @@ func MergeMaps[K comparable, V any](map1 map[K]V, map2 map[K]V) map[K]V {
return merged
}

// add pkgs in doc2 to doc1
func mergePackages(doc1, doc2 *spdx.Document) error {
// merge pkgs from doc2 to doc1.
func mergePackages(doc1, doc2 *spdx.Document) {
for _, pkg2 := range doc2.Packages {
found := false

var pkg1 *spdx.Package

for _, pkg1 = range doc1.Packages {
if pkg1.Name == pkg2.Name && pkg1.Version == pkg2.Version {
found = true

break
}
}

if !found {
doc1.AddPackage(pkg2)
log.Info().Str("pkg", pkg2.Name).Msg("merge: adding pkg")
_ = doc1.AddPackage(pkg2)

log.Info().Str("package", pkg2.Name).Str("version", pkg2.Version).Msg("merging package")

continue
}

var file2 *spdx.File

for _, file2 = range pkg2.Files() {
log.Info().Interface("pkg file", file2).Int("len1", len(pkg1.Files())).Int("len2", len(pkg2.Files())).Msg("merge: adding pkg file3")
found = false

for _, file1 := range pkg1.Files() {
log.Info().Str("file1", file1.Name).Str("file2", file2.Name).Msg("merge: adding pkg file4")
if file1.Name == file2.Name {
found = true

break
}
}

if !found {
log.Info().Interface("pkg file", file2).Msg("merge: adding pkg file6")
pkg1.AddFile(file2)
_ = pkg1.AddFile(file2)

log.Info().Str("package", pkg1.Name).Str("version", pkg1.Version).
Str("file", file2.Name).Msg("merging file to package")
}
}
}

return nil
}

// add files in doc2 to doc1
func mergeFiles(doc1, doc2 *spdx.Document) error {
// merge files from doc2 to doc1.
func mergeFiles(doc1, doc2 *spdx.Document) {
for _, file2 := range doc2.Files {
found := false

var file1 *spdx.File

for _, file1 = range doc1.Files {
if file1.Name == file1.Name && reflect.DeepEqual(file1.Checksum, file2.Checksum) {
if file1.Name == file2.Name {
found = true

break
}
}

if !found {
doc1.AddFile(file2)
log.Info().Str("file", file2.Name).Msg("merge: adding file")
continue
_ = doc1.AddFile(file2)

log.Info().Str("file", file2.Name).Msg("merging file to document")
}
}

return nil
}

// MergeDocuments in a given dir.
Expand Down Expand Up @@ -144,8 +150,6 @@ func MergeDocuments(dir, namespace, name, author, organization, output string) e

mergePackages(sdoc, doc)
mergeFiles(sdoc, doc)
//sdoc.Files = MergeMaps(sdoc.Files, doc.Files)
//sdoc.Packages = MergeMaps(sdoc.Packages, doc.Packages)

log.Info().Str("path", path).Msg("file found for merging")

Expand Down
18 changes: 0 additions & 18 deletions pkg/fs/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ import (
"stackerbuild.io/stacker-bom/pkg/bom"
)

type errLogEntry struct {
PathEntry string
File spdx.File
Symlink string
Pkg string
Type bool
}

func checkBOM(input, pathEntry string) error {
doc, err := spdx.OpenDoc(input)
if err != nil {
Expand All @@ -38,8 +30,6 @@ func checkBOM(input, pathEntry string) error {
return fmt.Errorf("%w: %s", errors.ErrInvalidDoc, input)
}

errLogs := []errLogEntry{}

for _, pkg := range doc.Packages {
for _, file := range pkg.Files() {
symlink, err := filepath.EvalSymlinks(file.Name)
Expand All @@ -49,8 +39,6 @@ func checkBOM(input, pathEntry string) error {
return err
}

errLogs = append(errLogs, errLogEntry{PathEntry: pathEntry, File: *file, Symlink: symlink, Pkg: pkg.Name, Type: false})

if file.Name != pathEntry && symlink != pathEntry {
continue
}
Expand All @@ -77,8 +65,6 @@ func checkBOM(input, pathEntry string) error {
return err
}

errLogs = append(errLogs, errLogEntry{PathEntry: pathEntry, File: *file, Symlink: symlink, Type: true})

if file.Name != pathEntry && file.Name != symlink {
continue
}
Expand All @@ -96,10 +82,6 @@ func checkBOM(input, pathEntry string) error {
return nil
}

for _, loge := range errLogs {
log.Info().Interface("errlog", loge).Msg("checked but not found")
}

return fmt.Errorf("%w: %s", errors.ErrNotFound, pathEntry)
}

Expand Down

0 comments on commit aed4e54

Please sign in to comment.