From 97a7c5c25a240bcfa22d00f2571848a7826a8548 Mon Sep 17 00:00:00 2001 From: Hannes Laimer Date: Wed, 1 Mar 2023 08:24:15 +0100 Subject: [PATCH] kdb-cli: rewrite meta-show --- src/tools/kdb/factory.hpp | 2 - src/tools/kdb/meta-show.c | 76 ++++++++++++++++++++++++++++++++++++++ src/tools/kdb/meta-show.h | 33 +++++++++++++++++ src/tools/kdb/meta.c | 2 + src/tools/kdb/showmeta.cpp | 53 -------------------------- src/tools/kdb/showmeta.hpp | 47 ----------------------- 6 files changed, 111 insertions(+), 102 deletions(-) create mode 100644 src/tools/kdb/meta-show.c create mode 100644 src/tools/kdb/meta-show.h delete mode 100644 src/tools/kdb/showmeta.cpp delete mode 100644 src/tools/kdb/showmeta.hpp diff --git a/src/tools/kdb/factory.hpp b/src/tools/kdb/factory.hpp index 959bd3ab710..e928e4179ec 100644 --- a/src/tools/kdb/factory.hpp +++ b/src/tools/kdb/factory.hpp @@ -48,7 +48,6 @@ #include #include #include -#include #include #include #include @@ -89,7 +88,6 @@ class Factory m_factory.insert (std::make_pair ("remount", std::make_shared> ())); m_factory.insert (std::make_pair ("shell", std::make_shared> ())); m_factory.insert (std::make_pair ("find", std::make_shared> ())); - m_factory.insert (std::make_pair ("meta-show", std::make_shared> ())); m_factory.insert (std::make_pair ("meta-rm", std::make_shared> ())); m_factory.insert (std::make_pair ("plugin-info", std::make_shared> ())); m_factory.insert (std::make_pair ("test", std::make_shared> ())); diff --git a/src/tools/kdb/meta-show.c b/src/tools/kdb/meta-show.c new file mode 100644 index 00000000000..dceb056ef50 --- /dev/null +++ b/src/tools/kdb/meta-show.c @@ -0,0 +1,76 @@ +/** + * @file + * + * @brief Implementation of kdb meta-show command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#include + +#include +#include +#include +#include +#include + +#define COMMAND_NAME "meta/show" + +#define GET_OPTION_KEY(options, name) GET_OPT_KEY (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name) +#define GET_OPTION(options, name) GET_OPT (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name) + +void addMetaShowSpec (KeySet * spec) +{ + ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", + "Print all metakeys with their value for a key.", KEY_META, "command", "show", KEY_END)); + ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/keyname", KEY_META, "description", "The name of the key", KEY_META, + "args", "indexed", KEY_META, "args/index", "0", KEY_END)); + ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME)) +} + +int execMetaShow (KeySet * options, Key * errorKey) +{ + int ret = 0; + GET_BASIC_OPTIONS + + bool nullTerm = false; + tmp = GET_OPTION_KEY (options, "nullterm"); + if (tmp != NULL) + { + elektraKeyToBoolean (tmp, &nullTerm); + } + + const char * name = getKeyNameFromOptions (options, GET_OPTION (options, "keyname"), errorKey, verbose); + if (name == NULL) return 1; + + Key * toLookUp = keyNew (name, KEY_END); + KeySet * conf = ksNew (0, KS_END); + KDB * handle = kdbOpen (NULL, errorKey); + + if (kdbGet (handle, conf, toLookUp) == -1) + { + ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not load '%s': %s", name, GET_ERR (toLookUp)); + ret = 1; + goto cleanup; + } + + Key * key = ksLookup (conf, toLookUp, KDB_O_NONE); + KeySet * metaKeys = keyMeta (key); + + Key * cur = NULL; + for (elektraCursor it = 0; it < ksGetSize (metaKeys); ++it) + { + cur = ksAtCursor (metaKeys, it); + CLI_PRINT (CLI_LOG_NONE, "%s -> %s%c", keyName (cur), BOLD(keyString (cur)), nullTerm ? '\0' : '\n'); + + } + + +cleanup: + kdbClose (handle, errorKey); + ksDel (conf); + keyDel (toLookUp); + elektraFree (fmtBuffer); + elektraFree ((void *) name); + return ret; +} diff --git a/src/tools/kdb/meta-show.h b/src/tools/kdb/meta-show.h new file mode 100644 index 00000000000..98f11bd50f6 --- /dev/null +++ b/src/tools/kdb/meta-show.h @@ -0,0 +1,33 @@ +/** + * @file + * + * @brief Header for meta-show command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#ifndef ELEKTRA_KDB_META_SHOW_H +#define ELEKTRA_KDB_META_SHOW_H + +#include + +/** + * Adds options specification of meta-show command to @spec + * + * @param spec the base spec where the commands spec should be added + */ +void addMetaShowSpec (KeySet * spec); + +/** + * Executes the meta-show command + * + * @param options cli options and arguments as specified in @addMetaShowSpec() + * @param errorKey key where errors and warnings should be saved + * + * @retval 0 show command ran without errors + * @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info + * + */ +int execMetaShow (KeySet * options, Key * errorKey); + +#endif // ELEKTRA_KDB_META_SHOW_H diff --git a/src/tools/kdb/meta.c b/src/tools/kdb/meta.c index cf9872a6877..c30db2287b9 100644 --- a/src/tools/kdb/meta.c +++ b/src/tools/kdb/meta.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,7 @@ command metaSubcommands[] = { { "get", addMetaGetSpec, execMetaGet }, { "ls", addMetaLsSpec, execMetaLs }, { "set", addMetaSetSpec, execMetaSet }, + { "show", addMetaShowSpec, execMetaShow }, }; void addMetaSpec (KeySet * spec) diff --git a/src/tools/kdb/showmeta.cpp b/src/tools/kdb/showmeta.cpp deleted file mode 100644 index 4705705df04..00000000000 --- a/src/tools/kdb/showmeta.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#include - -#include -#include - -#include - -using namespace std; -using namespace kdb; - -ShowMetaCommand::ShowMetaCommand () -{ -} - -int ShowMetaCommand::execute (Cmdline const & cl) -{ - if (cl.arguments.size () != 1) throw invalid_argument ("Need one argument"); - - Key root = cl.createKey (0); - KeySet conf; - kdb.get (conf, root); - printWarnings (cerr, root, cl.verbose, cl.debug); - - Key k = conf.lookup (root); - - if (!k) - { - cerr << "Key not found" << endl; - return 1; - } - - ckdb::KeySet * metaKeys = ckdb::keyMeta (k.getKey ()); - - for (ssize_t it = 0; it < ckdb::ksGetSize (metaKeys); ++it) - { - const Key & curMeta = ckdb::ksAtCursor (metaKeys, it); - cout << curMeta.getName ().substr (sizeof ("meta:/") - 1) << ": " << curMeta.getString () << endl; - } - - return 0; -} - -ShowMetaCommand::~ShowMetaCommand () -{ -} diff --git a/src/tools/kdb/showmeta.hpp b/src/tools/kdb/showmeta.hpp deleted file mode 100644 index 052d0fb0732..00000000000 --- a/src/tools/kdb/showmeta.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#ifndef SHOWMETA_HPP -#define SHOWMETA_HPP - -#include "coloredkdbio.hpp" -#include -#include - -class ShowMetaCommand : public Command -{ - kdb::KDB kdb; - -public: - ShowMetaCommand (); - ~ShowMetaCommand (); - - virtual std::string getShortOptions () override - { - return ""; - } - - virtual std::string getSynopsis () override - { - return ""; - } - - virtual std::string getShortHelpText () override - { - return "Print all metakeys along with their value for the given key."; - } - - virtual std::string getLongHelpText () override - { - return ""; - } - - virtual int execute (Cmdline const & cmdline) override; -}; - -#endif