From 62293ad50a3bbdc13bf0fbd2b5e05748c32618c5 Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Thu, 22 Oct 2020 22:15:59 -0400 Subject: [PATCH] Add license details output --- README.md | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/license.go | 2 +- go.mod | 1 + go.sum | 3 ++ model/License.go | 49 +++++++++++++++++++++++++++++- 5 files changed, 130 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0bf4cde..e3e091f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,83 @@ This project aims to go one step further and include templating and configuratio * `CONTRIBUTING.md` * GitHub Issues and Pull Request templates +## Examples + +### Licenses + +#### Find licenses by keyword + +License keywords + +* copyleft +* discouraged +* international +* miscellaneous +* non-reusable +* obsolete +* osi-approved +* permissive +* popular +* redundant +* retired +* special-purpose + +Example: + +```shell script +ossify license --keyword popular +Apache-2.0 (Apache License, Version 2.0) +BSD-2 (BSD 2-Clause License) +BSD-3 (BSD 3-Clause License) +CDDL-1.0 (Common Development and Distribution License, Version 1.0) +EPL-1.0 (Eclipse Public License, Version 1.0) +GPL-2.0 (GNU General Public License, Version 2.0) +GPL-3.0 (GNU General Public License, Version 3.0) +LGPL-2.1 (GNU Lesser General Public License, Version 2.1) +LGPL-3.0 (GNU Lesser General Public License, Version 3.0) +MIT (MIT/Expat License) +MPL-2.0 (Mozilla Public License, Version 2.0) +``` + +#### Write out license text to file + +```shell script +ossify license MIT > LICENSE +# or, with --id switch. Both are case-insensitive. +ossify license --id mit > LICENSE +``` + +#### Search for specific license text + +```shell script +ossify license --search apache +Apache-1.1 (Apache Software License, Version 1.1) +Apache-2.0 (Apache License, Version 2.0) +``` + +#### View details for specific license + +```shell script +ossify license MIT --details +MIT (MIT/Expat License) +osi-approved, popular, permissive + +Common names + * MIT + * Expat + +License Standards + * DEP5 MIT + * DEP5 Expat + * SPDX MIT + * Trove License :: OSI Approved :: MIT License + +Links + * https://opensource.org/licenses/mit (OSI Page) + * https://tldrlegal.com/license/mit-license (tl;dr legal) + * https://en.wikipedia.org/wiki/MIT_License (Wikipedia page) +``` + ## License This project is [Licensed MIT](./LICENSE) diff --git a/cmd/license.go b/cmd/license.go index fe7e013..ef3aed5 100644 --- a/cmd/license.go +++ b/cmd/license.go @@ -81,7 +81,7 @@ var licenseCmd = &cobra.Command{ details := licenseFlags.details if license != nil { if details { - _ = license.Print() + _ = license.PrintDetails() } else { err := licenses.PrintLicenseText(license.Id, conf.LicensePath) failOnError(err) diff --git a/go.mod b/go.mod index be9e7e2..1837488 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/jimschubert/ossify go 1.14 require ( + github.com/fatih/color v1.7.0 github.com/gobuffalo/envy v1.9.0 // indirect github.com/gobuffalo/packd v1.0.0 // indirect github.com/gobuffalo/packr v1.30.1 diff --git a/go.sum b/go.sum index 6139340..6076f41 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -132,7 +133,9 @@ github.com/lithammer/fuzzysearch v1.1.1 h1:8F9OAV2xPuYblToVohjanztdnPjbtA0MLgMvD github.com/lithammer/fuzzysearch v1.1.1/go.mod h1:H2bng+w5gsR7NlfIJM8ElGZI0sX6C/9uzGqicVXGU6c= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= diff --git a/model/License.go b/model/License.go index 5f87404..d5e20d6 100644 --- a/model/License.go +++ b/model/License.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/fatih/color" "github.com/lithammer/fuzzysearch/fuzzy" ) @@ -23,7 +24,53 @@ type License struct { type Licenses []License func (license License) Print() error { - _, err := fmt.Printf("%-20s(%s)\n", license.Id, license.Name) + d := color.New(color.FgWhite, color.Bold) + _, err := d.Printf("%-20s(%s)\n", license.Id, license.Name) + return err +} + +func (license License) PrintDetails() error { + builder := strings.Builder{} + + bold := color.New(color.FgWhite, color.Bold) + italic := color.New(color.FgWhite, color.Italic) + warn := color.New(color.FgRed, color.Italic) + builder.WriteString(bold.Sprintf("%-20s(%s)\n", license.Id, license.Name)) + if len(license.Keywords) > 0 { + builder.WriteString(italic.Sprint(strings.Join(license.Keywords, ", "))) + builder.WriteString("\n") + } + if license.SupersededBy != nil { + builder.WriteString(warn.Sprintf("This license is superseded by %s\n", *license.SupersededBy)) + } + + if license.OtherNames != nil && len(*license.OtherNames) > 0 { + builder.WriteString(bold.Sprintln("\nCommon names")) + for _, other := range *license.OtherNames { + builder.WriteString(fmt.Sprintf(" * %s\n", other.Name)) + } + } + + if len(license.Identifiers) > 0 { + builder.WriteString(bold.Sprintln("\nLicense Standards")) + for _, identifier := range license.Identifiers { + builder.WriteString(fmt.Sprintf(" * %-10s %s\t\n", identifier.Scheme, identifier.Identifier)) + } + } + + if len(license.Links) > 0 { + builder.WriteString(bold.Sprintln("\nLinks")) + for _, link := range license.Links { + builder.WriteString(" * ") + builder.WriteString(link.Url) + if link.Note != nil { + builder.WriteString(italic.Sprintf(" (%s)", *link.Note)) + } + builder.WriteString("\n") + } + } + + _, err := fmt.Print(builder.String()) return err }