-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (119 loc) · 3.17 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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
flags "github.com/jessevdk/go-flags"
)
type options struct {
GitHubAPIURI string `long:"github-api-uri" default:"https://api.github.com/licenses" default-mask:"-" env:"GITHUB_API_URI" description:"URI for GitHub licenses API, without trailing slash"`
ListFlag bool `short:"l" long:"list" description:"show license name list"`
Username string `short:"u" long:"username" env:"USERNAME" description:"Username to embed to LICENSE file"`
Year string `short:"y" long:"year" default-mask:"current year" description:"License year"`
PosArgs struct {
License string `positional-arg-name:"LICENSE"`
} `positional-args:"true"`
}
type license struct {
Key string `json:"key"`
Name string `json:"name"`
SPDXID string `json:"spdx_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
}
type licenseDetail struct {
Key string `json:"key"`
Name string `json:"name"`
SPDXID string `json:"spdx_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
Description string `json:"description"`
Implementation string `json:"implementation"`
Permissions []string `json:"permissions"`
Conditions []string `json:"conditions"`
Limitations []string `json:"limitations"`
Body string `json:"body"`
}
func main() { os.Exit(exec()) }
func exec() int {
var opts options
if _, err := flags.Parse(&opts); err != nil {
if fe, ok := err.(*flags.Error); ok && fe.Type == flags.ErrHelp {
return 0
}
printErr(err)
return 1
}
if opts.ListFlag {
return showList(opts)
}
return showLicense(opts)
}
func showList(opts options) int {
res, err := http.Get(opts.GitHubAPIURI)
if err != nil {
printErr(err)
return 1
}
if res.StatusCode != http.StatusOK {
apiErr(res.StatusCode, res.Body)
return 1
}
var licenses []license
if err := json.NewDecoder(res.Body).Decode(&licenses); err != nil {
printErr(err)
return 1
}
var names []string
for _, l := range licenses {
names = append(names, l.Key)
}
fmt.Print(strings.Join(names, "\n"))
return 0
}
func showLicense(opts options) int {
if opts.PosArgs.License == "" || opts.Username == "" {
printErr(fmt.Errorf("license and username is required"))
return 1
}
uri := opts.GitHubAPIURI + "/" + strings.ToLower(opts.PosArgs.License)
res, err := http.Get(uri)
if err != nil {
printErr(err)
return 1
}
if res.StatusCode != http.StatusOK {
apiErr(res.StatusCode, res.Body)
return 1
}
var license licenseDetail
if err := json.NewDecoder(res.Body).Decode(&license); err != nil {
printErr(err)
return 1
}
year := opts.Year
if year == "" {
year = strconv.Itoa(time.Now().Year())
}
text := license.Body
text = strings.Replace(text, "[year]", year, -1)
text = strings.Replace(text, "[fullname]", opts.Username, -1)
fmt.Print(text)
return 0
}
func apiErr(code int, body io.Reader) {
b, err := ioutil.ReadAll(body)
if err != nil {
printErr(err)
}
printErr(fmt.Errorf("api error [%d] %s", code, string(b)))
}
func printErr(err error) {
fmt.Fprintf(os.Stderr, "error occured: %s\n", err)
}