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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#!/bin/sh
# Constants
PRGN="${0##*/}"
LICENSOR_CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/licensor"
# Configuration
: "${LICENSE_REPO_VERSION:=3.17}"
: "${LICENSE_REPO_URL:=https://github.com/spdx/license-list-data/archive/refs/tags/v$LICENSE_REPO_VERSION.tar.gz}"
: "${LICENSE_REPO_PATH:=license-list-data-$LICENSE_REPO_VERSION/template}"
: "${LICENSOR_LICENSE:=MIT}"
: "${LICENSOR_OUTPUT_FILE:=COPYING}"
usage() {
cat <<EOF
$PRGN: easily provide a license for a project
USAGE: $PRGN -h
$PRGN [-l|-L]
$PRGN -s QUERY
$PRGN [FLAGS] [OPTIONS] [LICENSE]
FLAGS:
-h Display this help and exit.
-l List available licenses and exit.
-L List available licenses, disregarding cache.
This flag will re-download the license repo.
-r Output the raw license file specified by LICENSE.
-f Overwrite existing license.
-q Be quiet (don't log anything).
-z Fold output to $__width characters (see -w).
-Z Disable folding of output.
-p Include "optional" license content.
-P Don't include "optional" content.
OPTIONS:
-s QUERY Search for a license matching QUERY.
Only license titles are searched.
-y YEARS Set copyright date(s) to YEARS.
Default: \$(date +%Y).
-a AUTHORS Set copyright holder(s) to AUTHOR.
Default: use the first of
- git config --get user.name
- getent password \$USER
- \$USER
-e EMAILS Set AUTHOR's EMAIL address.
Default: \$(git config --get user.email),
or stay blank.
-c COPYRIGHT Set the entire COPYRIGHT string to print.
By default, it's built from the above information:
"Copyright (C) <year> <author> <email>".
-w WIDTH Fold the output to WIDTH characters.
Default: $__width.
-o FILE Output the fetched license to FILE.
Default: \$PWD/$LICENSOR_OUTPUT_FILE.
PARAMETERS:
LICENSE The license to use.
Default: $LICENSOR_LICENSE.
EOF
exit ${1:-0}
}
main() {
_force=false
_quiet=false
_fold=1
_optional=0
__width=70
__output="$LICENSOR_OUTPUT_FILE"
__year="$(date +%Y)"
__author="$(guess_author)"
__email="$(guess_email)"
_converter=license_convert
while getopts hlLrs:fzZpPqy:a:e:o:w:c: opt; do
case "$opt" in
# commands
h) usage ;;
l) list_licenses && exit || exit $? ;;
L) list_licenses -f && exit || exit $? ;;
r) _converter=lcat ;;
s) search_licenses "$OPTARG" ;;
# flags
f) _force=true ;;
z) _fold=1 ;;
Z) _fold=0 ;;
p) _optional=1 ;;
P) _optional=0 ;;
q) _quiet=true ;;
# options
y) __year="$OPTARG" ;;
a) __author="$OPTARG" ;;
e) __email="$OPTARG" ;;
o) __output="$OPTARG" ;;
w) __width="$OPTARG" ;;
c) __copyright="$OPTARG" ;;
*) usage 1 ;;
esac
done
shift $((OPTIND - 1))
__license="${1:-$LICENSOR_LICENSE}"
if [ -e "$__output" ] && ! $_force; then
log "File exists: $__output"
exit 3
fi
if [ "x$__output" = x- ]; then
__output=/dev/stdout
fi
if [ -z "$__copyright" ]; then
__copyright="Copyright (C) $__year $__author <$__email>"
fi
license_file="$(get_license "$__license")" || exit $?
$_converter <"$LICENSOR_CACHE/$license_file.template.txt" \
"$__copyright" "$_optional" "$_fold" "$__width" >"$__output"
[ "$__output" != /dev/stdout ] && log "$__license license written to $__output."
}
lcat() {
set --
cat
}
get_licenses() {
# Get licenses from cache, or download them
if ! [ -d "$LICENSOR_CACHE" ] || [ "x$1" = "x-f" ]; then
log "Downlading licenses from $LICENSE_REPO_URL..."
mkdir -p "$LICENSOR_CACHE"
tmpfile="/tmp/licenses.tar.gz"
if ! [ -f "$tmpfile" ]; then
curl -o "$tmpfile" -L "$LICENSE_REPO_URL" >/dev/null 2>&1 ||
return 1
fi
log "Extracting licenses to $LICENSOR_CACHE..."
if tar -C "$LICENSOR_CACHE" \
-xvf "$tmpfile" --strip-components=2 \
"$LICENSE_REPO_PATH" >/dev/null 2>&1; then
rm "$tmpfile"
else
return 1
fi
fi
}
get_license() {
list_licenses | grep -iE '^'"$1"'$' || {
log "Can't find license \"$1\"."
exit 1
}
}
list_licenses() {
get_licenses "$1" || exit 1
find "$LICENSOR_CACHE" -iname '*.template.txt' |
sort |
xargs basename -s .template.txt
}
search_licenses() {
list_licenses | grep -iE "$1"
exit $?
}
guess_author() {
author="$(git config --get user.name)"
if [ -z "$author" ]; then
author="$(getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}')"
fi
if [ -z "$author" ]; then author="$USER"; fi
put "$author"
}
guess_email() {
email="$(git config --get user.email)"
put "$email"
}
put() { printf '%s\n' "$*"; }
log() { $_quiet || put "$PRGN: $*" >&2; }
license_convert() {
copyright="$1"
show_optional="${2:-0}"
fold_output="${3:-1}"
fold_width="${4:-70}"
awk 'BEGIN {
foldOutput = '"$fold_output"'
foldWidth = '"$fold_width"'
showOptional = '"$show_optional"'
FIELDS["copyright"] = "'"$copyright"'"
optional = 0
buffer = ""
begOptRx = showOptional ? "<<beginOptional[^>]*>>" : "<<beginOptional[^>]*>>.*"
endOptRx = showOptional ? "<<endOptional[^>]*>>" : ".*<<endOptional[^>]*>>"
}
/<<var.*>>/ {
match($0, /<<var.*>>/)
split(substr($0, RSTART + 6, RLENGTH - 8), _rule, ";")
for (r in _rule) {
key = substr(_rule[r], 1, index(_rule[r], "=") - 1)
val = substr(_rule[r], index(_rule[r], "=") + 1)
rule[key] = val
}
for (r in rule) {
quoted = match(rule[r], /^".*"$/)
if (quoted) {
rule[r] = substr(rule[r], RSTART + 1, RLENGTH - 2)
}
# print r, rule[r]
}
name = rule["name"]
# print FIELDS[name]
sub(/<<var.*>>/, (FIELDS[name] ? FIELDS[name] : rule["original"]), $0)
}
/<<beginOptional.*>>/ {
optional = 1
if (match($0,/<<beginOptional.*>>.*<<endOptional.*>>/)) {
optional = 0
if (! showOptional) {
sub(/<<beginOptional.*>>.*<<endOptional.*>>/, "", $0)
}
}
sub(begOptRx, "", $0)
}
/<<endOptional.*>>/ {
sub(endOptRx, "", $0)
optional = 0
}
optional && ! showOptional {
next
}
{
bufput()
}
END {
# "Fold" the output
if (foldOutput) {
buffer = fold(buffer, foldWidth)
}
print buffer
}
function bufput(str, sep)
{
buffer = buffer (buffer ? (sep ? sep : "\n") : "") (str ? str : $0)
}
function fold(out, foldWidth)
{
split(out, oa, "\n")
out = ""
for (l in oa) {
split(oa[l], la, FS)
lc = 0
for (w in la) {
lc += length(la[w]) + 1
if (lc >= foldWidth) {
out = out "\n" la[w] " "
lc = length(la[w]) + 1
} else {
out = out la[w] " "
}
}
out = out "\n"
}
return out
}
'
}
main "$@"
|