1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 3// Example for: *tvloader*, *tvsaver* 4 5// This example demonstrates loading an SPDX tag-value file from disk into memory, 6// and re-saving it to a different file on disk. 7// Run project: go run example_load_save.go ../sample-docs/tv/hello.spdx test.spdx 8 9package main 10 11import ( 12 "fmt" 13 "os" 14 15 "github.com/spdx/tools-golang/tvloader" 16 "github.com/spdx/tools-golang/tvsaver" 17) 18 19func main() { 20 21 // check that we've received the right number of arguments 22 args := os.Args 23 if len(args) != 3 { 24 fmt.Printf("Usage: %v <spdx-file-in> <spdx-file-out>\n", args[0]) 25 fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n") 26 fmt.Printf(" save it out to <spdx-file-out>.\n") 27 return 28 } 29 30 // open the SPDX file 31 fileIn := args[1] 32 r, err := os.Open(fileIn) 33 if err != nil { 34 fmt.Printf("Error while opening %v for reading: %v", fileIn, 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", fileIn, err) 43 return 44 } 45 46 // if we got here, the file is now loaded into memory. 47 fmt.Printf("Successfully loaded %s\n", fileIn) 48 49 // we can now save it back to disk, using tvsaver. 50 51 // create a new file for writing 52 fileOut := args[2] 53 w, err := os.Create(fileOut) 54 if err != nil { 55 fmt.Printf("Error while opening %v for writing: %v", fileOut, err) 56 return 57 } 58 defer w.Close() 59 60 // try to save the document to disk as an SPDX tag-value file, version 2.2 61 err = tvsaver.Save2_2(doc, w) 62 if err != nil { 63 fmt.Printf("Error while saving %v: %v", fileOut, err) 64 return 65 } 66 67 // it worked 68 fmt.Printf("Successfully saved %s\n", fileOut) 69} 70