From b29c4a9cacb9c2f1026beb330fcb6d9db8c453ed Mon Sep 17 00:00:00 2001 From: CatalinStratu Date: Wed, 4 May 2022 17:26:14 +0300 Subject: Add YAML use example Signed-off-by: CatalinStratu --- examples/13-yamlloader/exampleYAMLLoader.go | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/13-yamlloader/exampleYAMLLoader.go (limited to 'examples/13-yamlloader') 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 \n", args[0]) + fmt.Printf(" Load SPDX 2.2 JSON file , 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)) +} -- cgit v1.2.3