| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | ) |
| 8 | |
| 9 | func main() { |
| 10 | cmd := "extract" |
| 11 | |
| 12 | if len(os.Args) > 1 { |
| 13 | cmd = os.Args[1] |
| 14 | } |
| 15 | argsRemainder := []string{} |
| 16 | if len(os.Args) > 2 { |
| 17 | argsRemainder = os.Args[2:] |
| 18 | } |
| 19 | |
| 20 | flagSet := flag.NewFlagSet("run", flag.ExitOnError) |
| 21 | extractLicenses := flagSet.Bool("l", false, "Should license ids be extracted?") |
| 22 | extractExceptions := flagSet.Bool("e", false, "Should exception ids be extracted?") |
| 23 | help := flagSet.Bool("h", false, "Show help") |
| 24 | |
| 25 | err := flagSet.Parse(argsRemainder) |
| 26 | if err != nil { |
| 27 | fmt.Printf("error parsing flags for run: %v\n", err) |
| 28 | os.Exit(1) |
| 29 | } |
| 30 | |
| 31 | switch cmd { |
| 32 | case "extract": |
| 33 | if *help || (!*extractLicenses && !*extractExceptions) { |
| 34 | writeHelpMessage() |
| 35 | os.Exit(0) |
| 36 | } |
| 37 | if *extractLicenses { |
| 38 | fmt.Println("-------------------------") |
| 39 | fmt.Println("Extracting license ids...") |
| 40 | err := extractLicenseIDs() |
| 41 | if err != nil { |
| 42 | fmt.Printf("error extracting license ids: %v\n", err) |
| 43 | os.Exit(1) |
| 44 | } |
| 45 | fmt.Println("Done!") |
| 46 | } |
| 47 | if *extractExceptions { |
| 48 | fmt.Println("---------------------------") |
| 49 | fmt.Println("Extracting exception ids...") |
| 50 | err := extractExceptionLicenseIDs() |
| 51 | if err != nil { |
| 52 | fmt.Printf("error extracting exception ids: %v\n", err) |
| 53 | os.Exit(1) |
| 54 | } |
| 55 | fmt.Println("Done!") |
| 56 | fmt.Println("---------------------------") |
| 57 | } |
| 58 | default: |
| 59 | writeHelpMessage() |
| 60 | os.Exit(0) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func writeHelpMessage() { |
| 65 | fmt.Println("") |
| 66 | fmt.Println("Extracts license, deprecation, and exception ids from the official spdx license list data.") |
| 67 | fmt.Println("The source data needs to be manually updated by copying the licenses.json file from") |
| 68 | fmt.Println("https://github.com/spdx/license-list-data/blob/main/json/licenses.json and exceptions.json") |
| 69 | fmt.Println("file from https://github.com/spdx/license-list-data/blob/main/json/exceptions.json.") |
| 70 | fmt.Println("") |
| 71 | fmt.Println("After running the extract command, the license_ids.json, deprecated_ids.json, and exception_ids.json") |
| 72 | fmt.Println("files will be overwritten with the extracted ids. These license ids can then be used to update the") |
| 73 | fmt.Println("spdxexp/license.go file.") |
| 74 | fmt.Println("") |
| 75 | fmt.Println("Command to run all extractions (run command from the /cmd directory):") |
| 76 | fmt.Println(" `go run . extract -l -e`") |
| 77 | fmt.Println("") |
| 78 | fmt.Println("Usage options:") |
| 79 | fmt.Println(" -h: prints this help message") |
| 80 | fmt.Println(" -l: Extract license ids") |
| 81 | fmt.Println(" -e: Extract exception ids") |
| 82 | fmt.Println("") |
| 83 | os.Exit(0) |
| 84 | } |