Skip to content

Instantly share code, notes, and snippets.

@dcm
Last active August 6, 2022 04:00
Show Gist options
  • Save dcm/a1268ce1f8b2c415348b1658591a430b to your computer and use it in GitHub Desktop.
Save dcm/a1268ce1f8b2c415348b1658591a430b to your computer and use it in GitHub Desktop.
Git alias to pull a license's text by name
#!/usr/bin/env bash
DBG="${DBG:-0}";
PRNT_LABEL=0;
URL="https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json";
function run_prechecks() {
# is jq installed
if ! type jq &>/dev/null; then
echo "Download 'jq'";
echo return 1;
fi;
}
function get_license_list() {
curl -s "${URL}" \
| jq -r ".licenses | map(.licenseId) | .[]" \
| sort -f;
}
function dbg() {
if [ ${DBG:-0} -eq 1 ]; then
echo >&2 "$@";
fi
}
if [ $# -eq 0 ] || [[ "${1}" == "list" ]]; then # MAIN_CONDITIONAL
get_license_list;
exit 0;
elif [ $# -gt 1 ]; then # MAIN_CONDITIONAL
PRNT_LABEL=1;
else # MAIN_CONDITIONAL
for lname in $@; do # MAIN_PARSEARGS
lname_lower="$(echo ${lname} | tr '[:upper:]' '[:lower:]')";
dbg "lname_lower=$lname_lower";
JQ_ARGS="$(
cat <<EOF | tr '\n' ' '
.licenses[]
| select(
.licenseId
| ascii_downcase == "${lname_lower}"
)
| [ .licenseId, .name, .detailsUrl ]
| join("|")
EOF
)";
dbg "JQ_ARGS=$JQ_ARGS";
LID_LNOM_LDURL=$(curl -s "${URL}" | jq -r "$JQ_ARGS");
dbg "LID_LNOM_LDURL=$LID_LNOM_LDURL";
if [ -z "${LID_LNOM_LDURL}" ]; then
printf >&2 "!!! %s !!!\n\n" "No license named '${lname}'";
I=0
get_license_list | grep -i "${lname}" \
| while read -r ln; do
if [ $I -eq 0 ]; then
I=1;
echo >&2 "Did you mean one of these?";
fi;
printf "> %s\n" "$ln";
done;
else
LID="$( cut -d'|' -f1 <<< "${LID_LNOM_LDURL}")";
LNOM="$( cut -d'|' -f2 <<< "${LID_LNOM_LDURL}")";
LDURL="$(cut -d'|' -f3 <<< "${LID_LNOM_LDURL}")";
if [ $PRNT_LABEL -eq 1 ]; then
printf "# %s (%s)\n# %s\n\n" "${LNOM}" "${LID}" "${LDURL}";
fi;
curl -s "${LDURL}" \
| jq -r ".licenseText" \
| sed "s/<year>/$(date +%Y)/" \
| fold -sw80;
echo -e >&2 '\n!!! Be sure to replace all instances of "year" and "copyright holders" with the appropriate values.';
fi;
done; # MAIN_PARSEARGS
fi; # MAIN_CONDITIONAL
## Add this in ~/.gitconfig under the [alias] section
##
## Usage:
# $ git license mit 0bsd poop # print labeled license text only (case-insensitive)
#
# # MIT License (MIT)
# # https://spdx.org/licenses/MIT.json
#
# MIT License
#
# Copyright (c) <year> <copyright holders>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# # BSD Zero Clause License (0BSD)
# # https://spdx.org/licenses/0BSD.json
#
# Copyright (C) YEAR by AUTHOR EMAIL
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
# #!# No license named 'poop' #!#
#
# $ git license 0bsd # print the license text only (case-insensitive)
#
# Copyright (C) YEAR by AUTHOR EMAIL
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
# $ git license # print all available license SPDX codes
#
# 0BSD
# AAL
# Abstyles
# Adobe-2006
# ...
# zlib-acknowledgement
# ZPL-1.1
# ZPL-2.0
# ZPL-2.1
## Uncomment this if you don't have an [alias] section yet:
#[alias]
license = "\
!gl() { \
if ! type jq &>/dev/null; then \
echo \"Download 'jq'\"; \
echo return 1; \
fi; \
local URL=\"https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json\"; \
if [ $# -eq 0 ] || [[ \"${1}\" == \"list\" ]]; then \
curl -s \"${URL}\" \
| jq -r \".licenses | map(.licenseId) | .[]\" \
| sort -f; \
return 0; \
fi; \
local PRNT_LABEL=0; \
if [ $# -gt 1 ]; then PRNT_LABEL=1; fi; \
for lname in $@; do \
local lname_lower=\"$(echo ${lname} | tr '[:upper:]' '[:lower:]')\"; \
local LID_LNOM_LDURL=$( \
curl -s \"${URL}\" | jq -r \" \
.licenses[] \
| select( \
.licenseId | ascii_downcase == \\\"${lname_lower}\\\" \
) \
| [ .licenseId, .name, .detailsUrl ] \
| join(\\\"|\\\") \
\" \
); \
if [ -z \"${LID_LNOM_LDURL}\" ]; then \
printf \"#!# %s #!#\n\n\" \"No license named '${lname}'\"; \
else \
local LID=\"$( cut -d'|' -f1 <<< \"${LID_LNOM_LDURL}\")\"; \
local LNOM=\"$( cut -d'|' -f2 <<< \"${LID_LNOM_LDURL}\")\"; \
local LDURL=\"$(cut -d'|' -f3 <<< \"${LID_LNOM_LDURL}\")\"; \
if [ $PRNT_LABEL -eq 1 ]; then \
printf \"# %s (%s)\n# %s\n\n\" \"${LNOM}\" \"${LID}\" \"${LDURL}\"; \
fi; \
curl -s \"${LDURL}\" | jq -r \".licenseText\" | fold -sw80; \
fi; \
done; \
}; \
gl \
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment