license

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: Apache-2.0 Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	LicenseDataURL      = "https://spdx.org/licenses/"
	LicenseListFilename = "licenses.json"
	BaseReleaseURL      = "https://github.com/spdx/license-list-data/archive/refs/tags/"
	LatestReleaseURL    = "https://api.github.com/repos/spdx/license-list-data/releases/latest"
	EmbeddedDataDir     = "pkg/license/data/"
)

ListURL is the json list of all spdx licenses

Variables

View Source
var DebianLicenseLabels = map[string]string{
	"Apache-2.0": "Apache-2.0",
	"Artistic":   "Artistic-1.0-Perl",
	"BSD":        "BSD-1-Clause",
	"CC0-1.0":    "CC0-1.0",
	"GFDL-1.2":   "GFDL-1.2",
	"GFDL-1.3":   "GFDL-1.3",
	"GPL":        "GPL-1.0",
	"GPL-1":      "GPL-1.0",
	"GPL-2":      "GPL-2.0",
	"GPL-3":      "GPL-3.0",
	"LGPL-2":     "LGPL-2.0",
	"LGPL-2.1":   "LGPL-2.1",
	"LGPL-3":     "LGPL-3.0",
	"MPL-1.1":    "MPL-1.1",
	"MPL-2.0":    "MPL-2.0",
}

DebianLicenseLabels is a map to get the SPDX label from a debian label

View Source
var DefaultCatalogOpts = CatalogOptions{
	Version: "v3.21",
}

DefaultCatalogOpts are the predetermined settings. License and cache directories are in the temporary OS directory and are created if the do not exist.

The version included here is hardcoded and is intended to be the latest. The magefile in the project takes care of replacing this value when updating the license zip file.

DO NOT RENAME OR MOVE THIS OPTION WITHOUT MODIFYING THE MAGEFILE
View Source
var DefaultDownloaderOpts = &DownloaderOptions{
	EnableCache:       true,
	CacheDir:          "",
	parallelDownloads: 5,
}

DefaultDownloaderOpts set of options for the license downloader

View Source
var DefaultReaderOptions = &ReaderOptions{
	ConfidenceThreshold: 0.9,
}

DefaultReaderOptions is the default set of options for the classifier

Functions

func HasKubernetesBoilerPlate

func HasKubernetesBoilerPlate(filePath string) (bool, error)

HasKubernetesBoilerPlate checks if a file contains the Kubernetes License boilerplate

Types

type Catalog

type Catalog struct {
	Downloader *Downloader // License Downloader
	List       *List       // List of licenses
	// contains filtered or unexported fields
}

Catalog is an objec to interact with licenses and manifest creation

func NewCatalogWithOptions

func NewCatalogWithOptions(opts CatalogOptions) (catalog *Catalog, err error)

NewCatalogWithOptions returns a SPDX object with the specified options

func (*Catalog) GetLicense

func (catalog *Catalog) GetLicense(label string) *License

GetLicense returns a license struct from its SPDX ID label

func (*Catalog) LoadLicenses

func (catalog *Catalog) LoadLicenses() error

LoadLicenses reads the license data from the downloader

func (*Catalog) Options

func (catalog *Catalog) Options() CatalogOptions

Options returns a pointer to the catlog options

func (*Catalog) WriteLicensesAsText

func (catalog *Catalog) WriteLicensesAsText(targetDir string) error

WriteLicensesAsText writes the SPDX license collection to text files

type CatalogOptions

type CatalogOptions struct {
	CacheDir string // Directrory to catch the license we download from SPDX.org
	Version  string // Version of the licenses to download  (eg v3.19) or blank for latest
}

CatalogOptions are the spdx settings

type ClassifyResult

type ClassifyResult struct {
	File    string
	Text    string
	License *License
}

ClassifyResult abstracts the data resulting from a file classification

type DefaultDownloaderImpl

type DefaultDownloaderImpl struct {
	Options *DownloaderOptions
}

DefaultDownloaderImpl is the default implementation that gets licenses

func (*DefaultDownloaderImpl) DownloadLicenseArchive added in v0.5.0

func (ddi *DefaultDownloaderImpl) DownloadLicenseArchive(tag string) (zipData []byte, err error)

func (*DefaultDownloaderImpl) GetLatestTag added in v0.5.0

func (ddi *DefaultDownloaderImpl) GetLatestTag() (string, error)

GetLatestTag gets the latest version of the license list from github

func (*DefaultDownloaderImpl) GetLicenses

func (ddi *DefaultDownloaderImpl) GetLicenses(tag string) (licenses *List, err error)

GetLicenses downloads the main json file listing all SPDX supported licenses

func (*DefaultDownloaderImpl) SetOptions

func (ddi *DefaultDownloaderImpl) SetOptions(opts *DownloaderOptions)

SetOptions sets the implementation options

func (*DefaultDownloaderImpl) Version added in v0.5.0

func (ddi *DefaultDownloaderImpl) Version() string

Version returns the version from the options

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

Downloader handles downloading f license data

func NewDownloader

func NewDownloader() (*Downloader, error)

NewDownloader returns a downloader with the default options

func NewDownloaderWithOptions

func NewDownloaderWithOptions(opts *DownloaderOptions) (*Downloader, error)

NewDownloaderWithOptions returns a downloader with specific options

func (*Downloader) DownloadLicenseListToFile added in v0.5.0

func (d *Downloader) DownloadLicenseListToFile(tag, path string) (err error)

DownloadLicenseListToFile downloads the license list from github and stores it in a file

func (*Downloader) GetLatestTag added in v0.5.0

func (d *Downloader) GetLatestTag() (string, error)

GetLatestTag returns the last version of the SPDX LIcense list found on GitHub

func (*Downloader) GetLicenses

func (d *Downloader) GetLicenses() (*List, error)

GetLicenses is the mina function of the downloader. Returns a license list or an error if could get them

func (*Downloader) SetImplementation

func (d *Downloader) SetImplementation(di DownloaderImplementation)

SetImplementation sets the implementation that will drive the downloader

type DownloaderImplementation

type DownloaderImplementation interface {
	GetLicenses(versionTag string) (*List, error)
	SetOptions(*DownloaderOptions)
	GetLatestTag() (string, error)
	Version() string
	DownloadLicenseArchive(tag string) (zipData []byte, err error)
}

DownloaderImplementation has only one method

type DownloaderOptions

type DownloaderOptions struct {
	EnableCache bool   // Should we use the cache or not
	CacheDir    string // Directory where data will be cached, defaults to temporary dir

	Version string // Version of the licenses to download  (eg v3.19) or blank for latest
	// contains filtered or unexported fields
}

DownloaderOptions is a set of options for the license downloader

func (*DownloaderOptions) Validate

func (do *DownloaderOptions) Validate() error

Validate Checks the downloader options

type License

type License struct {
	IsDeprecatedLicenseID         bool     `json:"isDeprecatedLicenseId"`
	IsFsfLibre                    bool     `json:"isFsfLibre"`
	IsOsiApproved                 bool     `json:"isOsiApproved"`
	LicenseText                   string   `json:"licenseText"`
	StandardLicenseHeaderTemplate string   `json:"standardLicenseHeaderTemplate"`
	StandardLicenseTemplate       string   `json:"standardLicenseTemplate"`
	Name                          string   `json:"name"`
	LicenseID                     string   `json:"licenseId"`
	StandardLicenseHeader         string   `json:"standardLicenseHeader"`
	SeeAlso                       []string `json:"seeAlso"`
}

SPDXLicense is a license described in JSON

func ParseLicense

func ParseLicense(licenseJSON []byte) (license *License, err error)

ParseLicense parses a SPDX license from its JSON source

func (*License) WriteText

func (license *License) WriteText(filePath string) error

WriteText writes the SPDX license text to a text file

type List

type List struct {
	sync.RWMutex
	Version           string      `json:"licenseListVersion"`
	ReleaseDateString string      `json:"releaseDate "`
	LicenseData       []ListEntry `json:"licenses"`
	Licenses          map[string]*License
}

List abstracts the list of licenses published by SPDX.org

func (*List) Add

func (list *List) Add(license *License)

Add appends a license to the license list

type ListEntry

type ListEntry struct {
	IsOsiApproved   bool     `json:"isOsiApproved"`
	IsDeprectaed    bool     `json:"isDeprecatedLicenseId"`
	Reference       string   `json:"reference"`
	DetailsURL      string   `json:"detailsUrl"`
	ReferenceNumber int      `json:"referenceNumber"`
	Name            string   `json:"name"`
	LicenseID       string   `json:"licenseId"`
	SeeAlso         []string `json:"seeAlso"`
}

ListEntry a license entry in the list

type Reader

type Reader struct {
	Options *ReaderOptions
	// contains filtered or unexported fields
}

Reader is an object that finds and interprets license files

func NewReader

func NewReader() (*Reader, error)

NewReader returns a license reader with the default options

func NewReaderWithOptions

func NewReaderWithOptions(opts *ReaderOptions) (r *Reader, err error)

NewReaderWithOptions returns a new license reader with the specified options

func (*Reader) LicenseFromFile

func (r *Reader) LicenseFromFile(filePath string) (license *License, err error)

LicenseFromFile reads a file ans returns its license

func (*Reader) LicenseFromLabel

func (r *Reader) LicenseFromLabel(label string) (license *License)

LicenseFromLabel returns a spdx license from its label

func (*Reader) ReadLicenses

func (r *Reader) ReadLicenses(path string) (
	licenseList []*ClassifyResult, unknownPaths []string, err error,
)

ReadLicenses returns an array of all licenses found in the specified path

func (*Reader) ReadTopLicense

func (r *Reader) ReadTopLicense(path string) (*ClassifyResult, error)

ReadTopLicense returns the topmost license file in a directory

func (*Reader) SetImplementation

func (r *Reader) SetImplementation(i ReaderImplementation) error

SetImplementation sets the implementation that the license reader will use

type ReaderDefaultImpl

type ReaderDefaultImpl struct {
	// contains filtered or unexported fields
}

ReaderDefaultImpl the default license reader imlementation, uses Google's cicense classifier

func (*ReaderDefaultImpl) Catalog

func (d *ReaderDefaultImpl) Catalog() *Catalog

SPDX returns the reader's SPDX object

func (*ReaderDefaultImpl) Classifier

Classifier returns the license classifier

func (*ReaderDefaultImpl) ClassifyFile

func (d *ReaderDefaultImpl) ClassifyFile(path string) (licenseTag string, moreTags []string, err error)

ClassifyFile takes a file path and returns the most probable license tag

func (*ReaderDefaultImpl) ClassifyLicenseFiles

func (d *ReaderDefaultImpl) ClassifyLicenseFiles(paths []string) (
	licenseList []*ClassifyResult, unrecognizedPaths []string, err error,
)

ClassifyLicenseFiles takes a list of paths and tries to find return all licenses found in it

func (*ReaderDefaultImpl) FindLicenseFiles

func (d *ReaderDefaultImpl) FindLicenseFiles(path string) ([]string, error)

FindLicenseFiles will scan a directory and return files that may be licenses

func (*ReaderDefaultImpl) Initialize

func (d *ReaderDefaultImpl) Initialize(opts *ReaderOptions) error

Initialize checks the options and creates the needed objects

func (*ReaderDefaultImpl) LicenseFromFile

func (d *ReaderDefaultImpl) LicenseFromFile(path string) (license *License, err error)

LicenseFromFile a file path and returns its license

func (*ReaderDefaultImpl) LicenseFromLabel

func (d *ReaderDefaultImpl) LicenseFromLabel(label string) (license *License)

LicenseFromLabel return a spdx license from its label

type ReaderImplementation

type ReaderImplementation interface {
	Initialize(*ReaderOptions) error
	ClassifyLicenseFiles([]string) ([]*ClassifyResult, []string, error)
	ClassifyFile(string) (string, []string, error)
	LicenseFromFile(string) (*License, error)
	LicenseFromLabel(string) *License
	FindLicenseFiles(string) ([]string, error)
}

ReaderImplementation implements the basic lifecycle of a license reader: initializes -> finds license files to scan -> classifies them to a SPDX license

type ReaderOptions

type ReaderOptions struct {
	ConfidenceThreshold float64 // Miniumum confidence to consider a license detected
	WorkDir             string  // Directory where the reader will store its data
	CacheDir            string  // Optional directory where the reader will store its downloads cache
	LicenseDir          string  // Optional dir to store and read the SPDX licenses from
	LicenseListVersion  string  // Version of the SPDX license list to use
}

ReaderOptions are the optional settings for the license reader

func (*ReaderOptions) CachePath

func (ro *ReaderOptions) CachePath() string

CachePath return the full path to the downloads cache

func (*ReaderOptions) LicensesPath

func (ro *ReaderOptions) LicensesPath() string

LicensesPath return the full path the dir where the licenses are

func (*ReaderOptions) Validate

func (ro *ReaderOptions) Validate() error

Validate checks the options to verify the are sane

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL