From 892cbda491d619b8a64454ec3bf94ce1a0d56c0c Mon Sep 17 00:00:00 2001 From: Hannes Laimer Date: Wed, 1 Mar 2023 08:06:26 +0100 Subject: [PATCH] kdb-cli: rewrite meta-ls --- src/tools/kdb/factory.hpp | 2 -- src/tools/kdb/meta-ls.c | 76 +++++++++++++++++++++++++++++++++++++++ src/tools/kdb/meta-ls.h | 33 +++++++++++++++++ src/tools/kdb/meta.c | 2 ++ src/tools/kdb/metals.cpp | 73 ------------------------------------- src/tools/kdb/metals.hpp | 47 ------------------------ 6 files changed, 111 insertions(+), 122 deletions(-) create mode 100644 src/tools/kdb/meta-ls.c create mode 100644 src/tools/kdb/meta-ls.h delete mode 100644 src/tools/kdb/metals.cpp delete mode 100644 src/tools/kdb/metals.hpp diff --git a/src/tools/kdb/factory.hpp b/src/tools/kdb/factory.hpp index 7cf79b667fe..959bd3ab710 100644 --- a/src/tools/kdb/factory.hpp +++ b/src/tools/kdb/factory.hpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -92,7 +91,6 @@ class Factory 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 ("meta-ls", std::make_shared> ())); m_factory.insert (std::make_pair ("plugin-info", std::make_shared> ())); m_factory.insert (std::make_pair ("test", std::make_shared> ())); m_factory.insert (std::make_pair ("plugin-check", std::make_shared> ())); diff --git a/src/tools/kdb/meta-ls.c b/src/tools/kdb/meta-ls.c new file mode 100644 index 00000000000..6b28a2a15dc --- /dev/null +++ b/src/tools/kdb/meta-ls.c @@ -0,0 +1,76 @@ +/** + * @file + * + * @brief Implementation of kdb meta-ls command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#define COMMAND_NAME "meta/ls" + +#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 addMetaLsSpec (KeySet * spec) +{ + ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "List available metadata names for a key.", + KEY_META, "command", "ls", 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 execMetaLs (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%c", keyName (cur), nullTerm ? '\0' : '\n'); + } + +cleanup: + kdbClose (handle, errorKey); + ksDel (conf); + keyDel (toLookUp); + elektraFree ((void *) name); + return ret; +} diff --git a/src/tools/kdb/meta-ls.h b/src/tools/kdb/meta-ls.h new file mode 100644 index 00000000000..21600a4c423 --- /dev/null +++ b/src/tools/kdb/meta-ls.h @@ -0,0 +1,33 @@ +/** + * @file + * + * @brief Header for meta-ls command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#ifndef ELEKTRA_KDB_META_LS_H +#define ELEKTRA_KDB_META_LS_H + +#include + +/** + * Adds options specification of meta-ls command to @spec + * + * @param spec the base spec where the commands spec should be added + */ +void addMetaLsSpec (KeySet * spec); + +/** + * Executes the meta-ls command + * + * @param options cli options and arguments as specified in @addMetaLsSpec() + * @param errorKey key where errors and warnings should be saved + * + * @retval 0 ls command ran without errors + * @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info + * + */ +int execMetaLs (KeySet * options, Key * errorKey); + +#endif // ELEKTRA_KDB_META_LS_H diff --git a/src/tools/kdb/meta.c b/src/tools/kdb/meta.c index 6ba21457c49..cf9872a6877 100644 --- a/src/tools/kdb/meta.c +++ b/src/tools/kdb/meta.c @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -21,6 +22,7 @@ command metaSubcommands[] = { { "get", addMetaGetSpec, execMetaGet }, + { "ls", addMetaLsSpec, execMetaLs }, { "set", addMetaSetSpec, execMetaSet }, }; diff --git a/src/tools/kdb/metals.cpp b/src/tools/kdb/metals.cpp deleted file mode 100644 index 6360994a9d8..00000000000 --- a/src/tools/kdb/metals.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#include - -#include - -#include -#include - -using namespace kdb; -using namespace std; - -MetaLsCommand::MetaLsCommand () -{ -} - -int MetaLsCommand::execute (Cmdline const & cl) -{ - int ret = 0; - if (cl.arguments.size () != 1) - { - throw invalid_argument ("1 argument required"); - } - - Key root = cl.createKey (0); - - kdb.get (ks, root); - - Key k = ks.lookup (root); - - if (k) - { - if (cl.verbose) - { - std::cout << "Got key " << k.getName () << std::endl; - } - - 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); - if (cl.null) - { - cout << '\0' << std::flush; - } - else - { - cout << endl; - } - } - } - else - { - std::cerr << "Did not find key" << std::endl; - ret = 1; - } - - printWarnings (cerr, root, cl.verbose, cl.debug); - - return ret; -} - -MetaLsCommand::~MetaLsCommand () -{ -} diff --git a/src/tools/kdb/metals.hpp b/src/tools/kdb/metals.hpp deleted file mode 100644 index 5faf59fe8f1..00000000000 --- a/src/tools/kdb/metals.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#ifndef METALS_H -#define METALS_H - -#include -#include - -class MetaLsCommand : public Command -{ - kdb::KDB kdb; - kdb::KeySet ks; - -public: - MetaLsCommand (); - ~MetaLsCommand (); - - virtual std::string getShortOptions () override - { - return "0"; - } - - virtual std::string getSynopsis () override - { - return ""; - } - - virtual std::string getShortHelpText () override - { - return "Get all meta information of an individual key."; - } - - virtual std::string getLongHelpText () override - { - return ""; - } - - virtual int execute (Cmdline const & cmdline) override; -}; - -#endif