2015-10-28 16:51:48 +00:00
|
|
|
// Copyright © 2015 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Parts inspired by https://github.com/ryanuber/go-license
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2017-04-20 18:28:16 +00:00
|
|
|
import (
|
2017-04-29 10:02:02 +00:00
|
|
|
"fmt"
|
2017-04-20 18:28:16 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2015-11-23 09:39:02 +00:00
|
|
|
//Licenses contains all possible licenses a user can chose from
|
2015-10-28 16:51:48 +00:00
|
|
|
var Licenses map[string]License
|
|
|
|
|
2015-11-23 09:39:02 +00:00
|
|
|
//License represents a software license agreement, containing the Name of
|
|
|
|
// the license, its possible matches (on the command line as given to cobra)
|
|
|
|
// the header to be used with each file on the file's creating, and the text
|
|
|
|
// of the license
|
2015-10-28 16:51:48 +00:00
|
|
|
type License struct {
|
|
|
|
Name string // The type of license in use
|
|
|
|
PossibleMatches []string // Similar names to guess
|
|
|
|
Text string // License text data
|
|
|
|
Header string // License header for source files
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Licenses = make(map[string]License)
|
|
|
|
|
2016-03-31 18:31:03 +00:00
|
|
|
// Allows a user to not use a license.
|
|
|
|
Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
|
|
|
|
|
2017-04-20 15:34:10 +00:00
|
|
|
initApache2()
|
|
|
|
initMit()
|
|
|
|
initBsdClause3()
|
|
|
|
initBsdClause2()
|
|
|
|
initGpl2()
|
|
|
|
initGpl3()
|
2017-04-20 18:46:09 +00:00
|
|
|
initLgpl()
|
2017-04-20 18:48:21 +00:00
|
|
|
initAgpl()
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
2017-04-20 18:28:16 +00:00
|
|
|
|
2017-04-29 10:02:02 +00:00
|
|
|
// TODO: Inspect project for existing license
|
2017-04-20 18:28:16 +00:00
|
|
|
func getLicense() License {
|
2017-04-29 10:02:02 +00:00
|
|
|
// If explicitly flagged, use that.
|
2017-04-20 18:28:16 +00:00
|
|
|
if userLicense != "" {
|
2017-04-29 10:02:02 +00:00
|
|
|
return findLicense(userLicense)
|
2017-04-20 18:28:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 10:02:02 +00:00
|
|
|
// If user wants to have custom license, use that.
|
2017-04-20 18:28:16 +00:00
|
|
|
if viper.IsSet("license.header") || viper.IsSet("license.text") {
|
2017-04-29 10:02:02 +00:00
|
|
|
return License{Header: viper.GetString("license.header"),
|
|
|
|
Text: "license.text"}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user wants to have built-in license, use that.
|
|
|
|
if viper.IsSet("license") {
|
|
|
|
return findLicense(viper.GetString("license"))
|
2017-04-20 18:28:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 10:02:02 +00:00
|
|
|
// If user didn't set any license, use Apache 2.0 by default.
|
|
|
|
fmt.Println("apache")
|
|
|
|
return Licenses["apache"]
|
2017-04-20 18:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyrightLine() string {
|
|
|
|
author := viper.GetString("author")
|
|
|
|
year := time.Now().Format("2006")
|
|
|
|
|
|
|
|
return "Copyright © " + year + " " + author
|
|
|
|
}
|
|
|
|
|
2017-04-29 10:02:02 +00:00
|
|
|
func findLicense(name string) License {
|
|
|
|
found := matchLicense(name)
|
|
|
|
if found == "" {
|
|
|
|
er(fmt.Errorf("unknown license %q", name))
|
|
|
|
}
|
|
|
|
return Licenses[found]
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a license name, try to match the license indicated
|
|
|
|
func matchLicense(name string) string {
|
|
|
|
if name == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:28:16 +00:00
|
|
|
for key, lic := range Licenses {
|
|
|
|
for _, match := range lic.PossibleMatches {
|
2017-04-29 10:02:02 +00:00
|
|
|
if strings.EqualFold(name, match) {
|
2017-04-20 18:28:16 +00:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-29 10:02:02 +00:00
|
|
|
|
2017-04-20 18:28:16 +00:00
|
|
|
return ""
|
|
|
|
}
|