Compare commits

...

6 Commits

Author SHA1 Message Date
AJ ONeal
b3672e5412 ref: move shell integrations to their own functions 2023-11-21 13:31:36 -07:00
AJ ONeal
cb272101b3 ref: move 'webi --info' to its own function 2023-11-21 13:31:36 -07:00
AJ ONeal
68ec49b8e8 feat(completions): add --<option>s to the list 2023-11-21 13:31:36 -07:00
AJ ONeal
02bbecc561 feat(webi): save install options for shell completions 2023-11-21 13:31:35 -07:00
AJ ONeal
50f982f6e3 fix(zsh): must 'compinit' before 'compdef' 2023-11-21 19:53:21 +00:00
RubenRam
f2d7ef229e feat(webi): add webi init option
Add shell package completion for bash, zsh and fish
2023-11-21 19:53:19 +00:00

View File

@@ -188,6 +188,8 @@ __webi_main() {
echo " Output the version number of webi and exit."
echo ""
echo " Helper Utilities"
echo " --init Register command line completions with shell"
echo ""
echo " --list Show everything webi has to offer."
echo ""
echo " --info <package>"
@@ -212,71 +214,18 @@ __webi_main() {
exit 0
fi
if echo "$1" | grep -q -E '^(-l|--list|list)$'; then
echo >&2 "[warn] the format of --list output may change"
# because we don't have sitemap.xml for dev sites yet
my_host="https://webinstall.dev"
my_len="${#my_host}"
# 6 because the field will looks like "loc>WEBI_HOST/PKG_NAME"
# and the count is 1-indexed
my_count="$((my_len + 6))"
curl -fsS "${my_host}/sitemap.xml" |
grep -F "${my_host}" |
cut -d'<' -f2 |
cut -c "${my_count}"-
if echo "$1" | grep -q -E '^(--list|list)$'; then
webi_list
exit 0
fi
if echo "${1}" | grep -q -E '^(--info|info)$'; then
if test -z "${2}"; then
echo >&2 "Usage: webi --info <package>"
exit 1
fi
echo >&2 "[warn] the output of --info is completely half-baked and will change"
my_pkg="${2}"
# TODO need a way to check that it exists at all (readme, win, lin)
echo ""
echo " Cheat Sheet: ${WEBI_HOST}/${my_pkg}"
echo " POSIX: curl -sS ${WEBI_HOST}/${my_pkg} | sh"
echo " Windows: curl.exe -A MS ${WEBI_HOST}/${my_pkg} | powershell"
echo "Releases (JSON): ${WEBI_HOST}/api/releases/${my_pkg}.json"
echo " Releases (tsv): ${WEBI_HOST}/api/releases/${my_pkg}.tab"
echo " (query params): ?channel=stable&limit=10"
echo " &os=${my_os}&arch=${my_arch}"
echo " Install Script: ${WEBI_HOST}/api/installers/${my_pkg}.sh?formats=tar,zip,xz,git,dmg,pkg"
echo " Static Assets: ${WEBI_HOST}/packages/${my_pkg}/README.md"
echo ""
# TODO os=linux,macos,windows (limit to tagged releases)
my_releases="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?channel=stable&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases}" | grep -q "error"; then
my_releases_beta="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases_beta}" | grep -q "error"; then
echo >&2 "'${my_pkg}' is a special case that does not have releases"
else
echo >&2 "ERROR no stable releases for '${my_pkg}'!"
fi
exit 0
fi
echo >&2 "Stable '${my_pkg}' releases:"
if command -v jq > /dev/null; then
printf '%s\n' "${my_releases}" |
jq
else
printf '%s\n' "${my_releases}"
fi
webi_info "$@"
exit 0
fi
if echo "$1" | grep -q -E '^(--init|init)$'; then
webi_shell_init "$@"
exit 0
fi
@@ -289,4 +238,271 @@ __webi_main() {
}
webi_shell_init() { (
a_shell="${2:-}"
fn_shell_integrate_bash ""
fn_shell_integrate_zsh ""
fn_shell_integrate_fish ""
# update completions now
webi_list > /dev/null
if [ $# -eq 1 ]; then
exit 0
fi
case "${a_shell}" in
bash)
fn_shell_integrate_bash "force"
fn_shell_init_bash
;;
zsh)
fn_shell_integrate_zsh "force"
fn_shell_init_zsh
;;
fish)
fn_shell_integrate_fish "force"
fn_shell_init_fish
;;
*)
echo >&2 "Unsupported shell: $2"
exit 1
;;
esac
) }
fn_shell_integrate_bash() { (
a_force="${1}"
if test -z "${a_force}"; then
if ! command -v bash > /dev/null; then
return 0
fi
if ! test -e ~/.bashrc && ! test -e ~/.bash_history; then
return 0
fi
fi
touch -a ~/.bashrc
if grep -q 'webi --init' ~/.bashrc; then
return 0
fi
echo >&2 " Edit ~/.bashrc to add 'eval \"\$(webi --init bash)\"'"
# shellcheck disable=SC2016
{
echo ''
echo '# Generated by Webi. Do not edit.'
echo 'eval "$(webi --init bash)"'
} >> ~/.bashrc
); }
# shellcheck disable=SC2016
fn_shell_init_bash() { (
echo '_webi() {'
echo ' COMPREPLY=()'
echo ' local cur="${COMP_WORDS[COMP_CWORD]}"'
echo ' if [ "$COMP_CWORD" -eq 1 ]; then'
echo ' local completions=$(webi --list | cut -d" " -f1)'
echo ' COMPREPLY=( $(compgen -W "$completions" -- "$cur") )'
echo ' fi'
echo '}'
echo ''
echo 'complete -F _webi webi'
); }
fn_shell_integrate_zsh() { (
a_force="${1}"
if test -z "${a_force}"; then
if ! command -v zsh > /dev/null; then
return 0
fi
if ! test -e ~/.zshrc &&
! test -e ~/.zsh_sessions &&
! test -e ~/.zsh_history; then
return 0
fi
fi
touch -a ~/.zshrc
if grep -q 'webi --init' ~/.zshrc; then
return 0
fi
echo >&2 " Edit ~/.zshrc to add 'eval \"\$(webi --init zsh)\"'"
# shellcheck disable=SC2016
{
echo ''
echo '# Generated by Webi. Do not edit.'
echo 'eval "$(webi --init zsh)"'
} >> ~/.zshrc
); }
# shellcheck disable=SC2016
fn_shell_init_zsh() { (
echo '_webi() {'
echo ' local -a list completions'
echo ' list=$(webi --list | cut -d" " -f1)'
echo ' completions=(${(f)list})'
echo ' _describe -t commands "command" completions && ret=0'
echo '}'
echo ''
echo 'autoload -Uz compinit && compinit'
echo 'compdef _webi webi'
); }
fn_shell_integrate_fish() { (
a_force="${1}"
if test -z "${a_force}"; then
if ! command -v fish > /dev/null; then
return 0
fi
fi
mkdir -p ~/.config/fish
touch -a ~/.config/fish/config.fish
if grep -q 'webi --init' ~/.config/fish/config.fish; then
return 0
fi
echo >&2 " Edit ~/.config/fish/config.fish to add 'webi --init fish | source'"
# shellcheck disable=SC2016
{
echo ''
echo '# Generated by Webi. Do not edit.'
echo 'webi --init fish | source'
} >> ~/.config/fish/config.fish
); }
# shellcheck disable=SC2016
fn_shell_init_fish() { (
echo 'function __fish_webi_needs_command'
echo ' set cmd (commandline -opc)'
echo ' if [ (count $cmd) -eq 1 -a $cmd[1] = "webi" ]'
echo ' return 0'
echo ' end'
echo ' return 1'
echo 'end'
echo ''
echo 'set completions (webi --list | cut -d" " -f1)'
echo 'complete -f -c webi -n __fish_webi_needs_command -a "$completions"'
); }
webi_list() { (
# make sure there's always a cache dir and timestamp file
mkdir -p ~/.local/share/webi/var/
if ! test -r ~/.local/share/webi/var/list.txt; then
echo '0' > ~/.local/share/webi/var/last_update
elif ! test -r ~/.local/share/webi/var/last_update; then
echo '0' > ~/.local/share/webi/var/last_update
fi
# compare the timestamp in the timestamp file to now
# (in seconds since unix epoch)
my_stale_age=600
my_expire_age=900
my_now="$(date -u '+%s')"
my_then="$(cat ~/.local/share/webi/var/last_update)"
my_diff=$((my_now - my_then))
# show when the cache will update
my_stales_in=$((my_stale_age - my_diff))
my_expires_in=$((my_expire_age - my_diff))
# update if it's been longer than the staletime
if test "${my_stales_in}" -lt "0"; then
if test "${my_expires_in}" -lt "0"; then
fn_list_uncached
else
fn_list_uncached &
fi
fi
# give back the list
cat ~/.local/share/webi/var/list.txt
); }
fn_list_uncached() { (
# because we don't have sitemap.xml for dev sites yet
my_host="https://webinstall.dev"
my_len="${#my_host}"
# 6 because the field will looks like "loc>WEBI_HOST/PKG_NAME"
# and the count is 1-indexed
my_count="$((my_len + 6))"
my_now="$(date -u '+%s')"
echo "${my_now}" > ~/.local/share/webi/var/last_update
my_tmp="$(mktemp)"
{
echo "help"
echo "--help"
echo "version"
echo "-V"
echo "--version"
echo "--init" # <shell>
echo "--list"
echo "--info" # <package>
} > "${my_tmp}"
curl -fsS "${my_host}/sitemap.xml" |
grep -F "${my_host}" |
cut -d'<' -f2 |
cut -c "${my_count}"- >> "${my_tmp}"
mv "${my_tmp}" ~/.local/share/webi/var/list.txt
my_now="$(date -u '+%s')"
echo "${my_now}" > ~/.local/share/webi/var/last_update
); }
webi_info() { (
if test -z "${2}"; then
echo >&2 "Usage: webi --info <package>"
exit 1
fi
echo >&2 "[warn] the output of --info is completely half-baked and will change"
my_pkg="${2}"
# TODO need a way to check that it exists at all (readme, win, lin)
echo ""
echo " Cheat Sheet: ${WEBI_HOST}/${my_pkg}"
echo " POSIX: curl -sS ${WEBI_HOST}/${my_pkg} | sh"
echo " Windows: curl.exe -A MS ${WEBI_HOST}/${my_pkg} | powershell"
echo "Releases (JSON): ${WEBI_HOST}/api/releases/${my_pkg}.json"
echo " Releases (tsv): ${WEBI_HOST}/api/releases/${my_pkg}.tab"
echo " (query params): ?channel=stable&limit=10"
echo " &os=${my_os}&arch=${my_arch}"
echo " Install Script: ${WEBI_HOST}/api/installers/${my_pkg}.sh?formats=tar,zip,xz,git,dmg,pkg"
echo " Static Assets: ${WEBI_HOST}/packages/${my_pkg}/README.md"
echo ""
# TODO os=linux,macos,windows (limit to tagged releases)
my_releases="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?channel=stable&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases}" | grep -q "error"; then
my_releases_beta="$(
curl -fsS "${WEBI_HOST}/api/releases/${my_pkg}.json?&limit=1&pretty=true"
)"
if printf '%s\n' "${my_releases_beta}" | grep -q "error"; then
echo >&2 "'${my_pkg}' is a special case that does not have releases"
else
echo >&2 "ERROR no stable releases for '${my_pkg}'!"
fi
exit 0
fi
echo >&2 "Stable '${my_pkg}' releases:"
if command -v jq > /dev/null; then
printf '%s\n' "${my_releases}" |
jq
else
printf '%s\n' "${my_releases}"
fi
); }
__webi_main "$@"