Merge pull request #109 into master

bbf96b0 container: bash: completion: add plugins support (Aaron Fiore)
a4f9b40 container: bash: add plugins support / lib_plugins (Aaron Fiore)
eaec539 client: lib_gen: generate plugins (Aaron Fiore)
This commit was merged in pull request #109.
This commit is contained in:
2024-08-08 16:24:11 -07:00
5 changed files with 173 additions and 5 deletions

View File

@@ -436,15 +436,93 @@ function lib_gen::__gen_container()
mkdir -p "$DOCKER_FINANCE_CLIENT_FLOW" || lib_utils::die_fatal
fi
#
# Generate profile/subprofile
#
lib_gen::__gen_plugins
lib_gen::__gen_profile
}
#
# Generate flow: plugins
#
# - Provides a place to drop-in any custom plugin
# - Provides a default example plugin
#
function lib_gen::__gen_plugins()
{
lib_utils::print_debug "Generating plugins"
local -r _plugins="${DOCKER_FINANCE_CLIENT_FLOW}/plugins"
if [ ! -d "$_plugins" ]; then
mkdir -p "$_plugins" || lib_utils::die_fatal
fi
local -r _example="${_plugins}/example.bash"
if [ -f "$_example" ]; then
lib_utils::print_debug "Example plugin found, overwriting: '${_example}'"
fi
echo "#!/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 <https://www.gnu.org/licenses/>.
#
# Plugins
#
# The plugins path (this directory) is where you can place any executable.
#
# Any plugin found in this directory will be made available via the \`plugins\` command.
# As such, your plugin will have access to all global variables provided by finance.bash
#
# The following is an example of how to create your own plugin:
#
echo -e \"
This container's environment:
\$(printenv | grep ^DOCKER_FINANCE)
This plugin's caller profile:
\${global_parent_profile}\${global_arg_delim_1}\${global_child_profile}
This plugin's path is:
\$0
This plugin's arguments:
'\${@}'
\"
# vim: sw=2 sts=2 si ai et" >"$_example"
chmod +x "$_example" || lib_utils::die_fatal
}
#
# Generate flow: profile/subprofile
#
function lib_gen::__gen_profile()
{
lib_utils::print_debug "Generating profiles"
local -r _profiles="${DOCKER_FINANCE_CLIENT_FLOW}/profiles"
if [ ! -d "$_profiles" ]; then
mkdir -p "$_profiles" || lib_utils::die_fatal

View File

@@ -28,7 +28,7 @@ function docker-finance::completion()
mapfile -t _profiles < <(pushd "${DOCKER_FINANCE_CONTAINER_FLOW}"/profiles &>/dev/null && ls -d */*)
declare -r _profiles
local -r _commands=("all" "edit" "fetch" "import" "ledger" "ledger-ui" "ledger-web" "meta" "reports" "root" "taxes")
local -r _commands=("all" "edit" "fetch" "import" "ledger" "ledger-ui" "ledger-web" "meta" "plugins" "reports" "root" "taxes")
local _reply
@@ -76,6 +76,12 @@ function docker-finance::completion()
# NOTE: args are dependent upon metadata contents
mapfile -t _reply < <(compgen -W "help" -- "$_cur")
;;
plugins)
local _plugins
mapfile -t _plugins < <(find "${DOCKER_FINANCE_CONTAINER_FLOW}"/plugins -type f -executable -printf '%P\n' 2>/dev/null)
declare -r _plugins
mapfile -t _reply < <(compgen -W "${_plugins[*]}" -- "$_cur")
;;
reports)
mapfile -t _reply < <(compgen -W "help all${global_arg_delim_2} type${global_arg_delim_2} interval${global_arg_delim_2} year${global_arg_delim_2}" -- "$_cur")
;;

View File

@@ -58,6 +58,7 @@ function main()
ledger-ui \e[34;3mStart terminal UI (hledger-ui)\e[0m
ledger-web \e[34;3mStart web-based UI (hledger-web)\e[0m
meta \e[34;3mSearch local financial metadata\e[0m
plugins \e[34;3mExecute given plugin available in plugins' path\e[0m
reports \e[34;3mGenerate balance sheet, income statement, etc.\e[0m
root \e[34;3mRun ROOT.cern instance for docker-finance analysis\e[0m
taxes \e[34;3mGenerate tax reports\e[0m
@@ -122,6 +123,9 @@ function main()
meta)
lib_finance::meta "${@:3}"
;;
plugins)
lib_finance::plugins "${@:3}"
;;
reports)
lib_finance::reports "${@:3}"
;;

View File

@@ -0,0 +1,73 @@
#!/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 <https://www.gnu.org/licenses/>.
#
# "Libraries"
#
[ -z "$DOCKER_FINANCE_CONTAINER_REPO" ] && exit 1
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_utils.bash" || exit 1
#
# Facade
#
function lib_plugins::plugins()
{
# NOTE: no args to parse, pass directly to plugin
lib_plugins::__plugins "$@"
lib_utils::catch $?
}
#
# Implementation
#
function lib_plugins::__plugins()
{
[ -z "$global_usage" ] && lib_utils::die_fatal
[ -z "$DOCKER_FINANCE_CONTAINER_FLOW" ] && lib_utils::die_fatal
local -r _path="${DOCKER_FINANCE_CONTAINER_FLOW}/plugins"
local -r _usage="
\e[32mDescription:\e[0m
Execute a plugin within the flow plugins path
\e[32mUsage:\e[0m
$ $global_usage <plugin> [args]
\e[32mExamples:\e[0m
\e[37;2m# Execute a custom plugin in ${_path}\e[0m
$ $global_usage example.bash \"hello\"
"
if [ "$#" -eq 0 ]; then
lib_utils::die_usage "$_usage"
fi
exec ${_path}/$*
return $?
}
# vim: sw=2 sts=2 si ai et

View File

@@ -87,6 +87,7 @@ function lib_finance::finance()
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_fetch.bash" || exit 1
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_ledger.bash" || exit 1
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_meta.bash" || exit 1
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_plugins.bash" || exit 1
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_reports.bash" || exit 1
source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_taxes.bash" || exit 1
}
@@ -199,6 +200,12 @@ function lib_finance::ledger-web()
lib_utils::catch $?
}
function lib_finance::plugins()
{
lib_plugins::plugins "$@"
lib_utils::catch $?
}
function lib_finance::reports()
{
lib_reports::reports "$@"