Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions util/spdx/genlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//go:build ignore
// +build ignore

// This file generates list.gen.go.
// It fetches the official SPDX license and exception lists from spdx.org.

package main

import (
"bytes"
"encoding/json"
"flag"
"go/format"
"io"
"log"
"net/http"
"os"
"sort"
"strings"
"text/template"
"time"
)

var (
outFile = flag.String("o", "list.gen.go", "`file` to write")
licensesURL = flag.String("licenses_url", "https://spdx.org/licenses/licenses.json", "`url` to fetch licenses from")
exceptionsURL = flag.String("exceptions_url", "https://spdx.org/licenses/exceptions.json", "`url` to fetch exceptions from")
)

type licenseInfo struct {
ID string `json:"licenseId"`
Name string `json:"name"`
// There are other fields we might want,
// like isDeprecatedLicenseId, isOsiApproved.
}

func fetchLicenses(url string) ([]licenseInfo, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
licenses := struct {
Licenses []licenseInfo `json:"licenses"`
}{}
if err := json.Unmarshal(body, &licenses); err != nil {
return nil, err
}
sort.Slice(licenses.Licenses, func(i, j int) bool {
return licenses.Licenses[i].ID < licenses.Licenses[j].ID
})
return licenses.Licenses, nil
}

type exceptionInfo struct {
ID string `json:"licenseExceptionId"`
}

func fetchExceptions(url string) ([]exceptionInfo, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
exceptions := struct {
Exceptions []exceptionInfo `json:"exceptions"`
}{}
if err := json.Unmarshal(body, &exceptions); err != nil {
return nil, err
}
sort.Slice(exceptions.Exceptions, func(i, j int) bool {
return exceptions.Exceptions[i].ID < exceptions.Exceptions[j].ID
})
return exceptions.Exceptions, nil
}

func main() {
flag.Parse()
licenses, err := fetchLicenses(*licensesURL)
if err != nil {
log.Fatalf("Fetching licenses: %v", err)
}
exceptions, err := fetchExceptions(*exceptionsURL)
if err != nil {
log.Fatalf("Fetching exceptions: %v", err)
}
data := struct {
Licenses []licenseInfo
LicensesURL string
Exceptions []exceptionInfo
ExceptionsURL string
Now time.Time
}{
Licenses: licenses,
LicensesURL: *licensesURL,
Exceptions: exceptions,
ExceptionsURL: *exceptionsURL,
Now: time.Now(),
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, &data); err != nil {
log.Fatalf("Generating template: %v", err)
}
src, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalf("Bad generated code: %v", err)
}
if err := os.WriteFile(*outFile, src, 0644); err != nil {
log.Fatalf("Writing file: %v", err)
}
}

var tmpl = template.Must(template.New("list").Funcs(template.FuncMap{
"lower": strings.ToLower,
"timeFormat": func(t time.Time) string { return t.Format(time.RFC1123) },
}).Parse(`
// Code generated by genlist.go at {{timeFormat .Now}}; DO NOT EDIT.

package spdx

// licenseIDs is the set of canonical license IDs.
// Based on data from {{.LicensesURL}}
var licenseIDs = map[string]bool{
{{- range .Licenses}}
{{printf "%q" .ID}}: true,
{{- end}}
}

// licenseIDIndex is a map of lower-case license IDs to their canonical form.
var licenseIDIndex = map[string]string{
{{- range .Licenses}}
{{printf "%q" (lower .ID)}}: {{printf "%q" .ID}},
{{- end}}
}

// exceptionIDs is the set of canonical exception IDs.
// Based on data from {{.ExceptionsURL}}
var exceptionIDs = map[string]bool{
{{- range .Exceptions}}
{{printf "%q" .ID}}: true,
{{- end}}
}

// exceptionIDIndex is a map of lower-case exception IDs to their canonical form.
var exceptionIDIndex = map[string]string{
{{- range .Exceptions}}
{{printf "%q" (lower .ID)}}: {{printf "%q" .ID}},
{{- end}}
}
`))
3 changes: 3 additions & 0 deletions util/spdx/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module deps.dev/util/spdx

go 1.23
Loading
Loading