#!/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 # TODO: HACK: needed for `plugins` completion declare -gx instance function docker-finance::completion() { local _cur="${COMP_WORDS[COMP_CWORD]}" local _prev="${COMP_WORDS[COMP_CWORD - 1]}" local _images mapfile -t _images < <(docker images --format "{{.Repository}}:{{.Tag}}" --filter=reference='docker-finance/*/*:*' | cut -d'/' -f2- | grep $USER) declare -r _images local -r _commands=("gen" "edit" "build" "backup" "rm" "up" "down" "start" "stop" "run" "shell" "version" "license" "linter" "doxygen" "plugins") 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 instance="${COMPREPLY[*]}" ;; 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") ;; plugins) # TODO: HACK: since this is completion, lib_docker is never initialized, prior to completing # (environment variables are needed). if [ -z "$DOCKER_FINANCE_CLIENT_REPO" ]; then _aliases=~/.bash_aliases ! test -f "$_aliases" && _aliases=~/.bashrc declare -g DOCKER_FINANCE_CLIENT_REPO DOCKER_FINANCE_CLIENT_REPO="$(dirname "$(grep '^alias docker-finance=' $_aliases | cut -d'=' -f2 | sed -e "s:'::g" -e 's:"::g')")/../" source "${DOCKER_FINANCE_CLIENT_REPO}/client/src/docker/lib/lib_docker.bash" || echo $? local _instance declare -r _instance="$(echo $instance | sed "s:'::g")" # TODO: remove after hack for colon tag is resolved lib_docker::docker "$_instance" || lib_utils::print_fatal "$_instance not initialized" fi [ -z "$DOCKER_FINANCE_CLIENT_PLUGINS" ] && lib_utils::print_fatal "DOCKER_FINANCE_CLIENT_PLUGINS not set" local _plugins mapfile -t _plugins < <({ find "${DOCKER_FINANCE_CLIENT_PLUGINS}"/client/docker -type f -executable -printf 'custom/%P\n' 2>/dev/null find "${DOCKER_FINANCE_CLIENT_REPO}"/client/plugins/docker -type f -executable -printf 'repo/%P\n' 2>/dev/null }) declare -r _plugins mapfile -t _reply < <(compgen -W "${_plugins[*]}" -- "$_cur") ;; esac declare -r _reply COMPREPLY=("${_reply[@]}") ;; *) COMPREPLY=() ;; esac } complete -F docker-finance::completion docker.bash complete -F docker-finance::completion docker-finance complete -F docker-finance::completion dfi # vim: sw=2 sts=2 si ai et