container: plugins: root: rewrite example plugin

- Reorganizes to utilize new pluggable framework
- Refactors, adds/updates documentation
This commit is contained in:
2025-12-05 10:56:17 -08:00
parent 8055a9494f
commit 29574ca74d
4 changed files with 274 additions and 145 deletions

View File

@@ -0,0 +1,125 @@
// docker-finance | modern accounting for the power-user
//
// Copyright (C) 2024-2025 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/>.
//! \file
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
//! \since docker-finance 1.1.0
// NOTE: `dfi` API headers are included by default
#include "./example.hh"
#include <limits>
namespace dfi::plugin::example
{
std::string Example::make_cmd(const std::string& base, const std::string& arg)
{
return std::string{base + arg};
}
void Example::exec_cmd(const std::string& cmd)
{
namespace common = ::dfi::common;
const std::string base{
common::get_env("global_basename") + " "
+ common::get_env("global_parent_profile")
+ common::get_env("global_arg_delim_1")
+ common::get_env("global_child_profile") + " "};
common::throw_ex_if<common::type::RuntimeError>(
common::exec("bash -i -c '" + Example::make_cmd(base, cmd) + "'"),
"command failed");
}
void Example::print_env(const std::string& env)
{
std::cout << env << "=" << ::dfi::common::get_env(env) << "\n";
};
void Example::example1()
{
using g_Random = dfi::crypto::cryptopp::Random;
using g_Hash = dfi::crypto::libsodium::Hash;
g_Random r;
g_Hash h;
for (size_t i{}; i < 5; i++)
{
uint32_t num = r.generate();
std::cout << h.encode<g_Hash::SHA2_256>(num) << " = " << num << "\n";
}
}
void Example::example2()
{
// Container environment
std::cout
<< "\nShell environment (container)\n-----------------------------\n";
Example::print_env("DOCKER_FINANCE_CLIENT_FLOW");
Example::print_env("DOCKER_FINANCE_CONTAINER_CMD");
Example::print_env("DOCKER_FINANCE_CONTAINER_CONF");
Example::print_env("DOCKER_FINANCE_CONTAINER_EDITOR");
Example::print_env("DOCKER_FINANCE_CONTAINER_FLOW");
Example::print_env("DOCKER_FINANCE_CONTAINER_PLUGINS");
Example::print_env("DOCKER_FINANCE_CONTAINER_REPO");
Example::print_env("DOCKER_FINANCE_CONTAINER_SHARED");
Example::print_env("DOCKER_FINANCE_DEBUG");
Example::print_env("DOCKER_FINANCE_VERSION");
std::cout << "\nSame as previous but executing commands in "
"shell\n------------------------------------------------\n";
::dfi::common::exec("printenv | grep ^DOCKER_FINANCE | sort");
// Caller environment
std::cout << "\nShell environment (caller)\n--------------------------\n";
Example::print_env("global_arg_delim_1");
Example::print_env("global_arg_delim_2");
Example::print_env("global_arg_delim_3");
Example::print_env("global_arg_subcommand");
Example::print_env("global_basename");
Example::print_env("global_child_profile");
Example::print_env("global_child_profile_flow");
Example::print_env("global_child_profile_journal");
Example::print_env("global_conf_fetch");
Example::print_env("global_conf_hledger");
Example::print_env("global_conf_meta");
Example::print_env("global_conf_subscript");
// TODO(unassigned): read array from shell? ROOT impl simply calls stdlib getenv()
// Example::print_env("global_hledger_cmd");
Example::print_env("global_parent_profile");
Example::print_env("global_usage");
}
void Example::example3()
{
std::cout << "\nImporting journals...\n";
Example::exec_cmd("import 1>/dev/null");
std::cout << "\nShowing BTC balance...\n";
Example::exec_cmd("hledger bal assets liabilities cur:BTC");
std::cout << "\nGenerating income tax snippet...\n";
const std::string delim{::dfi::common::get_env("global_arg_delim_2")};
Example::exec_cmd(
"taxes all" + delim + "account tag" + delim + "income write" + delim
+ "off | tail -n3");
}
} // namespace dfi::plugin::example
// # vim: sw=2 sts=2 si ai et

View File

@@ -0,0 +1,68 @@
// docker-finance | modern accounting for the power-user
//
// Copyright (C) 2024-2025 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/>.
//! \file
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
//! \since docker-finance 1.1.0
#ifndef CONTAINER_PLUGINS_ROOT_EXAMPLE_INTERNAL_EXAMPLE_HH_
#define CONTAINER_PLUGINS_ROOT_EXAMPLE_INTERNAL_EXAMPLE_HH_
#include <string>
// NOTE: `dfi` API headers are included by default
namespace dfi::plugin::example
{
//! \brief An example plugin implementation class
//!
//! \warning When implementing pluggables, prefer member functions with
//! definitions within source file(s) over certain lambdas to prevent
//! root segfaults. TODO(afiore): investigate.
//!
//! \note For demonstration purposes only
//!
//! \ingroup cpp_plugin_impl
//! \since docker-finance 1.1.0
class Example
{
public:
//! \brief Example plugin function 1
//! \details Directly access docker-finance crypto abstractions and
//! print a handful of SHA2-256 libsodium-generated hashes of
//! Crypto++-generated cryptographically secure random numbers
void example1();
//! \brief Example plugin function 2
//! \details Directly access underlying docker-finance shell environment
void example2();
//! \brief Example plugin function 3
//! \details Execute docker-finance shell commands
void example3();
protected:
std::string make_cmd(const std::string& base, const std::string& arg);
void exec_cmd(const std::string& cmd);
void print_env(const std::string& env);
};
} // namespace dfi::plugin::example
#endif // CONTAINER_PLUGINS_ROOT_EXAMPLE_INTERNAL_EXAMPLE_HH_
// # vim: sw=2 sts=2 si ai et