1// Copyright © 2015 Steve Francia <spf@spf13.com>.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Parts inspired by https://github.com/ryanuber/go-license
15
16package cmd
17
18import (
19	"strings"
20	"time"
21
22	"github.com/spf13/viper"
23)
24
25//Licenses contains all possible licenses a user can chose from
26var Licenses map[string]License
27
28//License represents a software license agreement, containing the Name of
29// the license, its possible matches (on the command line as given to cobra)
30// the header to be used with each file on the file's creating, and the text
31// of the license
32type License struct {
33	Name            string   // The type of license in use
34	PossibleMatches []string // Similar names to guess
35	Text            string   // License text data
36	Header          string   // License header for source files
37}
38
39func init() {
40	Licenses = make(map[string]License)
41
42	// Allows a user to not use a license.
43	Licenses["none"] = License{"None", []string{"none", "false"}, "", ""}
44
45	// Allows a user to use config for a custom license.
46	Licenses["custom"] = License{"Custom", []string{}, "", ""}
47
48	initApache2()
49	initMit()
50	initBsdClause3()
51	initBsdClause2()
52	initGpl2()
53	initGpl3()
54	initLgpl()
55	initAgpl()
56
57	// Licenses["apache20"] = License{
58	// 	Name:            "Apache 2.0",
59	// 	PossibleMatches: []string{"apache", "apache20", ""},
60	//   Header: `
61	//   `,
62	// 	Text: `
63	//   `,
64	// }
65}
66
67func getLicense() License {
68	l := whichLicense()
69	if l != "" {
70		if x, ok := Licenses[l]; ok {
71			return x
72		}
73	}
74
75	return Licenses["apache"]
76}
77
78func whichLicense() string {
79	// if explicitly flagged, use that
80	if userLicense != "" {
81		return matchLicense(userLicense)
82	}
83
84	// if already present in the project, use that
85	// TODO: Inspect project for existing license
86
87	// default to viper's setting
88
89	if viper.IsSet("license.header") || viper.IsSet("license.text") {
90		if custom, ok := Licenses["custom"]; ok {
91			custom.Header = viper.GetString("license.header")
92			custom.Text = viper.GetString("license.text")
93			Licenses["custom"] = custom
94			return "custom"
95		}
96	}
97
98	return matchLicense(viper.GetString("license"))
99}
100
101func copyrightLine() string {
102	author := viper.GetString("author")
103	year := time.Now().Format("2006")
104
105	return "Copyright © " + year + " " + author
106}
107
108// given a license name (in), try to match the license indicated
109func matchLicense(in string) string {
110	for key, lic := range Licenses {
111		for _, match := range lic.PossibleMatches {
112			if strings.EqualFold(in, match) {
113				return key
114			}
115		}
116	}
117	return ""
118}
119