-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from CatalinStratu/main
YAML documentation and JSON documentation fixes
- Loading branch information
Showing
7 changed files
with
225 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters