From 7f86b6a2503c8d26d09a3236acfb6ee816e0717d Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Sat, 3 Aug 2024 19:07:35 -0700 Subject: [PATCH] client: add preliminary bash completion --- client/src/docker/completion.bash | 99 +++++++++++++++++++++++++++++++ client/src/docker/docker.bash | 10 ++-- 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 client/src/docker/completion.bash diff --git a/client/src/docker/completion.bash b/client/src/docker/completion.bash new file mode 100644 index 0000000..1cbb6d7 --- /dev/null +++ b/client/src/docker/completion.bash @@ -0,0 +1,99 @@ +#!/usr/bin/env bash + +# docker-finance | modern accounting for the power-user +# +# Copyright (C) 2024 Aaron Fiore (Founder, Evergreen Crypto LLC) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# If not yet installed, gracefully exit +if ! hash docker &>/dev/null; then + exit 0 +fi + +function docker-finance::completion() +{ + local _cur="${COMP_WORDS[COMP_CWORD]}" + local _prev="${COMP_WORDS[COMP_CWORD - 1]}" + + local _images + mapfile -t _images < <(docker image ls | grep ^docker-finance | cut -d'/' -f2- | cut -d' ' -f1-4 | sed -e 's/ /:/') + declare -r _images + + local -r _commands=("gen" "edit" "build" "backup" "rm" "up" "down" "start" "stop" "run" "shell" "version" "license" "linter" "doxygen") + + local _reply + + case $COMP_CWORD in + 1) + mapfile -t _reply < <(compgen -W "${_images[*]}" -- "$_cur") + declare -r _reply + # TODO: HACK for colon tag. Consider using bash-completion package. + for _r in "${_reply[@]}"; do + COMPREPLY+=("'${_r}'") + done + ;; + 2) + mapfile -t _reply < <(compgen -W "${_commands[*]}" -- "$_cur") + declare -r _reply + COMPREPLY=("${_reply[@]}") + ;; + # TODO: multiple options (in 3) should have multiple completions + 3) + # NOTE: same as in client impl + [ -z "$global_arg_delim_2" ] && global_arg_delim_2="=" + + # Disable space after completion + compopt -o nospace + + case "$_prev" in + gen) + # TODO: _currently no-op + ;; + edit) + mapfile -t _reply < <(compgen -W "help type${global_arg_delim_2}" -- "$_cur") + ;; + build) + mapfile -t _reply < <(compgen -W "help type${global_arg_delim_2}" -- "$_cur") + ;; + backup | rm | up | down | start | stop) + # TODO: _currently no-op + ;; + shell) + mapfile -t _reply < <(compgen -W "help user${global_arg_delim_2}" -- "$_cur") + ;; + version) + mapfile -t _reply < <(compgen -W "help type${global_arg_delim_2}" -- "$_cur") + ;; + license) + mapfile -t _reply < <(compgen -W "help file${global_arg_delim_2}" -- "$_cur") + ;; + linter) + mapfile -t _reply < <(compgen -W "help type${global_arg_delim_2} file${global_arg_delim_2}" -- "$_cur") + ;; + doxygen) + mapfile -t _reply < <(compgen -W "help gen${global_arg_delim_2} rm${global_arg_delim_2}" -- "$_cur") + ;; + esac + declare -r _reply + COMPREPLY=("${_reply[@]}") + ;; + *) + COMPREPLY=() + ;; + esac +} + +complete -F docker-finance::completion docker.bash +complete -F docker-finance::completion docker-finance diff --git a/client/src/docker/docker.bash b/client/src/docker/docker.bash index fa69673..e917df3 100755 --- a/client/src/docker/docker.bash +++ b/client/src/docker/docker.bash @@ -95,8 +95,9 @@ function main() \e[37;2m# Finance platform\e[0m \e[37;2m#\e[0m - \e[37;2m# Setup an alias for latest \e[0m - $ alias docker-finance=\"$0 archlinux${global_arg_delim_1}${USER}:latest\" + \e[37;2m# For the sake of simplicity, recreate the docker-finance alias using \e[0m + $ unalias docker-finance 2>/dev/null \\ + ; alias docker-finance=\"$0 archlinux${global_arg_delim_1}${USER}:default\" \e[37;2m# Generate environment and build default image\e[0m $ docker-finance gen && docker-finance build type${global_arg_delim_2}default @@ -127,8 +128,9 @@ function main() \e[37;2m# Dev-tools platform\e[0m \e[37;2m#\e[0m - \e[37;2m# Setup an alias for dev-tools\e[0m - $ alias dev-tools=\"$0 dev-tools${global_arg_delim_1}${USER}:latest\" + \e[37;2m# For the sake of simplicity, recreate the dev-tools alias using \e[0m + $ unalias dev-tools 2>/dev/null \\ + ; alias dev-tools=\"$0 dev-tools${global_arg_delim_1}${USER}:default\" \e[37;2m# Generate environment and build default image\e[0m $ dev-tools gen && dev-tools build type${global_arg_delim_2}default