xref: /aosp_15_r20/external/spdx-tools/examples/13-yamlloader/exampleYAMLLoader.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2
3// Example for: *yaml*
4
5// This example demonstrates loading an SPDX YAML document from disk into memory,
6// and then logging some attributes to the console.
7// Run project: go run exampleYAMLLoader.go ../sample-docs/yaml/SPDXYAMLExample-2.2.spdx.yaml
8package main
9
10import (
11	"fmt"
12	"os"
13	"strings"
14
15	spdx_yaml "github.com/spdx/tools-golang/yaml"
16)
17
18func main() {
19
20	// check that we've received the right number of arguments
21	args := os.Args
22	if len(args) != 2 {
23		fmt.Printf("Usage: %v <json-file-in>\n", args[0])
24		fmt.Printf("  Load SPDX 2.2 JSON file <spdx-file-in>, and\n")
25		fmt.Printf("  print portions of its creation info data.\n")
26		return
27	}
28
29	// open the SPDX file
30	fileIn := args[1]
31	r, err := os.Open(fileIn)
32	if err != nil {
33		fmt.Printf("Error while opening %v for reading: %v", fileIn, err)
34		return
35	}
36	defer r.Close()
37
38	// try to load the SPDX file's contents as a json file, version 2.2
39	doc, err := spdx_yaml.Load2_2(r)
40	if err != nil {
41		fmt.Printf("Error while parsing %v: %v", args[1], err)
42		return
43	}
44
45	// if we got here, the file is now loaded into memory.
46	fmt.Printf("Successfully loaded %s\n", args[1])
47
48	fmt.Println(strings.Repeat("=", 80))
49	fmt.Println("Some Attributes of the Document:")
50	fmt.Printf("Document Name:         %s\n", doc.DocumentName)
51	fmt.Printf("DataLicense:           %s\n", doc.DataLicense)
52	fmt.Printf("Document Namespace:    %s\n", doc.DocumentNamespace)
53	fmt.Printf("SPDX Version:          %s\n", doc.SPDXVersion)
54	fmt.Println(strings.Repeat("=", 80))
55}
56