aboutsummaryrefslogtreecommitdiff
path: root/examples/2-load-save
diff options
context:
space:
mode:
authorSteve Winslow <swinslow@gmail.com>2018-10-24 17:46:56 -0400
committerGitHub <noreply@github.com>2018-10-24 17:46:56 -0400
commit941fa33ea499c1f9092818a264a38600efe1acce (patch)
tree27f7bbf471ef74dbde5a01003862d80aa198bcc9 /examples/2-load-save
parentd4c19259c711d59d26040295112d6d2fd618faa1 (diff)
downloadspdx-tools-941fa33ea499c1f9092818a264a38600efe1acce.tar.gz
Added examples (#29)
Closes #27 Signed-off-by: Steve Winslow <swinslow@gmail.com>
Diffstat (limited to 'examples/2-load-save')
-rw-r--r--examples/2-load-save/example_load_save.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/examples/2-load-save/example_load_save.go b/examples/2-load-save/example_load_save.go
new file mode 100644
index 0000000..4a5cfc6
--- /dev/null
+++ b/examples/2-load-save/example_load_save.go
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+
+// Example for: *tvloader*, *tvsaver*
+
+// This example demonstrates loading an SPDX tag-value file from disk into memory,
+// and re-saving it to a different file on disk.
+
+package main
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/swinslow/spdx-go/v0/tvloader"
+ "github.com/swinslow/spdx-go/v0/tvsaver"
+)
+
+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-in> <spdx-file-out>\n", args[0])
+ fmt.Printf(" Load SPDX 2.1 tag-value file <spdx-file-in>, and\n")
+ fmt.Printf(" save it out to <spdx-file-out>.\n")
+ return
+ }
+
+ // open the SPDX file
+ fileIn := args[1]
+ r, err := os.Open(fileIn)
+ if err != nil {
+ fmt.Printf("Error while opening %v for reading: %v", fileIn, err)
+ return
+ }
+ defer r.Close()
+
+ // try to load the SPDX file's contents as a tag-value file, version 2.1
+ doc, err := tvloader.Load2_1(r)
+ if err != nil {
+ fmt.Printf("Error while parsing %v: %v", fileIn, err)
+ return
+ }
+
+ // if we got here, the file is now loaded into memory.
+ fmt.Printf("Successfully loaded %s\n", fileIn)
+
+ // we can now save it back to disk, using tvsaver.
+
+ // create a new file for writing
+ fileOut := args[2]
+ w, err := os.Create(fileOut)
+ if err != nil {
+ fmt.Printf("Error while opening %v for writing: %v", fileOut, err)
+ return
+ }
+ defer w.Close()
+
+ // try to save the document to disk as an SPDX tag-value file, version 2.1
+ err = tvsaver.Save2_1(doc, w)
+ if err != nil {
+ fmt.Printf("Error while saving %v: %v", fileOut, err)
+ return
+ }
+
+ // it worked
+ fmt.Printf("Successfully saved %s\n", fileOut)
+}