diff options
author | CatalinStratu <catalinstratu45@gmail.com> | 2022-05-04 17:26:14 +0300 |
---|---|---|
committer | CatalinStratu <catalinstratu45@gmail.com> | 2022-05-05 09:38:40 +0300 |
commit | b29c4a9cacb9c2f1026beb330fcb6d9db8c453ed (patch) | |
tree | 814a052907a3ac5a3676529bd6bf26d292e911d2 | |
parent | c1a0838299943b6293ca76abcc3591ee0383be4a (diff) | |
download | spdx-tools-b29c4a9cacb9c2f1026beb330fcb6d9db8c453ed.tar.gz |
Add YAML use example
Signed-off-by: CatalinStratu <catalinstratu45@gmail.com>
-rw-r--r-- | examples/11-yamltotv/exampleyamltotv.go | 73 | ||||
-rw-r--r-- | examples/12-tvtoyaml/exampletvtoyaml.go | 68 | ||||
-rw-r--r-- | examples/13-yamlloader/exampleYAMLLoader.go | 55 | ||||
-rw-r--r-- | examples/8-jsontotv/examplejsontotv.go | 2 | ||||
-rw-r--r-- | examples/9-tvtojson/exampletvtojson.go | 2 | ||||
-rw-r--r-- | examples/README.md | 30 |
6 files changed, 225 insertions, 5 deletions
diff --git a/examples/11-yamltotv/exampleyamltotv.go b/examples/11-yamltotv/exampleyamltotv.go new file mode 100644 index 0000000..006d6d9 --- /dev/null +++ b/examples/11-yamltotv/exampleyamltotv.go @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +// Example for: *spdx_yaml* + +// This example demonstrates loading an SPDX tag-value file from disk into memory, +// and re-saving it to a different file on disk. +// Run project: go run exampleyamltotv.go ../sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml test.spdx + +package main + +import ( + "fmt" + "github.com/spdx/tools-golang/yaml" + "os" +) + +func main() { + + // check that we've received the right number of arguments + args := os.Args + if len(args) != 3 { + fmt.Printf("Usage: %v <yaml-file-in> <spdx-file-out>\n", args[0]) + fmt.Printf(" Load SPDX 2.2 tag-value file <yaml-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 yaml file, version 2.2 + doc, err := spdx_yaml.Load2_2(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 spdx_yaml, but tvsaver work also. + + // 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.2 + err = spdx_yaml.Save2_2(doc, w) + if err != nil { + fmt.Printf("Error while saving %v: %v", fileOut, err) + return + } + + //err = tvsaver.Save2_2(doc, w) + //if err != nil { + // fmt.Printf("Error while saving %v: %v", fileOut, err) + // return + //} + + // it worked + fmt.Printf("Successfully saved %s\n", fileOut) +} diff --git a/examples/12-tvtoyaml/exampletvtoyaml.go b/examples/12-tvtoyaml/exampletvtoyaml.go new file mode 100644 index 0000000..f348da7 --- /dev/null +++ b/examples/12-tvtoyaml/exampletvtoyaml.go @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +// Example for: *tvloader*, *spdx_yaml* + +// This example demonstrates loading an SPDX tag-value file from disk into memory, +// and re-saving it to a different json file on disk. +// Run project: go run exampletvtoyaml.go ../sample-docs/tv/hello.spdx example.yaml +package main + +import ( + "fmt" + "os" + + "github.com/spdx/tools-golang/tvloader" + "github.com/spdx/tools-golang/yaml" +) + +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> <yaml-file-out>\n", args[0]) + fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n") + fmt.Printf(" save it out to <yaml-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.2 + doc, err := tvloader.Load2_2(r) + if err != nil { + fmt.Printf("Error while parsing %v: %v", args[1], err) + return + } + + // if we got here, the file is now loaded into memory. + fmt.Printf("Successfully loaded %s\n", args[1]) + + // we can now save it back to disk, using jsonsaver. + + // 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 json file, version 2.2 + err = spdx_yaml.Save2_2(doc, w) + if err != nil { + fmt.Printf("Error while saving %v: %v", fileOut, err) + return + } + + // it worked + fmt.Printf("Successfully saved %s\n", fileOut) +} diff --git a/examples/13-yamlloader/exampleYAMLLoader.go b/examples/13-yamlloader/exampleYAMLLoader.go new file mode 100644 index 0000000..237cc65 --- /dev/null +++ b/examples/13-yamlloader/exampleYAMLLoader.go @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +// Example for: *yaml* + +// This example demonstrates loading an SPDX YAML document from disk into memory, +// and then logging some attributes to the console. +// Run project: go run exampleYAMLLoader.go ../sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/spdx/tools-golang/yaml" +) + +func main() { + + // check that we've received the right number of arguments + args := os.Args + if len(args) != 2 { + fmt.Printf("Usage: %v <json-file-in>\n", args[0]) + fmt.Printf(" Load SPDX 2.2 JSON file <spdx-file-in>, and\n") + fmt.Printf(" print portions of its creation info data.\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 json file, version 2.2 + doc, err := spdx_yaml.Load2_2(r) + if err != nil { + fmt.Printf("Error while parsing %v: %v", args[1], err) + return + } + + // if we got here, the file is now loaded into memory. + fmt.Printf("Successfully loaded %s\n", args[1]) + + fmt.Println(strings.Repeat("=", 80)) + fmt.Println("Some Attributes of the Document:") + fmt.Printf("Document Name: %s\n", doc.DocumentName) + fmt.Printf("DataLicense: %s\n", doc.DataLicense) + fmt.Printf("Document Namespace: %s\n", doc.DocumentNamespace) + fmt.Printf("SPDX Version: %s\n", doc.SPDXVersion) + fmt.Println(strings.Repeat("=", 80)) +} diff --git a/examples/8-jsontotv/examplejsontotv.go b/examples/8-jsontotv/examplejsontotv.go index 9faadce..fed8439 100644 --- a/examples/8-jsontotv/examplejsontotv.go +++ b/examples/8-jsontotv/examplejsontotv.go @@ -21,7 +21,7 @@ func main() { args := os.Args if len(args) != 3 { fmt.Printf("Usage: %v <json-file-in> <spdx-file-out>\n", args[0]) - fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n") + fmt.Printf(" Load SPDX 2.2 tag-value file <json-file-in>, and\n") fmt.Printf(" save it out to <spdx-file-out>.\n") return } diff --git a/examples/9-tvtojson/exampletvtojson.go b/examples/9-tvtojson/exampletvtojson.go index fac1c98..2aca5cf 100644 --- a/examples/9-tvtojson/exampletvtojson.go +++ b/examples/9-tvtojson/exampletvtojson.go @@ -22,7 +22,7 @@ func main() { if len(args) != 3 { fmt.Printf("Usage: %v <spdx-file-in> <json-file-out>\n", args[0]) fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n") - fmt.Printf(" save it out to <spdx-file-out>.\n") + fmt.Printf(" save it out to <json-file-out>.\n") return } diff --git a/examples/README.md b/examples/README.md index 3697e36..6a312c3 100644 --- a/examples/README.md +++ b/examples/README.md @@ -73,7 +73,7 @@ and then printing the corresponding spdx struct for the document. ## 8-jsontotv -*jsonloader*, *tvsaver* +*json*, *tvsaver* This example demonstrates loading an SPDX json from disk into memory and then re-saving it to a different file on disk in tag-value format. @@ -81,7 +81,7 @@ and then re-saving it to a different file on disk in tag-value format. ## 9-tvtojson -*jsonsaver*, *tvloader* +*json*, *tvloader* This example demonstrates loading an SPDX tag-value from disk into memory and then re-saving it to a different file on disk in json format. @@ -89,8 +89,32 @@ and then re-saving it to a different file on disk in json format. ## 10-jsonloader -*jsonloader* +*json* This example demonstrates loading an SPDX json from disk into memory and then logging some of the attributes to the console. #### Run project: *go run example_json_loader.go ../sample-docs/json/SPDXJSONExample-v2.2.spdx.json* + +## 11-yamltotv + +*yaml* + +This example demonstrates loading an SPDX yaml from disk into memory +and then re-saving it to a different file on disk in tag-value format. +#### Run project: *go run exampleyamltotv.go ../sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml test.spdx* + +## 12-tvtoyaml + +*yaml* + +This example demonstrates loading an SPDX tag-value from disk into memory +and then re-saving it to a different file on disk in yaml format. +#### Run project: *go run exampletvtoyaml.go ../sample-docs/tv/hello.spdx example.yaml* + +## 13-yamlloader + +*yaml* + +This example demonstrates loading an SPDX yaml from disk into memory +and then logging some of the attributes to the console. +#### Run project: *go run exampleYAMLLoader.go ../sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml* |