Skip to content

Commit

Permalink
examples: Add comments with sample run commands (#123)
Browse files Browse the repository at this point in the history
Signed-off-by: CatalinStratu <catalinstratu45@gmail.com>
  • Loading branch information
CatalinStratu committed Mar 31, 2022
1 parent d93d09f commit 7e3a22c
Show file tree
Hide file tree
Showing 14 changed files with 2,060 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/1-load/example_load.go
Expand Up @@ -4,6 +4,7 @@

// This example demonstrates loading an SPDX tag-value file from disk into
// memory, and printing some of its contents to standard output.
// Run project: go run example_load.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx

package main

Expand Down
4 changes: 2 additions & 2 deletions examples/10-jsonloader/example_json_loader.go
Expand Up @@ -4,7 +4,7 @@

// This example demonstrates loading an SPDX json from disk into memory,
// and then logging out some attributes to the console .

// Run project: go run example_json_loader.go ../sample-docs/json/SPDXJSONExample-v2.2.spdx.json example.spdx
package main

import (
Expand All @@ -20,7 +20,7 @@ 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> <spdx-file-out>\n", args[0])
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(" save it out to <spdx-file-out>.\n")
return
Expand Down
1 change: 1 addition & 0 deletions examples/2-load-save/example_load_save.go
Expand Up @@ -4,6 +4,7 @@

// 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 example_load_save.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx test.spdx

package main

Expand Down
1 change: 1 addition & 0 deletions examples/4-search/example_search.go
Expand Up @@ -7,6 +7,7 @@
// [SPDX short-form IDs](https://spdx.org/ids/); filling those IDs into the
// document's Package and File license fields; and saving the resulting document
// to disk.
// Run project: go run example_search.go project2 ../../testdata/project2/folder test.spdx

package main

Expand Down
2 changes: 1 addition & 1 deletion examples/5-report/example_report.go
Expand Up @@ -5,7 +5,7 @@
// This example demonstrates loading an SPDX tag-value file from disk into memory,
// generating a basic report listing counts of the concluded licenses for its
// files, and printing the report to standard output.

// Run project: go run example_report.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx
package main

import (
Expand Down
2 changes: 1 addition & 1 deletion examples/6-licensediff/example_licensediff.go
Expand Up @@ -8,7 +8,7 @@
// This is generally only useful when run with two SPDX documents that
// describe licenses for subsequent versions of the same set of files, AND if
// they have the same identifier in both documents.

// Run project: go run example_licensediff.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx ../sample-docs/example2/spdx/SPDXTagExample-v2.2.spdx
package main

import (
Expand Down
17 changes: 11 additions & 6 deletions examples/7-rdfloader/exampleRDFLoader.go
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

// Run project: go run exampleRDFLoader.go ../sample-docs/rdf/SPDXRdfExample-v2.2.spdx.rdf
package main

import (
Expand All @@ -9,20 +9,25 @@ import (
"strings"
)

func getFilePathFromUser() string {
func getFilePathFromUser() (string, error) {
if len(os.Args) == 1 {
// user hasn't specified the rdf file path
panic("kindly provide path of the rdf file to be loaded as a spdx-document while running this file")
return "", fmt.Errorf("kindly provide path of the rdf file to be loaded as a spdx-document while running this file")
}
return os.Args[1]
return os.Args[1], nil
}

func main() {
// example to use the rdfLoader.
filePath := getFilePathFromUser()
filePath, ok := getFilePathFromUser()
if ok != nil {
fmt.Println(fmt.Errorf("%v", ok))
os.Exit(1)
}
file, err := os.Open(filePath)
if err != nil {
panic(fmt.Errorf("error opening File: %s", err))
fmt.Println(fmt.Errorf("error opening File: %s", err))
os.Exit(1)
}

// loading the spdx-document
Expand Down
4 changes: 2 additions & 2 deletions examples/8-jsontotv/examplejsontotv.go
Expand Up @@ -4,7 +4,7 @@

// 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
package main

import (
Expand All @@ -20,7 +20,7 @@ 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> <spdx-file-out>\n", args[0])
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(" save it out to <spdx-file-out>.\n")
return
Expand Down
4 changes: 2 additions & 2 deletions examples/9-tvtojson/exampletvtojson.go
Expand Up @@ -4,7 +4,7 @@

// 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 exampletvtojson.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx example.json
package main

import (
Expand All @@ -20,7 +20,7 @@ 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> <spdx-file-out>\n", args[0])
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")
return
Expand Down
14 changes: 11 additions & 3 deletions examples/README.md
Expand Up @@ -11,21 +11,23 @@ tools-golang sub-packages.

This example demonstrates loading an SPDX tag-value file from disk into memory,
and printing some of its contents to standard output.
#### Run project: *go run example_load.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx*

## 2-load-save/

*tvloader*, *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 example_load_save.go ../sample-docs/example1/spdx/example1.spdx test.spdx*
## 3-build/

*builder*, *tvsaver*

This example demonstrates building an 'empty' SPDX document in memory that
corresponds to a given directory's contents, including all files with their
hashes and the package's verification code, and saving the document to disk.
#### Run project: *go run example_build.go project2 ../../testdata/project2 test.spdx*

## 4-search/

Expand All @@ -35,6 +37,7 @@ This example demonstrates building an SPDX document for a directory's contents
(implicitly using *builder*); searching through that directory for [SPDX
short-form IDs](https://spdx.org/ids/); filling those IDs into the document's
Package and File license fields; and saving the resulting document to disk.
#### Run project: *go run example_search.go project2 ../../testdata/project2/folder test.spdx*

## 5-report/

Expand All @@ -43,7 +46,8 @@ Package and File license fields; and saving the resulting document to disk.
This example demonstrates loading an SPDX tag-value file from disk into memory,
generating a basic report listing counts of the concluded licenses for its
files, and printing the report to standard output.

#### Run project: * go run example_report.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx
*
## 6-licensediff

*licensediff*, *tvloader*
Expand All @@ -55,32 +59,36 @@ with matching IDs in each document.
This is generally only useful when run with two SPDX documents that describe
licenses for subsequent versions of the same set of files, AND if they have
the same identifier in both documents.

#### Run project *Run project: go run example_licensediff.go ../sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx ../sample-docs/example2/spdx/SPDXTagExample2-v2.2.spdx*

## 7-rdfloader

*rdfloader*, *spdx*

This example demonstrates loading an SPDX rdf file from disk into memory
and then printing the corresponding spdx struct for the document.
#### Run project: *go run exampleRDFLoader.go ../sample-docs/rdf/SPDXRdfExample-v2.2.spdx.rdf*

## 8-jsontotv

*jsonloader*, *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*

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/example1/spdx/SPDXTagExample-v2.2.spdx example.json*

## 10-jsonloader

*jsonloader*

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 example.spdx*
57 changes: 57 additions & 0 deletions examples/sample-docs/example1/spdx/SPDXTagExample-v2.2.spdx
@@ -0,0 +1,57 @@
SPDXVersion: SPDX-2.2
DataLicense: CC0-1.0
SPDXID: SPDXRef-DOCUMENT
DocumentName: hello
DocumentNamespace: https://swinslow.net/spdx-examples/example1/hello-v3
Creator: Person: Steve Winslow (steve@swinslow.net)
Creator: Tool: github.com/spdx/tools-golang/builder
Creator: Tool: github.com/spdx/tools-golang/idsearcher
Created: 2021-08-26T01:46:00Z

##### Package: hello

PackageName: hello
SPDXID: SPDXRef-Package-hello
PackageDownloadLocation: git+https://github.com/swinslow/spdx-examples.git#example1/content
FilesAnalyzed: true
PackageVerificationCode: 9d20237bb72087e87069f96afb41c6ca2fa2a342
PackageLicenseConcluded: GPL-3.0-or-later
PackageLicenseInfoFromFiles: GPL-3.0-or-later
PackageLicenseDeclared: GPL-3.0-or-later
PackageCopyrightText: NOASSERTION

Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package-hello

FileName: /build/hello
SPDXID: SPDXRef-hello-binary
FileType: BINARY
FileChecksum: SHA1: 20291a81ef065ff891b537b64d4fdccaf6f5ac02
FileChecksum: SHA256: 83a33ff09648bb5fc5272baca88cf2b59fd81ac4cc6817b86998136af368708e
FileChecksum: MD5: 08a12c966d776864cc1eb41fd03c3c3d
LicenseConcluded: GPL-3.0-or-later
LicenseInfoInFile: NOASSERTION
FileCopyrightText: NOASSERTION

FileName: /src/Makefile
SPDXID: SPDXRef-Makefile
FileType: SOURCE
FileChecksum: SHA1: 69a2e85696fff1865c3f0686d6c3824b59915c80
FileChecksum: SHA256: 5da19033ba058e322e21c90e6d6d859c90b1b544e7840859c12cae5da005e79c
FileChecksum: MD5: 559424589a4f3f75fd542810473d8bc1
LicenseConcluded: GPL-3.0-or-later
LicenseInfoInFile: GPL-3.0-or-later
FileCopyrightText: NOASSERTION

FileName: /src/hello.c
SPDXID: SPDXRef-hello-src
FileType: SOURCE
FileChecksum: SHA1: 20862a6d08391d07d09344029533ec644fac6b21
FileChecksum: SHA256: b4e5ca56d1f9110ca94ed0bf4e6d9ac11c2186eb7cd95159c6fdb50e8db5a823
FileChecksum: MD5: 935054fe899ca782e11003bbae5e166c
LicenseConcluded: GPL-3.0-or-later
LicenseInfoInFile: GPL-3.0-or-later
FileCopyrightText: Copyright Contributors to the spdx-examples project.

Relationship: SPDXRef-hello-binary GENERATED_FROM SPDXRef-hello-src
Relationship: SPDXRef-hello-binary GENERATED_FROM SPDXRef-Makefile
Relationship: SPDXRef-Makefile BUILD_TOOL_OF SPDXRef-Package-hello

0 comments on commit 7e3a22c

Please sign in to comment.