xref: /aosp_15_r20/external/spdx-tools/examples/1-load/example_load.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3// Example for: *tvloader*, *spdx*
4
5// This example demonstrates loading an SPDX tag-value file from disk into
6// memory, and printing some of its contents to standard output.
7// Run project:  go run example_load.go ../sample-docs/tv/hello.spdx
8
9package main
10
11import (
12	"fmt"
13	"os"
14
15	"github.com/spdx/tools-golang/spdxlib"
16	"github.com/spdx/tools-golang/tvloader"
17)
18
19func main() {
20
21	// check that we've received the right number of arguments
22	args := os.Args
23	if len(args) != 2 {
24		fmt.Printf("Usage: %v <spdx-file-in>\n", args[0])
25		fmt.Printf("  Load SPDX 2.2 tag-value file <spdx-file-in>, and\n")
26		fmt.Printf("  print a portion of its contents.\n")
27		return
28	}
29
30	// open the SPDX file
31	filename := args[1]
32	r, err := os.Open(filename)
33	if err != nil {
34		fmt.Printf("Error while opening %v for reading: %v", filename, err)
35		return
36	}
37	defer r.Close()
38
39	// try to load the SPDX file's contents as a tag-value file, version 2.2
40	doc, err := tvloader.Load2_2(r)
41	if err != nil {
42		fmt.Printf("Error while parsing %v: %v", filename, err)
43		return
44	}
45
46	// if we got here, the file is now loaded into memory.
47	fmt.Printf("Successfully loaded %s\n\n", filename)
48
49	// we can now take a look at its contents via the various data
50	// structures representing the SPDX document's sections.
51
52	// print the struct containing the SPDX file's Creation Info section data
53	fmt.Printf("==============\n")
54	fmt.Printf("Creation info:\n")
55	fmt.Printf("==============\n")
56	fmt.Printf("%#v\n\n", doc.CreationInfo)
57
58	// check whether the SPDX file has at least one package that it describes
59	pkgIDs, err := spdxlib.GetDescribedPackageIDs2_2(doc)
60	if err != nil {
61		fmt.Printf("Unable to get describe packages from SPDX document: %v\n", err)
62		return
63	}
64
65	if len(pkgIDs) == 0 {
66		return
67	}
68
69	// it does, so we'll go through each one
70	for _, pkg := range doc.Packages {
71		var documentDescribesPackage bool
72		for _, describedPackageID := range pkgIDs {
73			if pkg.PackageSPDXIdentifier == describedPackageID {
74				documentDescribesPackage = true
75				break
76			}
77		}
78
79		if !documentDescribesPackage {
80			continue
81		}
82
83		pkgID := pkg.PackageSPDXIdentifier
84
85		// check whether the package had its files analyzed
86		if !pkg.FilesAnalyzed {
87			fmt.Printf("Package %s (%s) had FilesAnalyzed: false\n", string(pkgID), pkg.PackageName)
88			continue
89		}
90
91		// also check whether the package has any files present
92		if pkg.Files == nil || len(pkg.Files) < 1 {
93			fmt.Printf("Package %s (%s) has no Files\n", string(pkgID), pkg.PackageName)
94			continue
95		}
96
97		// if we got here, there's at least one file
98		// print the filename and license info for the first 50
99		fmt.Printf("============================\n")
100		fmt.Printf("Package %s (%s)\n", string(pkgID), pkg.PackageName)
101		fmt.Printf("File info (up to first 50):\n")
102		i := 1
103		for _, f := range pkg.Files {
104			// note that these will be in random order, since we're pulling
105			// from a map. if we care about order, we should first pull the
106			// IDs into a slice, sort it, and then print the ordered files.
107			fmt.Printf("- File %d: %s\n", i, f.FileName)
108			fmt.Printf("    License from file: %v\n", f.LicenseInfoInFiles)
109			fmt.Printf("    License concluded: %v\n", f.LicenseConcluded)
110			i++
111			if i > 50 {
112				break
113			}
114		}
115	}
116}
117