-
Notifications
You must be signed in to change notification settings - Fork 12
Add simple cli to validate expressions #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,127 @@ | ||||||||||||||||||||
| package main | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import ( | ||||||||||||||||||||
| "bufio" | ||||||||||||||||||||
| "fmt" | ||||||||||||||||||||
| "io" | ||||||||||||||||||||
| "os" | ||||||||||||||||||||
| "strings" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| "github.com/github/go-spdx/v2/spdxexp" | ||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| var filePath string | ||||||||||||||||||||
|
|
||||||||||||||||||||
| var rootCmd = &cobra.Command{ | ||||||||||||||||||||
| Use: "spdx-validate", | ||||||||||||||||||||
| Short: "Validate SPDX license expressions", | ||||||||||||||||||||
| Long: `spdx-validate reads SPDX license expressions and validates them. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| By default it reads a single expression from stdin. Use -f/--file to read | ||||||||||||||||||||
| a newline-separated list of expressions from a file. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Exits 0 if all expressions are valid, or 1 if any are invalid. | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Examples: | ||||||||||||||||||||
| echo "MIT" | spdx-validate | ||||||||||||||||||||
| echo "Apache-2.0 OR MIT" | spdx-validate | ||||||||||||||||||||
| spdx-validate -f licenses.txt`, | ||||||||||||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||
| if filePath != "" { | ||||||||||||||||||||
| f, err := os.Open(filePath) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return fmt.Errorf("unable to open file: %w", err) | ||||||||||||||||||||
ahpook marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| defer f.Close() | ||||||||||||||||||||
| ok, err := validateExpressions(f, os.Stderr) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if !ok { | ||||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return nil | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ok, err := validateSingleExpression(os.Stdin, os.Stderr) | ||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| return err | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if !ok { | ||||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||||
| } | ||||||||||||||||||||
ahpook marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| return nil | ||||||||||||||||||||
| }, | ||||||||||||||||||||
| SilenceUsage: true, | ||||||||||||||||||||
| SilenceErrors: true, | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func init() { | ||||||||||||||||||||
| rootCmd.Flags().StringVarP(&filePath, "file", "f", "", "path to a newline-separated file of SPDX expressions") | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // validateSingleExpression reads one line from r, validates it as an SPDX | ||||||||||||||||||||
| // expression, and writes an error message to w if invalid. Returns (true, nil) | ||||||||||||||||||||
| // when valid, (false, nil) when invalid, or (false, err) on read errors. | ||||||||||||||||||||
| func validateSingleExpression(r io.Reader, w io.Writer) (bool, error) { | ||||||||||||||||||||
| scanner := bufio.NewScanner(r) | ||||||||||||||||||||
| if !scanner.Scan() { | ||||||||||||||||||||
|
||||||||||||||||||||
| scanner := bufio.NewScanner(r) | |
| if !scanner.Scan() { | |
| scanner := bufio.NewScanner(r) | |
| // Increase the scanner buffer to better handle long expressions. | |
| scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) | |
| if !scanner.Scan() { | |
| if err := scanner.Err(); err != nil { | |
| return false, fmt.Errorf("failed to read input: %w", err) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's not going to be a 64mb spdx expression.
Uh oh!
There was an error while loading. Please reload this page.