Skip to content

Commit

Permalink
Merge pull request #141 from CatalinStratu/main
Browse files Browse the repository at this point in the history
YAML documentation and JSON documentation fixes
  • Loading branch information
swinslow authored May 15, 2022
2 parents c21431f + cfe2ffb commit a532726
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/10-jsonloader/example_json_loader.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
68 changes: 68 additions & 0 deletions examples/11-yamltotv/exampleyamltotv.go
Original file line number Diff line number Diff line change
@@ -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)
}
68 changes: 68 additions & 0 deletions examples/12-tvtoyaml/exampletvtoyaml.go
Original file line number Diff line number Diff line change
@@ -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)
}
55 changes: 55 additions & 0 deletions examples/13-yamlloader/exampleYAMLLoader.go
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 2 additions & 2 deletions examples/8-jsontotv/examplejsontotv.go
Original file line number Diff line number Diff line change
@@ -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 .
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions examples/9-tvtojson/exampletvtojson.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
30 changes: 27 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,48 @@ 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.
#### Run project: *go run examplejsontotv.go ../sample-docs/json/SPDXJSONExample-v2.2.spdx.json example.spdx*

## 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.
#### Run project: *go run exampletvtojson.go ../sample-docs/tv/hello.spdx example.json*

## 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*

0 comments on commit a532726

Please sign in to comment.