diff options
author | Steve Winslow <steve@swinslow.net> | 2022-05-15 10:25:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-15 10:25:48 -0400 |
commit | a532726dbb7a38d0f714075e9a1f1df4cae60230 (patch) | |
tree | 5d306da8f910b81daf2377433528dfb0d917c5de | |
parent | c21431fe8bc96468a682cc6cd00ef5d137380a61 (diff) | |
parent | cfe2ffbea5fc2d73b79cfca51b7db56bd6be9ddd (diff) | |
download | spdx-tools-a532726dbb7a38d0f714075e9a1f1df4cae60230.tar.gz |
Merge pull request #141 from CatalinStratu/main
YAML documentation and JSON documentation fixes
-rw-r--r-- | examples/10-jsonloader/example_json_loader.go | 4 | ||||
-rw-r--r-- | examples/11-yamltotv/exampleyamltotv.go | 68 | ||||
-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 | 4 | ||||
-rw-r--r-- | examples/9-tvtojson/exampletvtojson.go | 6 | ||||
-rw-r--r-- | examples/README.md | 30 |
7 files changed, 225 insertions, 10 deletions
diff --git a/examples/10-jsonloader/example_json_loader.go b/examples/10-jsonloader/example_json_loader.go index 4de9561..793f10e 100644 --- a/examples/10-jsonloader/example_json_loader.go +++ b/examples/10-jsonloader/example_json_loader.go @@ -1,9 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *jsonparser2v2* +// Example for: *json* // This example demonstrates loading an SPDX JSON document from disk into memory, -// and then logging some of the attributes to the console. +// and then logging some attributes to the console. // Run project: go run example_json_loader.go ../sample-docs/json/SPDXJSONExample-v2.2.spdx.json package main diff --git a/examples/11-yamltotv/exampleyamltotv.go b/examples/11-yamltotv/exampleyamltotv.go new file mode 100644 index 0000000..b56a67d --- /dev/null +++ b/examples/11-yamltotv/exampleyamltotv.go @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later + +// Example for: *yaml* *tvsaver* + +// 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/tvsaver" + "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 YAML 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 + 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 = 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..1f43a03 --- /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*, *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 yaml. + + // 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 YAML file + 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..85be282 100644 --- a/examples/8-jsontotv/examplejsontotv.go +++ b/examples/8-jsontotv/examplejsontotv.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *jsonparser2v2*, *tvsaver* +// Example for: *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 . @@ -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 JSON 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..e8e6937 100644 --- a/examples/9-tvtojson/exampletvtojson.go +++ b/examples/9-tvtojson/exampletvtojson.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later -// Example for: *tvloader*, *jsonsaver* +// Example for: *tvloader*, *json* // This example demonstrates loading an SPDX tag-value file from disk into memory, // and re-saving it to a different json file on disk. @@ -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 <json-file-out>.\n") return } @@ -56,7 +56,7 @@ func main() { } defer w.Close() - // try to save the document to disk as an SPDX json file, version 2.2 + // try to save the document to disk as JSON file err = spdx_json.Save2_2(doc, w) if err != nil { fmt.Printf("Error while saving %v: %v", fileOut, err) diff --git a/examples/README.md b/examples/README.md index 3697e36..2f1ab34 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* *tvsaver* + +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* *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 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* |