You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
3.6 KiB
Bash
182 lines
3.6 KiB
Bash
# chezmoi:template:left-delimiter=#{{
|
|
#{{- /* vim: set filetype=sh: */ -}}
|
|
#shellcheck shell=sh
|
|
|
|
source_env_file() {
|
|
if [ ! -f "${1}" ]; then
|
|
return
|
|
fi
|
|
eval "$(
|
|
grep -v '^\s*\#' "${1}" |
|
|
grep -v '^\s*$' |
|
|
sed 's/^\s*export//g' |
|
|
sed 's/^/export /g'
|
|
)"
|
|
}
|
|
|
|
make_scripts() (
|
|
for FILE in "$@"; do
|
|
make_script "${FILE}"
|
|
done
|
|
)
|
|
|
|
make_script() (
|
|
cat <<-EOF > "${1}"
|
|
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
main() (
|
|
echo "todo"
|
|
)
|
|
|
|
main "$@"
|
|
EOF
|
|
chmod +x "${1}"
|
|
)
|
|
|
|
add_date_prefix() (
|
|
DIR=$(dirname "$1")
|
|
FILE=$(basename "$1")
|
|
DATE=$(date -r "$1" +"%F")
|
|
if [ ! -f "$1" ]; then
|
|
echo "unknown file: $1"
|
|
exit 1
|
|
fi
|
|
mv "$1" "$DIR/${DATE}_${FILE}"
|
|
)
|
|
|
|
get_bitrate() (
|
|
if [ ! -f "$1" ]; then
|
|
echo "[ERROR] unknown file: $1"
|
|
exit 1
|
|
fi
|
|
command_installed exiftool
|
|
exiftool -AudioBitrate "$1" | awk '{print $4}'
|
|
)
|
|
|
|
json_to_yaml() (
|
|
ARG=${1-}
|
|
command_installed bat dasel
|
|
if [ -z "${ARG}" ]; then
|
|
dasel --read json --write yaml | bat --language yaml
|
|
elif [ -f "${ARG}" ]; then
|
|
dasel --read json --write yaml --file "${ARG}" | bat --language yaml
|
|
else
|
|
echo "${ARG}" | dasel --read json --write yaml | bat --language yaml
|
|
fi
|
|
)
|
|
|
|
yaml_to_json() (
|
|
ARG=${1-}
|
|
command_installed bat dasel
|
|
if [ -z "${ARG}" ]; then
|
|
dasel --read yaml --write json | bat --language json
|
|
elif [ -f "${ARG}" ]; then
|
|
dasel --read yaml --write json --file "${ARG}" | bat --language json
|
|
else
|
|
echo "${ARG}" | dasel --read yaml --write json | bat --language json
|
|
fi
|
|
)
|
|
|
|
#{{ if lookPath "rg" -}}
|
|
github_install_scripts () (
|
|
rg github.com "${HOME}/.local/bin" \
|
|
| sed 's/:/ /' \
|
|
| sed 's/=/ /g' \
|
|
| awk '{print $3}' \
|
|
| sed 's|https://github.com/||g' \
|
|
| sed 's|/| |g' \
|
|
| awk '{print $1 " " $2}' \
|
|
| sed 's| |/|g' \
|
|
| sort \
|
|
| uniq
|
|
)
|
|
|
|
github_install_scripts_latest_releases() (
|
|
for REPO in $(github_install_scripts); do
|
|
echo "${REPO} $(github_latest_release "${REPO}")"
|
|
done
|
|
)
|
|
#{{- end }}
|
|
|
|
github_latest_release() (
|
|
github_tags "$@" | head -n 1
|
|
)
|
|
|
|
github_releases() (
|
|
USER=${1}
|
|
REPO=${2-}
|
|
command_installed curl
|
|
if [ -z "${REPO}" ]; then
|
|
USER=$(echo "${1}" | sed 's|/| |g' | awk '{print $1}')
|
|
REPO=$(echo "${1}" | sed 's|/| |g' | awk '{print $2}')
|
|
fi
|
|
curl --silent --header "Accept: application/vnd.github.v3+json" \
|
|
"https://api.github.com/repos/${USER}/${REPO}/releases"
|
|
)
|
|
|
|
github_tags() (
|
|
USER=${1}
|
|
REPO=${2-}
|
|
command_installed curl jq
|
|
if [ -z "${REPO}" ]; then
|
|
USER=$(echo "${1}" | sed 's|/| |g' | awk '{print $1}')
|
|
REPO=$(echo "${1}" | sed 's|/| |g' | awk '{print $2}')
|
|
fi
|
|
curl --silent --header "Accept: application/vnd.github.v3+json" \
|
|
"https://api.github.com/repos/${USER}/${REPO}/tags" |
|
|
jq -r '.[].name'
|
|
)
|
|
|
|
command_installed() (
|
|
set -ue
|
|
for cmd in "${@}"; do
|
|
if ! command -v "${cmd}" 1>&2 >/dev/null; then
|
|
echo "${cmd} not installed" 1>&2
|
|
exit 1
|
|
fi
|
|
done
|
|
)
|
|
|
|
min_jpg() (
|
|
set -ue
|
|
command_installed jpegtran
|
|
for FILE in "$@" ; do
|
|
PERMISSIONS=$(stat --format '%a' "${FILE}")
|
|
OUT_FILE=$(mktemp)
|
|
jpegtran -optimize -perfect -outfile "${OUT_FILE}" "${FILE}"
|
|
mv "${OUT_FILE}" "${FILE}"
|
|
chmod "${PERMISSIONS}" "${FILE}"
|
|
done
|
|
)
|
|
|
|
min_png() (
|
|
set -ue
|
|
command_installed pngcrush
|
|
for FILE in "$@" ; do
|
|
PERMISSIONS=$(stat --format '%a' "${FILE}")
|
|
OUT_FILE=$(mktemp)
|
|
pngcrush -rem alla -reduce -brute "${FILE}" "${OUT_FILE}"
|
|
mv "${OUT_FILE}" "${FILE}"
|
|
chmod "${PERMISSIONS}" "${FILE}"
|
|
done
|
|
)
|
|
|
|
get_create_date() (
|
|
date -r "$1" +"%F"
|
|
)
|
|
|
|
#shellcheck disable=SC2120
|
|
slugify() (
|
|
if [ "$#" -ne 0 ]; then
|
|
#shellcheck disable=SC2119
|
|
echo "$@" | slugify
|
|
else
|
|
iconv --to-code ascii//TRANSLIT |
|
|
sed -E 's/[^a-zA-Z0-9]+/-/g' |
|
|
sed -E 's/^-+\|-+$//g' |
|
|
tr '[:upper:]' '[:lower:]'
|
|
fi
|
|
)
|