-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
172 lines (152 loc) · 3.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"flag"
"log"
"os"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/nishanths/go-hgconfig"
"github.com/tcnksm/go-gitconfig"
)
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)
const (
nameEnv = "LICENSE_FULL_NAME"
usageString = `Usage: license [flags] [license-type]
Flags:
-help print help information
-list print list of available license types
-n, -name full name to use on license (default %q)
-o, -output path to output file (prints to stdout if unspecified)
-v, -version print version
-y, -year year to use on license (default %q)
-p, -project your project to use on license (default %q)
Examples:
license mit
license -name "Alice L" -year 2013 -project "myapp" bsd-4-clause
license -o LICENSE.txt mpl-2.0`
)
var (
stdout = log.New(os.Stdout, "", 0)
stderr = log.New(os.Stderr, "", 0)
)
var (
fName string
fYear string
fProject string
fOutput string
fVersion bool
fHelp bool
fList bool
)
func main() {
flag.StringVar(&fName, "name", "", "name on license")
flag.StringVar(&fName, "n", "", "name on license")
flag.StringVar(&fYear, "year", "", "year on license")
flag.StringVar(&fYear, "y", "", "year on license")
flag.StringVar(&fProject, "project", "", "project name on license")
flag.StringVar(&fProject, "p", "", "project name on license")
flag.StringVar(&fOutput, "output", "", "path to output file")
flag.StringVar(&fOutput, "o", "", "path to output file")
flag.BoolVar(&fVersion, "version", false, "print version")
flag.BoolVar(&fVersion, "v", false, "print version")
flag.BoolVar(&fHelp, "help", false, "print help")
flag.BoolVar(&fList, "list", false, "print available licenses")
flag.Usage = func() {
printUsage()
os.Exit(1)
}
flag.Parse()
run()
}
func run() {
if flag.NArg() != 1 && !(fVersion || fHelp || fList) {
printUsage()
os.Exit(1)
}
switch {
case fVersion:
printVersion()
os.Exit(0)
case fHelp:
printUsage()
os.Exit(0)
case fList:
printList()
os.Exit(0)
default:
license := strings.ToLower(flag.Arg(0))
printLicense(license, fOutput, getName(), getYear(), getProject()) // internally calls os.Exit() on failure
}
}
func printVersion() {
stdout.Printf("license version %s", version)
if commit != "" {
stdout.Printf("commit: %s", commit)
}
if date != "" {
stdout.Printf("built at: %s", date)
}
if builtBy != "" {
stdout.Printf("built by: %s", builtBy)
}
stdout.Printf("goos: %s", runtime.GOOS)
stdout.Printf("goarch: %s", runtime.GOARCH)
}
func printUsage() {
stderr.Printf(usageString, getName(), getYear(), getProject())
}
func getName() string {
if fName != "" {
return fName
}
n := os.Getenv(nameEnv)
if n != "" {
return n
}
n, err := gitconfig.Username()
if err == nil {
return n
}
n, err = gitconfig.Global("user.name")
if err == nil {
return n
}
n, err = hgconfig.Username()
if err == nil {
return n
}
usr, err := user.Current()
if err == nil {
return usr.Name
}
return ""
}
func getYear() string {
if fYear != "" {
return fYear
}
return strconv.Itoa(time.Now().Year())
}
func getProject() string {
if fProject != "" {
return fProject
}
dir, err := os.Getwd()
if err != nil {
return ""
}
rootDir, err := filepath.Abs(dir)
if err != nil {
return ""
}
return filepath.Base(rootDir)
}