container: lib_ledger: refactor for base arguments
- Future-proof for hledger features - Related refactoring
This commit is contained in:
@@ -30,30 +30,35 @@ source "${DOCKER_FINANCE_CONTAINER_REPO}/src/finance/lib/internal/lib_utils.bash
|
|||||||
|
|
||||||
function lib_ledger::ledger-import()
|
function lib_ledger::ledger-import()
|
||||||
{
|
{
|
||||||
|
lib_ledger::__ledger "$@"
|
||||||
lib_ledger::__ledger-import "$@"
|
lib_ledger::__ledger-import "$@"
|
||||||
lib_utils::catch $?
|
lib_utils::catch $?
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::ledger-cli()
|
function lib_ledger::ledger-cli()
|
||||||
{
|
{
|
||||||
|
lib_ledger::__ledger "$@"
|
||||||
lib_ledger::__ledger-cli "$@"
|
lib_ledger::__ledger-cli "$@"
|
||||||
lib_utils::catch $?
|
lib_utils::catch $?
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::ledger-ui()
|
function lib_ledger::ledger-ui()
|
||||||
{
|
{
|
||||||
|
lib_ledger::__ledger "$@"
|
||||||
lib_ledger::__ledger-ui "$@"
|
lib_ledger::__ledger-ui "$@"
|
||||||
lib_utils::catch $?
|
lib_utils::catch $?
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::ledger-vui()
|
function lib_ledger::ledger-vui()
|
||||||
{
|
{
|
||||||
|
lib_ledger::__ledger "$@"
|
||||||
lib_ledger::__ledger-vui "$@"
|
lib_ledger::__ledger-vui "$@"
|
||||||
lib_utils::catch $?
|
lib_utils::catch $?
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::ledger-web()
|
function lib_ledger::ledger-web()
|
||||||
{
|
{
|
||||||
|
lib_ledger::__ledger "$@"
|
||||||
lib_ledger::__ledger-web "$@"
|
lib_ledger::__ledger-web "$@"
|
||||||
lib_utils::catch $?
|
lib_utils::catch $?
|
||||||
}
|
}
|
||||||
@@ -62,7 +67,38 @@ function lib_ledger::ledger-web()
|
|||||||
# Implementation
|
# Implementation
|
||||||
#
|
#
|
||||||
|
|
||||||
function lib_ledger::__ledger-import()
|
# Constructor
|
||||||
|
function lib_ledger::__ledger()
|
||||||
|
{
|
||||||
|
# Base arguments to hledger before end-user added
|
||||||
|
[ -z "$global_child_profile_journal" ] && lib_utils::die_fatal
|
||||||
|
declare -g global_base_args=("-f" "$global_child_profile_journal")
|
||||||
|
|
||||||
|
#
|
||||||
|
# Apply features to given hledger version.
|
||||||
|
#
|
||||||
|
# CLI version formats:
|
||||||
|
#
|
||||||
|
# hledger 1.34, linux-x86_64
|
||||||
|
# hledger 1.34.99-gcf0c7c2ef-20240702, linux-x86_64
|
||||||
|
#
|
||||||
|
# TODO: hope that container platforms will package 1.35 before 1.40 rolls out
|
||||||
|
#
|
||||||
|
|
||||||
|
# 1.34.99 and above
|
||||||
|
hledger --version \
|
||||||
|
| gawk '{ if ($2 !~ /^1.3(4.99|([5-9][\.[0-99]?))/) { exit 1 } }' FS=' '
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
# --conf is supported
|
||||||
|
[ -z "$global_conf_hledger" ] && lib_utils::die_fatal
|
||||||
|
global_base_args+=("--conf" "$global_conf_hledger")
|
||||||
|
fi
|
||||||
|
|
||||||
|
lib_utils::print_debug "${global_base_args[*]}" "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
function lib_ledger::__parse_ledger-import()
|
||||||
{
|
{
|
||||||
[ -z "$global_usage" ] && lib_utils::die_fatal
|
[ -z "$global_usage" ] && lib_utils::die_fatal
|
||||||
[ -z "$global_arg_delim_1" ] && lib_utils::die_fatal
|
[ -z "$global_arg_delim_1" ] && lib_utils::die_fatal
|
||||||
@@ -80,7 +116,7 @@ function lib_ledger::__ledger-import()
|
|||||||
|
|
||||||
\e[32mArguments:\e[0m
|
\e[32mArguments:\e[0m
|
||||||
|
|
||||||
Fetch year:
|
Import year:
|
||||||
|
|
||||||
year${global_arg_delim_2}<all|year>
|
year${global_arg_delim_2}<all|year>
|
||||||
|
|
||||||
@@ -94,13 +130,11 @@ function lib_ledger::__ledger-import()
|
|||||||
lib_utils::die_usage "$_usage"
|
lib_utils::die_usage "$_usage"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get/Set year
|
|
||||||
if [ ! -z "$_arg" ]; then
|
if [ ! -z "$_arg" ]; then
|
||||||
if [[ ! "$_arg" =~ ^year[s]?${global_arg_delim_2} ]]; then
|
if [[ ! "$_arg" =~ ^year[s]?${global_arg_delim_2} ]]; then
|
||||||
lib_utils::die_usage "$_usage"
|
lib_utils::die_usage "$_usage"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Parse key for value
|
|
||||||
local _key="${_arg%${global_arg_delim_2}*}"
|
local _key="${_arg%${global_arg_delim_2}*}"
|
||||||
local _len="$((${#_key} + 1))"
|
local _len="$((${#_key} + 1))"
|
||||||
if [[ "$_key" =~ ^year[s]?$ ]]; then
|
if [[ "$_key" =~ ^year[s]?$ ]]; then
|
||||||
@@ -122,31 +156,44 @@ function lib_ledger::__ledger-import()
|
|||||||
global_arg_year="$(date +%Y)"
|
global_arg_year="$(date +%Y)"
|
||||||
declare -gr global_arg_year
|
declare -gr global_arg_year
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function lib_ledger::__ledger-import()
|
||||||
|
{
|
||||||
|
lib_ledger::__parse_ledger-import "$@"
|
||||||
|
|
||||||
[ -z "$global_child_profile_journal" ] && lib_utils::die_fatal
|
|
||||||
time hledger-flow import \
|
time hledger-flow import \
|
||||||
"$(dirname $global_child_profile_journal)" --start-year "$global_arg_year"
|
"$(dirname $global_child_profile_journal)" \
|
||||||
|
--start-year "$global_arg_year"
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::__ledger-cli()
|
function lib_ledger::__ledger-cli()
|
||||||
{
|
{
|
||||||
hledger -f "$global_child_profile_journal" "$@"
|
[ -z "${global_base_args[*]}" ] && lib_utils::die_fatal
|
||||||
|
|
||||||
|
hledger "${global_base_args[@]}" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::__ledger-ui()
|
function lib_ledger::__ledger-ui()
|
||||||
{
|
{
|
||||||
hledger-ui -f "$global_child_profile_journal" "$@"
|
[ -z "${global_base_args[*]}" ] && lib_utils::die_fatal
|
||||||
|
|
||||||
|
hledger-ui "${global_base_args[@]}" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::__ledger-vui()
|
function lib_ledger::__ledger-vui()
|
||||||
{
|
{
|
||||||
hledger -f "$global_child_profile_journal" print -O csv "$@" \
|
[ -z "${global_base_args[*]}" ] && lib_utils::die_fatal
|
||||||
|
|
||||||
|
hledger "${global_base_args[@]}" print -O csv "$@" \
|
||||||
| visidata --motd-url file:///dev/null --filetype csv
|
| visidata --motd-url file:///dev/null --filetype csv
|
||||||
}
|
}
|
||||||
|
|
||||||
function lib_ledger::__ledger-web()
|
function lib_ledger::__ledger-web()
|
||||||
{
|
{
|
||||||
hledger-web -f "$global_child_profile_journal" "$@"
|
[ -z "${global_base_args[*]}" ] && lib_utils::die_fatal
|
||||||
|
|
||||||
|
hledger-web "${global_base_args[@]}" "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
# vim: sw=2 sts=2 si ai et
|
# vim: sw=2 sts=2 si ai et
|
||||||
|
|||||||
Reference in New Issue
Block a user