diff options
Diffstat (limited to 'examples/1-load')
-rw-r--r-- | examples/1-load/example_load.go | 67 |
1 files changed, 39 insertions, 28 deletions
diff --git a/examples/1-load/example_load.go b/examples/1-load/example_load.go index 7cc9575..e2af1ac 100644 --- a/examples/1-load/example_load.go +++ b/examples/1-load/example_load.go @@ -11,6 +11,7 @@ import ( "fmt" "os" + "github.com/spdx/tools-golang/spdxlib" "github.com/spdx/tools-golang/tvloader" ) @@ -53,40 +54,50 @@ func main() { fmt.Printf("==============\n") fmt.Printf("%#v\n\n", doc.CreationInfo) - // check whether the SPDX file has at least one package - if doc.Packages == nil || len(doc.Packages) < 1 { - fmt.Printf("No packages found in SPDX document\n") + // check whether the SPDX file has at least one package that it describes + pkgIDs, err := spdxlib.GetDescribedPackageIDs2_1(doc) + if err != nil { + fmt.Printf("Unable to get describe packages from SPDX document: %v\n", err) return } - // it does, so we'll choose the first one - pkg := doc.Packages[0] + // it does, so we'll go through each one + for _, pkgID := range pkgIDs { + pkg, ok := doc.Packages[pkgID] + if !ok { + fmt.Printf("Package %s has described relationship but ID not found\n", string(pkgID)) + continue + } - // check whether the package had its files analyzed - if !pkg.FilesAnalyzed { - fmt.Printf("First Package (%s) had FilesAnalyzed: false\n", pkg.PackageName) - return - } + // check whether the package had its files analyzed + if !pkg.FilesAnalyzed { + fmt.Printf("Package %s (%s) had FilesAnalyzed: false\n", string(pkgID), pkg.PackageName) + continue + } - // also check whether the package has any files present - if pkg.Files == nil || len(pkg.Files) < 1 { - fmt.Printf("No Files found in first Package (%s)\n", pkg.PackageName) - return - } + // also check whether the package has any files present + if pkg.Files == nil || len(pkg.Files) < 1 { + fmt.Printf("Package %s (%s) has no Files\n", string(pkgID), pkg.PackageName) + continue + } - // if we got here, there's at least one file - // print the filename and license info for the first 50 - fmt.Printf("============================\n") - fmt.Printf("Files info (up to first 50):\n") - fmt.Printf("============================\n") - i := 1 - for _, f := range pkg.Files { - fmt.Printf("File %d: %s\n", i, f.FileName) - fmt.Printf(" License from file: %v\n", f.LicenseInfoInFile) - fmt.Printf(" License concluded: %v\n", f.LicenseConcluded) - i++ - if i > 50 { - break + // if we got here, there's at least one file + // print the filename and license info for the first 50 + fmt.Printf("============================\n") + fmt.Printf("Package %s (%s)\n", string(pkgID), pkg.PackageName) + fmt.Printf("File info (up to first 50):\n") + i := 1 + for _, f := range pkg.Files { + // note that these will be in random order, since we're pulling + // from a map. if we care about order, we should first pull the + // IDs into a slice, sort it, and then print the ordered files. + fmt.Printf("- File %d: %s\n", i, f.FileName) + fmt.Printf(" License from file: %v\n", f.LicenseInfoInFile) + fmt.Printf(" License concluded: %v\n", f.LicenseConcluded) + i++ + if i > 50 { + break + } } } } |