| 1 | <sub>[](https://pkg.go.dev/github.com/github/go-spdx/[email protected]/spdxexp) spdxexp</sub> <br> |
| 2 | <sub>[](https://pkg.go.dev/github.com/github/go-spdx/[email protected]/spdxexp/spdxlicenses) spdxlicenses</sub> |
| 3 | |
| 4 | # go-spdx |
| 5 | |
| 6 | Golang implementation of a checker for determining if a set of SPDX IDs satisfies an SPDX Expression. |
| 7 | |
| 8 | ## Installation |
| 9 | |
| 10 | There are several ways to include a go package. To download and install, you can use `go get`. The command for that is: |
| 11 | |
| 12 | ```sh |
| 13 | go get github.com/github/go-spdx@latest |
| 14 | ``` |
| 15 | |
| 16 | ## Packages |
| 17 | |
| 18 | - [spdxexp](https://pkg.go.dev/github.com/github/go-spdx/spdxexp) - Expression package validates licenses and determines if a license expression is satisfied by a list of licenses. Validity of a license is determined by the SPDX license list. |
| 19 | |
| 20 | ## CLI: spdx-validate |
| 21 | |
| 22 | `spdx-validate` is a command-line tool that validates SPDX license expressions. |
| 23 | |
| 24 | ### Building |
| 25 | |
| 26 | ```sh |
| 27 | go build -o spdx-validate ./cmd/spdx-validate/ |
| 28 | ``` |
| 29 | |
| 30 | ### Usage |
| 31 | |
| 32 | **Validate expressions on stdin:** |
| 33 | |
| 34 | Stdin is read as a stream of newline-separated expressions: |
| 35 | |
| 36 | ```sh |
| 37 | echo "MIT" | ./spdx-validate |
| 38 | printf "MIT\nApache-2.0\nBSD-3-Clause\n" | ./spdx-validate |
| 39 | ``` |
| 40 | |
| 41 | Exits with code 0 if all expressions are valid, or code 1 (with error messages on stderr) if any are invalid. |
| 42 | |
| 43 | ```sh |
| 44 | $ printf "MIT\nBOGUS\nApache-2.0\n" | ./spdx-validate |
| 45 | line 2: invalid SPDX expression: "BOGUS" |
| 46 | 1 of 3 expressions failed validation |
| 47 | ``` |
| 48 | |
| 49 | **Validate from a file with `-f`/`--file`:** |
| 50 | |
| 51 | ```sh |
| 52 | ./spdx-validate -f licenses.txt |
| 53 | ``` |
| 54 | |
| 55 | The file should contain one SPDX expression per line. Blank lines are skipped. |
| 56 | |
| 57 | ```sh |
| 58 | $ cat licenses.txt |
| 59 | MIT |
| 60 | NOT-A-LICENSE |
| 61 | Apache-2.0 |
| 62 | |
| 63 | $ ./spdx-validate -f licenses.txt |
| 64 | line 2: invalid SPDX expression: "NOT-A-LICENSE" |
| 65 | 1 of 3 expressions failed validation |
| 66 | ``` |
| 67 | |
| 68 | ## Public API |
| 69 | |
| 70 | _NOTE: The public API is initially limited to the Satisfies and ValidateLicenses functions. If |
| 71 | there is interest in the output of the parser or license checking being public, please submit an |
| 72 | issue for consideration._ |
| 73 | |
| 74 | ### Function: Satisfies |
| 75 | |
| 76 | ```go |
| 77 | Satisfies(testExpression string, allowedList []string, options *Options) |
| 78 | ``` |
| 79 | |
| 80 | **Parameter: testExpression** |
| 81 | |
| 82 | testExpression is an [SPDX expression](https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/#d1-overview) describing the licensing terms of source code or a binary file. |
| 83 | |
| 84 | Example expressions that can be used for testExpression: |
| 85 | |
| 86 | ```go |
| 87 | "MIT" |
| 88 | "MIT AND Apache-2.0" |
| 89 | "MIT OR Apache-2.0" |
| 90 | "MIT AND (Apache-1.0 OR Apache-2.0)" |
| 91 | "Apache-1.0+" |
| 92 | "DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2" |
| 93 | "GPL-2.0 WITH Bison-exception-2.2" |
| 94 | ``` |
| 95 | |
| 96 | _See satisfies_test.go for more example expressions._ |
| 97 | |
| 98 | **Parameter: allowedList** |
| 99 | |
| 100 | allowedList is an array of single licenses describing what licenses can be used to satisfy the testExpression. |
| 101 | |
| 102 | Example allowedList: |
| 103 | |
| 104 | ```go |
| 105 | []string{"MIT"} |
| 106 | []string{"MIT", "Apache-2.0"} |
| 107 | []string{"MIT", "Apache-2.0", "ISC", "GPL-2.0"} |
| 108 | []string{"MIT", "Apache-1.0+"} |
| 109 | []string{"GPL-2.0-or-later"} |
| 110 | ``` |
| 111 | |
| 112 | **N.B.** If at least one of expressions from `allowedList` is not a valid SPDX expression, the call |
| 113 | to `Satisfies` will produce an error. Use [`ValidateLicenses`](###-ValidateLicenses) function |
| 114 | to first check if all of the expressions from `allowedList` are valid. |
| 115 | |
| 116 | #### Examples: Satisfies returns true |
| 117 | |
| 118 | [Go Playground for Satisfies](https://go.dev/play/p/Ul8H15hyEpQ) |
| 119 | |
| 120 | ```go |
| 121 | Satisfies("MIT", []string{"MIT"}) |
| 122 | Satisfies("MIT", []string{"MIT", "Apache-2.0"}) |
| 123 | Satisfies("Apache-2.0", []string{"Apache-1.0+"}) |
| 124 | Satisfies("MIT OR Apache-2.0", []string{"Apache-2.0"}) |
| 125 | Satisfies("MIT OR Apache-2.0", []string{"MIT", "Apache-2.0"}) |
| 126 | Satisfies("MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}) |
| 127 | Satisfies("MIT AND Apache-2.0", []string{"MIT", "Apache-2.0", "GPL-2.0"}) |
| 128 | ``` |
| 129 | |
| 130 | #### Examples: Satisfies returns false |
| 131 | |
| 132 | ```go |
| 133 | Satisfies("MIT", []string{"Apache-2.0"}) |
| 134 | Satisfies("Apache-1.0", []string{"Apache-2.0+"}) |
| 135 | Satisfies("MIT AND Apache-2.0", []string{"MIT"}) |
| 136 | ``` |
| 137 | |
| 138 | ### ValidateLicenses |
| 139 | |
| 140 | ```go |
| 141 | func ValidateLicenses(licenses []string) (bool, []string) |
| 142 | ``` |
| 143 | |
| 144 | Function `ValidateLicenses` is used to determine if any of the provided license expressions is |
| 145 | invalid. |
| 146 | |
| 147 | **parameter: licenses** |
| 148 | |
| 149 | Licenses is a slice of strings which must be validated as SPDX expressions. |
| 150 | |
| 151 | **returns** |
| 152 | |
| 153 | Function `ValidateLicenses` has 2 return values. First is `bool` which equals `true` if all of |
| 154 | the provided licenses provided are valid, and `false` otherwise. |
| 155 | |
| 156 | The second parameter is a slice of all invalid licenses which were provided. |
| 157 | |
| 158 | #### Examples: ValidateLicenses returns no invalid licenses |
| 159 | |
| 160 | ```go |
| 161 | valid, invalidLicenses := ValidateLicenses([]string{"Apache-2.0"}) |
| 162 | assert.True(valid) |
| 163 | assert.Empty(invalidLicenses) |
| 164 | ``` |
| 165 | |
| 166 | #### Examples: ValidateLicenses returns invalid licenses |
| 167 | |
| 168 | ```go |
| 169 | valid, invalidLicenses := ValidateLicenses([]string{"NON-EXISTENT-LICENSE", "MIT"}) |
| 170 | assert.False(valid) |
| 171 | assert.Contains(invalidLicenses, "NON-EXISTENT-LICENSE") |
| 172 | assert.NotContains(invalidLicenses, "MIT") |
| 173 | ``` |
| 174 | |
| 175 | #### Examples: ValidateLicenses works with SPDX expressions |
| 176 | |
| 177 | ```go |
| 178 | valid, invalidLicenses := ValidateLicenses([]string{"MIT AND APACHE-2.0"}) |
| 179 | assert.True(valid) |
| 180 | assert.NotContains(invalidLicenses, "MIT AND APACHE-2.0") |
| 181 | ``` |
| 182 | |
| 183 | ### ExtractLicenses |
| 184 | |
| 185 | ```go |
| 186 | func ExtractLicenses(expression string) ([]string, error) |
| 187 | ``` |
| 188 | |
| 189 | Function `ExtractLicenses` is used to extract licenses from the given expression without duplicates. |
| 190 | |
| 191 | **parameter: expression** |
| 192 | |
| 193 | `expression` is an SPDX expression string. |
| 194 | |
| 195 | **returns** |
| 196 | |
| 197 | Function `ExtractLicenses` has 2 return values. First is `[]string` which contains all of the SPDX licenses without duplicates. |
| 198 | |
| 199 | The second return value is an `error` which is not `nil` if the given expression is not a valid SPDX expression. |
| 200 | |
| 201 | #### Example |
| 202 | |
| 203 | ```go |
| 204 | licenses, err := ExtractLicenses("(MIT AND APACHE-2.0) OR (APACHE-2.0)") |
| 205 | assert.Equal(licenses, []string{"MIT", "Apache-2.0"}) |
| 206 | ``` |
| 207 | |
| 208 | ## Background |
| 209 | |
| 210 | This package was developed to support testing whether a repository's license requirements are met by an allowed-list of licenses. |
| 211 | |
| 212 | Dependencies are defined in [go.mod](./go.mod). |
| 213 | |
| 214 | Contributions and requests are welcome. Refer to the [Contributing](#contributing) section for more information including how to set up a test environment and install dependencies. |
| 215 | |
| 216 | ## License |
| 217 | |
| 218 | This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE.md) for the full terms. |
| 219 | |
| 220 | ## Maintainers |
| 221 | |
| 222 | - @elrayle |
| 223 | - @dangoor |
| 224 | |
| 225 | ## Support |
| 226 | |
| 227 | You can expect the following support: |
| 228 | |
| 229 | - bug fixes |
| 230 | - review of feature request issues |
| 231 | - review of questions in discussions |
| 232 | |
| 233 | ## Contributing |
| 234 | |
| 235 | Contributions in the form of bug identification Issues, bug fix PRs, and feature requests are welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) for more information on how to get involved and set up a testing environment. |
| 236 | |
| 237 | _NOTE: The list of valid licenses is maintained manually. If you notice a missing license, an excellent way to contribute to the long term viability of this package is to open an Issue or PR addressing the missing license._ |
| 238 | |
| 239 | ## Acknowledgement |
| 240 | |
| 241 | The process for parsing and evaluating expressions is a translation from JavaScript to Go based heavily on the JavaScript implementation defined across several repositories. |
| 242 | |
| 243 | - [spdx-satisfies.js](https://github.com/clearlydefined/spdx-satisfies.js) |
| 244 | - [spdx-expression-parse.js](https://github.com/clearlydefined/spdx-expression-parse.js) |
| 245 | - [spdx-ranges](https://github.com/jslicense/spdx-ranges.js) |
| 246 | - [spdx-compare](https://github.com/jslicense/spdx-compare.js) |
| 247 | - [spdx-license-ids](https://github.com/jslicense/spdx-license-ids) |
| 248 | - [spdx-exceptions](https://github.com/jslicense/spdx-exceptions.json) |