license

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2015 License: MIT, MPL-2.0 Imports: 7 Imported by: 0

README

go-license Build Status

A license management utility for programs written in Golang.

This program handles identifying software licenses and standardizing on a short, abbreviated name for each known license type.

Enforcement

License identifier enforcement is not strict. This makes it possible to warn when an unrecognized license type is used, encouraging either conformance or an update to the list of known licenses. There is no way we can know all types of licenses.

License guessing

This program also provides naive license guessing based on the license body (text). This makes it easy to just throw a blob of text in and get a standardized license identifier string out.

It is also possible to have go-license guess the file name that contains the license data. This is done by scanning a directory for well-known license file names.

Recognized License Types

MIT
The MIT license. (text)

NewBSD
The "new" or "revised" BSD license. (text)

FreeBSD
The "simplified" BSD license. (text)

Apache-2.0
Apache License, version 2.0 (text)

MPL-2.0
The Mozilla Public License v2.0 (text)

GPL-2.0
The GNU General Public License v2.0 (text)

GPL-3.0
The GNU General Public License v3.0 (text)

LGPL-2.1
GNU Library or "Lesser" General Public License v2.1 (text)

LGPL-3.0
GNU Library or "Lesser" General Public License v3.0 (text)

CDDL-1.0
Common Development and Distribution License v1.0 (text)

EPL-1.0
Eclipse Public License v1.0 (text)

Example

package main

import (
    "fmt"
    "github.com/ryanuber/go-license"
)

func main() {
    // This case will work if there is a guessable license file in the
    // current working directory.
    l, err := license.NewFromDir(".")
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println(l.Type)

    // This case will do the exact same thing as above, but uses an explicitly
    // set license file name instead of searching for one.
    l, err = license.NewFromFile("./LICENSE")
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println(l.Type)

    // This case will work when the license type can be guessed based on text
    l = new(license.License)
    l.Text = "The MIT License (MIT)"
    if err := l.GuessType(); err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Println(l.Type)

    // This case will work in all cases. The license type and the license data
    // are both being set explicitly. This enables one to use any license.
    l = license.New("MyLicense", "My terms go here")
    fmt.Println(l.Type)

    // This call determines if the license in use is recognized by go-license.
    fmt.Println(l.Recognized())
}

Documentation

Index

Constants

View Source
const (
	// Recognized license types
	LicenseMIT      = "MIT"
	LicenseNewBSD   = "NewBSD"
	LicenseFreeBSD  = "FreeBSD"
	LicenseApache20 = "Apache-2.0"
	LicenseMPL20    = "MPL-2.0"
	LicenseGPL20    = "GPL-2.0"
	LicenseGPL30    = "GPL-3.0"
	LicenseLGPL21   = "LGPL-2.1"
	LicenseLGPL30   = "LGPL-3.0"
	LicenseCDDL10   = "CDDL-1.0"
	LicenseEPL10    = "EPL-1.0"

	// Various errors
	ErrNoLicenseFile       = "license: unable to find any license file"
	ErrUnrecognizedLicense = "license: could not guess license type"
)

Variables

View Source
var DefaultLicenseFiles = []string{
	"LICENSE", "LICENSE.txt", "LICENSE.md", "license.txt",
	"COPYING", "COPYING.txt", "COPYING.md", "copying.txt",
}

A set of reasonable license file names to use when guessing where the license may be.

A slice of standardized license abbreviations

Functions

This section is empty.

Types

type License

type License struct {
	Type string // The type of license in use
	Text string // License text data
	File string // The path to the source file, if any
}

License describes a software license

func New

func New(licenseType, licenseText string) *License

New creates a new License from explicitly passed license type and data

func NewFromDir

func NewFromDir(dir string) (*License, error)

NewFromDir will search a directory for well-known and accepted license file names, and if one is found, read in its content and guess the license type.

func NewFromFile

func NewFromFile(path string) (*License, error)

NewFromFile will attempt to load a license from a file on disk, and guess the type of license based on the bytes read.

func (*License) GuessFile

func (l *License) GuessFile(dir string) error

GuessFile searches a given directory (non-recursively) for files with well- established names that indicate license content.

func (*License) GuessType

func (l *License) GuessType() error

GuessType will scan license text and attempt to guess what license type it describes. It will return the license type on success, or an error if it cannot accurately guess the license type.

This method is a hack. It might be more accurate to also scan the entire body of license text and compare it using an algorithm like Jaro-Winkler or Levenshtein against a generic version. The problem is that some of the common licenses, such as GPL-family licenses, are quite large, and running these algorithms against them is considerably more expensive and is still not completely deterministic on which license is in play. For now, we will just scan until we find differentiating strings and call that good-enuf.gov.

func (*License) Recognized

func (l *License) Recognized() bool

Recognized determines if the license is known to go-license.

Jump to

Keyboard shortcuts

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