| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "go/format" |
| 7 | "os" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type LicenseData struct { |
| 12 | Version string `json:"licenseListVersion"` |
| 13 | Licenses []License `json:"licenses"` |
| 14 | } |
| 15 | |
| 16 | type License struct { |
| 17 | Reference string `json:"reference"` |
| 18 | IsDeprecated bool `json:"isDeprecatedLicenseId"` |
| 19 | DetailsURL string `json:"detailsUrl"` |
| 20 | ReferenceNumber int `json:"referenceNumber"` |
| 21 | Name string `json:"name"` |
| 22 | LicenseID string `json:"licenseId"` |
| 23 | SeeAlso []string `json:"seeAlso"` |
| 24 | IsOsiApproved bool `json:"isOsiApproved"` |
| 25 | } |
| 26 | |
| 27 | // extractLicenseIDs reads the official licenses.json file copied from spdx/license-list-data |
| 28 | // and writes two files, license_ids.json and deprecated_license_ids.json, containing just |
| 29 | // the license IDs and deprecated license IDs, respectively. It returns an error if it |
| 30 | // encounters one. |
| 31 | func extractLicenseIDs() error { |
| 32 | // open file |
| 33 | file, err := os.Open("licenses.json") |
| 34 | if err != nil { |
| 35 | fmt.Println(err) |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | // read in all licenses marshalled into a slice of license structs |
| 40 | var licenseData LicenseData |
| 41 | err = json.NewDecoder(file).Decode(&licenseData) |
| 42 | if err != nil { |
| 43 | fmt.Println(err) |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | // create two slices of license IDs, one for deprecated and one for active |
| 48 | var activeLicenseIDs []string |
| 49 | var deprecatedLicenseIDs []string |
| 50 | for _, l := range licenseData.Licenses { |
| 51 | if l.IsDeprecated { |
| 52 | deprecatedLicenseIDs = append(deprecatedLicenseIDs, l.LicenseID) |
| 53 | } else { |
| 54 | activeLicenseIDs = append(activeLicenseIDs, l.LicenseID) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // generate the GetLicenses() function in get_licenses.go |
| 59 | getLicensesContents := []byte(`package spdxlicenses |
| 60 | |
| 61 | // Code generated by go-spdx cmd/license.go. DO NOT EDIT. |
| 62 | // Source: https://github.com/spdx/license-list-data specifies official SPDX license list. |
| 63 | |
| 64 | import "strings" |
| 65 | |
| 66 | // IsActiveLicense does a case-insensitive lookup for the license id in the active licenses map. |
| 67 | // It returns true and the case-sensitive ID if found, otherwise false and the original id. |
| 68 | func IsActiveLicense(id string) (bool, string) { |
| 69 | foundID, ok := licensesMap[strings.ToUpper(id)] |
| 70 | if ok { |
| 71 | return true, foundID |
| 72 | } |
| 73 | return false, id |
| 74 | } |
| 75 | |
| 76 | // GetLicensesMap returns a map of active license IDs keyed by uppercase ID. |
| 77 | func GetLicensesMap() map[string]string { |
| 78 | copied := make(map[string]string, len(licensesMap)) |
| 79 | for k, v := range licensesMap { |
| 80 | copied[k] = v |
| 81 | } |
| 82 | return copied |
| 83 | } |
| 84 | |
| 85 | // GetLicenses returns a slice of active license IDs. |
| 86 | func GetLicenses() []string { |
| 87 | return []string{ |
| 88 | `) |
| 89 | for _, id := range activeLicenseIDs { |
| 90 | getLicensesContents = append(getLicensesContents, ` "`+id+`", |
| 91 | `...) |
| 92 | } |
| 93 | getLicensesContents = append(getLicensesContents, ` } |
| 94 | } |
| 95 | |
| 96 | `...) |
| 97 | |
| 98 | getLicensesContents = append(getLicensesContents, `var licensesMap = map[string]string{ |
| 99 | `...) |
| 100 | for _, id := range activeLicenseIDs { |
| 101 | getLicensesContents = append(getLicensesContents, ` "`+strings.ToUpper(id)+`": "`+id+`", |
| 102 | `...) |
| 103 | } |
| 104 | getLicensesContents = append(getLicensesContents, `} |
| 105 | `...) |
| 106 | |
| 107 | getLicensesContents, err = format.Source(getLicensesContents) |
| 108 | if err != nil { |
| 109 | return fmt.Errorf("format generated get_licenses.go: %w", err) |
| 110 | } |
| 111 | |
| 112 | err = os.WriteFile("../spdxexp/spdxlicenses/get_licenses.go", getLicensesContents, 0600) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | fmt.Println("Writing `../spdxexp/spdxlicenses/get_licenses.go`... COMPLETE") |
| 117 | |
| 118 | // generate the GetDeprecated() function in get_deprecated.go |
| 119 | getDeprecatedContents := []byte(`package spdxlicenses |
| 120 | |
| 121 | // Code generated by go-spdx cmd/license.go. DO NOT EDIT. |
| 122 | // Source: https://github.com/spdx/license-list-data specifies official SPDX license list. |
| 123 | |
| 124 | import "strings" |
| 125 | |
| 126 | // IsDeprecatedLicense does a case-insensitive lookup for the license id in the deprecated licenses map. |
| 127 | // It returns true and the case-sensitive ID if found, otherwise false and the original id. |
| 128 | func IsDeprecatedLicense(id string) (bool, string) { |
| 129 | foundID, ok := deprecatedMap[strings.ToUpper(id)] |
| 130 | if ok { |
| 131 | return true, foundID |
| 132 | } |
| 133 | return false, id |
| 134 | } |
| 135 | |
| 136 | // GetDeprecatedMap returns a map of deprecated license IDs keyed by uppercase ID. |
| 137 | func GetDeprecatedMap() map[string]string { |
| 138 | copied := make(map[string]string, len(deprecatedMap)) |
| 139 | for k, v := range deprecatedMap { |
| 140 | copied[k] = v |
| 141 | } |
| 142 | return copied |
| 143 | } |
| 144 | |
| 145 | // GetDeprecated returns a slice of deprecated license IDs. |
| 146 | func GetDeprecated() []string { |
| 147 | return []string{ |
| 148 | `) |
| 149 | for _, id := range deprecatedLicenseIDs { |
| 150 | getDeprecatedContents = append(getDeprecatedContents, ` "`+id+`", |
| 151 | `...) |
| 152 | } |
| 153 | getDeprecatedContents = append(getDeprecatedContents, ` } |
| 154 | } |
| 155 | |
| 156 | `...) |
| 157 | |
| 158 | getDeprecatedContents = append(getDeprecatedContents, `var deprecatedMap = map[string]string{ |
| 159 | `...) |
| 160 | for _, id := range deprecatedLicenseIDs { |
| 161 | getDeprecatedContents = append(getDeprecatedContents, ` "`+strings.ToUpper(id)+`": "`+id+`", |
| 162 | `...) |
| 163 | } |
| 164 | getDeprecatedContents = append(getDeprecatedContents, `} |
| 165 | `...) |
| 166 | |
| 167 | getDeprecatedContents, err = format.Source(getDeprecatedContents) |
| 168 | if err != nil { |
| 169 | return fmt.Errorf("format generated get_deprecated.go: %w", err) |
| 170 | } |
| 171 | |
| 172 | err = os.WriteFile("../spdxexp/spdxlicenses/get_deprecated.go", getDeprecatedContents, 0600) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | fmt.Println("Writing `../spdxexp/spdxlicenses/get_deprecated.go`... COMPLETE") |
| 177 | |
| 178 | return nil |
| 179 | } |