diff --git a/src/tools/kdb/factory.hpp b/src/tools/kdb/factory.hpp index a1ef093cc8f..4c84949ff4f 100644 --- a/src/tools/kdb/factory.hpp +++ b/src/tools/kdb/factory.hpp @@ -46,7 +46,6 @@ #include #include #include -#include #include #include #include @@ -122,7 +121,6 @@ class Factory m_factory.insert (std::make_pair ("gumount", std::make_shared> ())); m_factory.insert (std::make_pair ("list-commands", std::make_shared> ())); m_factory.insert (std::make_pair ("gen", std::make_shared> ())); - m_factory.insert (std::make_pair ("namespace", std::make_shared> ())); m_factory.insert (std::make_pair ("dirname", std::make_shared> ())); } diff --git a/src/tools/kdb/main.c b/src/tools/kdb/main.c index 28e52c12086..d22f45d0fd4 100644 --- a/src/tools/kdb/main.c +++ b/src/tools/kdb/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,7 @@ command subcommands[] = { { "basename", addBasenameSpec, execBasename }, { "get", addGetSpec, execGet }, { "ls", addLsSpec, execLs }, + { "namespace", addNamespaceSpec, execNamespace }, }; void printWarnings (Key * errorKey) diff --git a/src/tools/kdb/namespace.c b/src/tools/kdb/namespace.c new file mode 100644 index 00000000000..7b01a569bac --- /dev/null +++ b/src/tools/kdb/namespace.c @@ -0,0 +1,73 @@ +/** + * @file + * + * @brief Implementation of kdb namespace command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define COMMAND_NAME "namespace" + +#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 addNamespaceSpec (KeySet * spec) +{ + ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Get the namespace of a key.", KEY_META, + "command", COMMAND_NAME, KEY_END)); + ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/name", 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 execNamespace (KeySet * options, Key * errorKey) +{ + bool verbose = false; + Key * tmp = GET_OPTION_KEY (options, "verbose"); + if (tmp != NULL) + { + elektraKeyToBoolean (GET_OPTION_KEY (options, "verbose"), &verbose); + } + + const char * rawName = GET_OPTION (options, "name"); + bool resolved = false; + const char * name = expandKeyName (options, rawName, &resolved); + if (name == NULL) + { + ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not resolve bookmark in '%s'", rawName); + return -1; + } + + if (verbose && resolved) + { + printf ("resolved bookmark: \'%s\' -> \'%s\'\n", rawName, name); + } + + Key * key = keyNew (name, KEY_END); + if (key == NULL) + { + ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "'%s' is not a valid key name.", name); + return 1; + } + + elektraNamespace ns = keyGetNamespace (key); + if (ns != KEY_NS_NONE && ns != KEY_NS_CASCADING) + { + int pos = (int) (strchr (name, '/') - name); + printf ("%*.*s", pos, pos, name); + } + + keyDel (key); + return 0; +} diff --git a/src/tools/kdb/namespace.cpp b/src/tools/kdb/namespace.cpp deleted file mode 100644 index 51e1b36fda0..00000000000 --- a/src/tools/kdb/namespace.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#include -#include -#include - - -using namespace std; - -NamespaceCommand::NamespaceCommand () -{ -} - -int NamespaceCommand::execute (Cmdline const & cl) -{ - return executeNamepartcommand (cl, keyNamespace); -} - -NamespaceCommand::~NamespaceCommand () -{ -} diff --git a/src/tools/kdb/namespace.h b/src/tools/kdb/namespace.h new file mode 100644 index 00000000000..cb18453041e --- /dev/null +++ b/src/tools/kdb/namespace.h @@ -0,0 +1,33 @@ +/** + * @file + * + * @brief Header for namespace command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#ifndef ELEKTRA_KDB_NAMESPACE_H +#define ELEKTRA_KDB_NAMESPACE_H + +#include + +/** + * Adds options specification of namespace command to @spec + * + * @param spec the base spec where the commands spec should be added + */ +void addNamespaceSpec (KeySet * spec); + +/** + * Executes the namespace command + * + * @param options cli options and arguments as specified in @addNamespaceSpec() + * @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 execNamespace (KeySet * options, Key * errorKey); + +#endif // ELEKTRA_KDB_NAMESPACE_H diff --git a/src/tools/kdb/namespace.hpp b/src/tools/kdb/namespace.hpp deleted file mode 100644 index 38959854463..00000000000 --- a/src/tools/kdb/namespace.hpp +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#ifndef NAMESPACE_HPP -#define NAMESPACE_HPP - -#include "coloredkdbio.hpp" -#include -#include - -class NamespaceCommand : public Command -{ - kdb::KDB kdb; - -public: - NamespaceCommand (); - ~NamespaceCommand (); - - virtual std::string getShortOptions () override - { - return "n"; - } - - virtual std::string getSynopsis () override - { - return ""; - } - - virtual std::string getShortHelpText () override - { - return "Get the namespace of a key (trailing \':\' included)."; - } - - virtual std::string getLongHelpText () override - { - return "For example, \"kdb namespace user:/key\" will yield \"user:\",\n" - "and, \"kdb namespace /key\" will yield the empty string.\n"; - } - - virtual int execute (Cmdline const & cmdline) override; -}; - -#endif