| 1 | // Package main validates newline-separated SPDX expressions from stdin or a file. |
| 2 | package main |
| 3 | |
| 4 | import ( |
| 5 | "bufio" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/github/go-spdx/v2/spdxexp" |
| 12 | "github.com/spf13/cobra" |
| 13 | ) |
| 14 | |
| 15 | var filePath string |
| 16 | |
| 17 | var rootCmd = &cobra.Command{ |
| 18 | Use: "spdx-validate", |
| 19 | Short: "Validate SPDX license expressions", |
| 20 | Long: `spdx-validate reads newline-separated SPDX license expressions and validates them. |
| 21 | |
| 22 | It reads from stdin by default, or from a file specified with -f/--file. |
| 23 | Blank lines are skipped. Exits 0 if all expressions are valid, or 1 if any |
| 24 | are invalid. |
| 25 | |
| 26 | Examples: |
| 27 | echo "MIT" | spdx-validate |
| 28 | printf "MIT\nApache-2.0\n" | spdx-validate |
| 29 | spdx-validate -f licenses.txt`, |
| 30 | RunE: func(_ *cobra.Command, _ []string) error { |
| 31 | var r io.Reader = os.Stdin |
| 32 | if filePath != "" { |
| 33 | // #nosec G304 -- file path is an explicit CLI input for this command. |
| 34 | f, err := os.Open(filePath) |
| 35 | if err != nil { |
| 36 | return fmt.Errorf("unable to open file: %w", err) |
| 37 | } |
| 38 | defer func() { |
| 39 | _ = f.Close() |
| 40 | }() |
| 41 | r = f |
| 42 | } |
| 43 | ok, err := validateExpressions(r, os.Stderr) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if !ok { |
| 48 | os.Exit(1) |
| 49 | } |
| 50 | return nil |
| 51 | }, |
| 52 | SilenceUsage: true, |
| 53 | SilenceErrors: true, |
| 54 | } |
| 55 | |
| 56 | func init() { |
| 57 | rootCmd.Flags().StringVarP(&filePath, "file", "f", "", "path to a newline-separated file of SPDX expressions") |
| 58 | } |
| 59 | |
| 60 | // validateExpressions reads newline-separated SPDX expressions from r, |
| 61 | // validates each one, and writes error messages to w for any that are invalid. |
| 62 | // Returns (true, nil) when all are valid, (false, nil) when any are invalid, or |
| 63 | // (false, err) on read errors or when no expressions are found. |
| 64 | func validateExpressions(r io.Reader, w io.Writer) (bool, error) { |
| 65 | scanner := bufio.NewScanner(r) |
| 66 | lineNum := 0 |
| 67 | failures := 0 |
| 68 | |
| 69 | for scanner.Scan() { |
| 70 | lineNum++ |
| 71 | line := strings.TrimSpace(scanner.Text()) |
| 72 | if line == "" { |
| 73 | continue |
| 74 | } |
| 75 | valid, _ := spdxexp.ValidateLicenses([]string{line}) |
| 76 | if !valid { |
| 77 | failures++ |
| 78 | _, _ = fmt.Fprintf(w, "line %d: invalid SPDX expression: %q\n", lineNum, line) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if err := scanner.Err(); err != nil { |
| 83 | return false, fmt.Errorf("error reading file: %w", err) |
| 84 | } |
| 85 | |
| 86 | if lineNum == 0 || (lineNum > 0 && failures == lineNum) { |
| 87 | return false, fmt.Errorf("no valid expressions found") |
| 88 | } |
| 89 | |
| 90 | if failures > 0 { |
| 91 | _, _ = fmt.Fprintf(w, "%d of %d expressions failed validation\n", failures, lineNum) |
| 92 | return false, nil |
| 93 | } |
| 94 | |
| 95 | return true, nil |
| 96 | } |
| 97 | |
| 98 | func main() { |
| 99 | if err := rootCmd.Execute(); err != nil { |
| 100 | fmt.Fprintln(os.Stderr, err) |
| 101 | os.Exit(1) |
| 102 | } |
| 103 | } |