aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSteve Winslow <swinslow@gmail.com>2018-12-06 13:39:20 +0900
committerSteve Winslow <swinslow@gmail.com>2018-12-06 13:39:20 +0900
commitc7f466f2e6a0afb7a52546fb5ab6b248e024e7bd (patch)
tree4cf99e5d9746d8409b5b454d575a4520f942d82c /examples
parentfe644adc096ddbc14cd4b797aea3a5890a3d0a02 (diff)
downloadspdx-tools-c7f466f2e6a0afb7a52546fb5ab6b248e024e7bd.tar.gz
Added example for licensediff
Signed-off-by: Steve Winslow <swinslow@gmail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/6-licensediff/example_licensediff.go106
-rw-r--r--examples/README.md11
2 files changed, 117 insertions, 0 deletions
diff --git a/examples/6-licensediff/example_licensediff.go b/examples/6-licensediff/example_licensediff.go
new file mode 100644
index 0000000..d7ff562
--- /dev/null
+++ b/examples/6-licensediff/example_licensediff.go
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+// Example for: *licensediff*, *tvloader*
+
+// This example demonstrates loading two SPDX tag-value files from disk into
+// memory, and generating a diff of the concluded licenses for Files in the
+// first-listed Packages in each document.
+// This is generally only useful when run with two SPDX documents that
+// describe licenses for subsequent versions of the same set of files.
+
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/swinslow/spdx-go/v0/licensediff"
+ "github.com/swinslow/spdx-go/v0/tvloader"
+)
+
+func main() {
+
+ // check that we've received the right number of arguments
+ args := os.Args
+ if len(args) != 3 {
+ fmt.Printf("Usage: %v <spdx-file-first> <spdx-file-second>\n", args[0])
+ fmt.Printf(" Load SPDX 2.1 tag-value files <spdx-file-first> and <spdx-file-second>,\n")
+ fmt.Printf(" run a diff between their concluded licenses, and print basic results.\n")
+ return
+ }
+
+ // open the first SPDX file
+ filenameFirst := args[1]
+ r, err := os.Open(filenameFirst)
+ if err != nil {
+ fmt.Printf("Error while opening %v for reading: %v", filenameFirst, err)
+ return
+ }
+ defer r.Close()
+
+ // try to load the first SPDX file's contents as a tag-value file, version 2.1
+ docFirst, err := tvloader.Load2_1(r)
+ if err != nil {
+ fmt.Printf("Error while parsing %v: %v", filenameFirst, err)
+ return
+ }
+ // make sure it has at least one Package
+ if len(docFirst.Packages) < 1 {
+ fmt.Printf("Error, no packages found in SPDX file %s\n", filenameFirst)
+ return
+ }
+
+ // if we got here, the file is now loaded into memory.
+ fmt.Printf("Successfully loaded first SPDX file %s\n", filenameFirst)
+
+ // open the second SPDX file
+ filenameSecond := args[2]
+ r, err = os.Open(filenameSecond)
+ if err != nil {
+ fmt.Printf("Error while opening %v for reading: %v", filenameSecond, err)
+ return
+ }
+ defer r.Close()
+
+ // try to load the second SPDX file's contents as a tag-value file, version 2.1
+ docSecond, err := tvloader.Load2_1(r)
+ if err != nil {
+ fmt.Printf("Error while parsing %v: %v", filenameSecond, err)
+ return
+ }
+ // make sure it has at least one Package
+ if len(docSecond.Packages) < 1 {
+ fmt.Printf("Error, no packages found in SPDX file %s\n", filenameSecond)
+ return
+ }
+
+ // if we got here, the file is now loaded into memory.
+ fmt.Printf("Successfully loaded second SPDX file %s\n\n", filenameSecond)
+
+ // extract the first-listed package from each Document.
+ // (there could be more than one Package within a Document. For
+ // purposes of this example, we'll just look at the first one.)
+ // we've already confirmed above that each has at least one Package.
+ p1 := docFirst.Packages[0]
+ p2 := docSecond.Packages[0]
+
+ // now, run a diff between the two
+ pairs, err := licensediff.MakePairs(p1, p2)
+ if err != nil {
+ fmt.Printf("Error generating licensediff pairs: %v\n", err)
+ return
+ }
+
+ // take the pairs and turn them into a more structured results set
+ resultSet, err := licensediff.MakeResults(pairs)
+ if err != nil {
+ fmt.Printf("Error generating licensediff results set: %v\n", err)
+ return
+ }
+
+ // print some information about the results
+ fmt.Printf("Files in first only: %d\n", len(resultSet.InFirstOnly))
+ fmt.Printf("Files in second only: %d\n", len(resultSet.InSecondOnly))
+ fmt.Printf("Files in both with different licenses: %d\n", len(resultSet.InBothChanged))
+ fmt.Printf("Files in both with same licenses: %d\n", len(resultSet.InBothSame))
+}
diff --git a/examples/README.md b/examples/README.md
index 64e41f2..19a58b9 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -42,3 +42,14 @@ Package and File license fields; and saving the resulting document to disk.
This example demonstrates loading an SPDX tag-value file from disk into memory,
generating a basic report listing counts of the concluded licenses for its
files, and printing the report to standard output.
+
+## 6-licensediff
+
+*licensediff*, *tvloader*
+
+This example demonstrates loading two SPDX tag-value files from disk into
+memory, and generating a diff of the concluded licenses for Files in the
+first-listed Packages in each document.
+
+This is generally only useful when run with two SPDX documents that describe
+licenses for subsequent versions of the same set of files.