diff options
Diffstat (limited to 'licenser')
-rwxr-xr-x | licenser | 181 |
1 files changed, 0 insertions, 181 deletions
diff --git a/licenser b/licenser deleted file mode 100755 index 2337470..0000000 --- a/licenser +++ /dev/null | |||
@@ -1,181 +0,0 @@ | |||
1 | #!/bin/sh | ||
2 | # licenser: drop a little ol license in there | ||
3 | # Author: Case Duckworth <acdw@acdw.net> | ||
4 | # License: MIT | ||
5 | # Version: 0.2 | ||
6 | |||
7 | # Constants | ||
8 | ## where to download licenses from | ||
9 | PRGN="${0##*/}" | ||
10 | : "${LICENSER_SOURCE:=https://git.sr.ht/~acdw/licenser-licenses/blob/master}" | ||
11 | LICENSER_CACHE="${XDG_DATA_HOME:-$HOME/.local/share}/licenser" | ||
12 | LICENSER_LICENSE="${LICENSER_LICENSE:-MIT}" | ||
13 | EXEC="${EXEC:-true}" | ||
14 | |||
15 | usage() { | ||
16 | cat <<EOF | ||
17 | $PRGN: plop a license in your project | ||
18 | |||
19 | usage: $PRGN [-h|-m|-M] | ||
20 | $PRGN [-f] [-q] [-y YEAR] [-a AUTHOR] [-e EMAIL] | ||
21 | [-l LANG] [-o FILE] [LICENSE] | ||
22 | flags: | ||
23 | -h show this help and exit | ||
24 | -m list available licenses and exit | ||
25 | -M list licenses, disregarding cached manifest | ||
26 | -f overwrite existing license | ||
27 | -q quiet: don't log anything | ||
28 | options: | ||
29 | -y YEAR set copyright date(s) to YEAR. | ||
30 | default: \$(date +%Y). | ||
31 | this option accepts arbitrary input. | ||
32 | -a AUTHOR set copyright holder(s) to AUTHOR. | ||
33 | default: | ||
34 | - git config --get user.name | ||
35 | - getent password \$USER | ||
36 | - \$USER | ||
37 | -e EMAIL set AUTHOR's email address to EMAIL. | ||
38 | default: | ||
39 | - git config --get user.email | ||
40 | - [blank] | ||
41 | -l LANG set license language to LANG. | ||
42 | default: 'en' (subject to change) | ||
43 | -o FILE set output file to FILE. | ||
44 | default: LICENSE in \$PWD | ||
45 | parameters: | ||
46 | LICENSE which license to use. | ||
47 | default: \$LICENSER_LICENSE, which | ||
48 | defaults to MIT. | ||
49 | EOF | ||
50 | } | ||
51 | |||
52 | # entry point | ||
53 | licenser() { | ||
54 | _force=false # don't replace licenses | ||
55 | _quiet=false # log stuff | ||
56 | __OUT=LICENSE # default output file | ||
57 | __LANG=en # default to en because it's got the most licenses XXX | ||
58 | |||
59 | while getopts hmMfqy:a:e:o:l:s: OPT; do | ||
60 | case "$OPT" in | ||
61 | h) | ||
62 | usage | ||
63 | exit 0 | ||
64 | ;; | ||
65 | f) _force=true ;; | ||
66 | m) | ||
67 | list_licenses | ||
68 | exit "$?" | ||
69 | ;; | ||
70 | M) | ||
71 | list_licenses -f | ||
72 | exit "$?" | ||
73 | ;; | ||
74 | q) _quiet=true ;; | ||
75 | y) __YEAR="$OPTARG" ;; | ||
76 | a) __AUTHOR="$OPTARG" ;; | ||
77 | e) __EMAIL="$OPTARG" ;; | ||
78 | o) __OUT="$OPTARG" ;; | ||
79 | l) __LANG="$OPTARG" ;; | ||
80 | *) | ||
81 | usage | ||
82 | exit 1 | ||
83 | ;; | ||
84 | esac | ||
85 | done | ||
86 | shift $((OPTIND - 1)) | ||
87 | __LICENSE="${1:-$LICENSER_LICENSE}" | ||
88 | |||
89 | if ! __CACHED=$(get_license "$__LICENSE"); then | ||
90 | list_licenses | ||
91 | exit 2 | ||
92 | fi | ||
93 | |||
94 | if [ -e "$__OUT" ] && ! $_force; then | ||
95 | log "File exists: $__OUT" | ||
96 | exit 3 | ||
97 | fi | ||
98 | |||
99 | eval "$( | ||
100 | put "cat<<_END_LICENSE_" | ||
101 | cat "$__CACHED" | ||
102 | put | ||
103 | put "_END_LICENSE_" | ||
104 | )" >"$__OUT" | ||
105 | |||
106 | log "$__LICENSE written to $__OUT." | ||
107 | } | ||
108 | |||
109 | # download a license, or get it from the cache | ||
110 | # if there's not one, return error | ||
111 | get_license() { | ||
112 | license="$1" | ||
113 | cached="$LICENSER_CACHE/$license/$__LANG" | ||
114 | source="$LICENSER_SOURCE/$license/$__LANG" | ||
115 | |||
116 | mkdir -p "$LICENSER_CACHE/$license" | ||
117 | log "Checking cache for $license..." | ||
118 | if [ ! -f "$cached" ]; then | ||
119 | log "Not found. Downloading from $source..." | ||
120 | if ! curl -fLo "$cached" "$source"; then | ||
121 | log "License $license unavailable in language: $__LANG" | ||
122 | return 1 | ||
123 | fi | ||
124 | fi | ||
125 | put "$cached" | ||
126 | } | ||
127 | |||
128 | # download a list of the licenses, or get it from the cache | ||
129 | list_licenses() { | ||
130 | cached="$LICENSER_CACHE/manifest" | ||
131 | source="$LICENSER_SOURCE/manifest" | ||
132 | |||
133 | [ "x$1" = "x-r" ] && rm "$cached" | ||
134 | |||
135 | mkdir -p "$LICENSER_CACHE" | ||
136 | log "Checking cache for manifest..." | ||
137 | if [ ! -f "$cached" ]; then | ||
138 | log "Not found. Downloading from $source..." | ||
139 | if ! curl -fLo "$cached" "$LICENSER_SOURCE/manifest"; then | ||
140 | log "Unable to get the manifest" | ||
141 | return 1 | ||
142 | fi | ||
143 | fi | ||
144 | cat "$cached" | ||
145 | } | ||
146 | |||
147 | # template function for the copyright year | ||
148 | year() { | ||
149 | case "$__YEAR" in | ||
150 | '') # figure out the year | ||
151 | date +%Y ;; | ||
152 | *) # one was provided | ||
153 | put "$__YEAR" ;; | ||
154 | esac | ||
155 | } | ||
156 | |||
157 | # template function for author/owner | ||
158 | author() { | ||
159 | if [ -z "$__AUTHOR" ]; then | ||
160 | __AUTHOR="$(git config --get user.name)" # try from git | ||
161 | [ -z "$__AUTHOR" ] && __AUTHOR="$( | ||
162 | getent passwd "$USER" | awk -F: '{sub(/,+/,"",$5);print $5}' | ||
163 | )" # or, try from getent | ||
164 | [ -z "$__AUTHOR" ] && __AUTHOR="$USER" # give up, get $USER | ||
165 | fi | ||
166 | put "$__AUTHOR" | ||
167 | } | ||
168 | |||
169 | email() { | ||
170 | if [ -z "$__EMAIL" ]; then | ||
171 | __EMAIL="$(git config --get user.email)" # try from git | ||
172 | fi | ||
173 | [ -n "$__EMAIL" ] && put "<$__EMAIL>" | ||
174 | } | ||
175 | |||
176 | # helpers | ||
177 | put() { printf '%s\n' "$*"; } | ||
178 | log() { $_quiet || put "$PRGN: $*" >&2; } | ||
179 | |||
180 | # run | ||
181 | $EXEC && licenser "$@" | ||