diff --git a/src/tools/kdb/basename.c b/src/tools/kdb/basename.c new file mode 100644 index 00000000000..a2c8f0c8f03 --- /dev/null +++ b/src/tools/kdb/basename.c @@ -0,0 +1,65 @@ +/** + * @file + * + * @brief Implementation of kdb basename command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define COMMAND_NAME "basename" + +#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 addBasenameSpec (KeySet * spec) +{ + ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Get the basename 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 execBasename (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 avlid key name.", name); + return 1; + } + printf ("%s", keyBaseName (key)); + return 0; +} diff --git a/src/tools/kdb/basename.cpp b/src/tools/kdb/basename.cpp deleted file mode 100644 index d66a6015ce9..00000000000 --- a/src/tools/kdb/basename.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; - -BasenameCommand::BasenameCommand () -{ -} - -int BasenameCommand::execute (Cmdline const & cl) -{ - return executeNamepartcommand (cl, keyBasename); -} - -BasenameCommand::~BasenameCommand () -{ -} diff --git a/src/tools/kdb/basename.h b/src/tools/kdb/basename.h new file mode 100644 index 00000000000..3a2262a540e --- /dev/null +++ b/src/tools/kdb/basename.h @@ -0,0 +1,33 @@ +/** + * @file + * + * @brief Header for basename command + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#ifndef ELEKTRA_KDB_BASENAME_H +#define ELEKTRA_KDB_BASENAME_H + +#include + +/** + * Adds options specification of basename command to @spec + * + * @param spec the base spec where the commands spec should be added + */ +void addBasenameSpec (KeySet * spec); + +/** + * Executes the basename command + * + * @param options cli options and arguments as specified in @addBasenameSpec() + * @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 execBasename (KeySet * options, Key * errorKey); + +#endif // ELEKTRA_KDB_BASENAME_H diff --git a/src/tools/kdb/basename.hpp b/src/tools/kdb/basename.hpp deleted file mode 100644 index db008303330..00000000000 --- a/src/tools/kdb/basename.hpp +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file - * - * @brief - * - * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) - */ - -#ifndef BASENAME_HPP -#define BASENAME_HPP - -#include "coloredkdbio.hpp" -#include -#include - -class BasenameCommand : public Command -{ - kdb::KDB kdb; - -public: - BasenameCommand (); - ~BasenameCommand (); - - virtual std::string getShortOptions () override - { - return "n"; - } - - virtual std::string getSynopsis () override - { - return ""; - } - - virtual std::string getShortHelpText () override - { - return "Get the basename of a key."; - } - - virtual std::string getLongHelpText () override - { - return "For example, \"kdb basename user:/key/subkey\" will yield \"subkey\".\n"; - } - - virtual int execute (Cmdline const & cmdline) override; -}; - -#endif diff --git a/src/tools/kdb/factory.hpp b/src/tools/kdb/factory.hpp index 8d020221a4c..a1ef093cc8f 100644 --- a/src/tools/kdb/factory.hpp +++ b/src/tools/kdb/factory.hpp @@ -23,7 +23,6 @@ #include // TODO: to add a new command, 1.) include your header here -#include #include #include #include @@ -124,7 +123,6 @@ class Factory 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 ("basename", 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 8e97ad48d39..28e52c12086 100644 --- a/src/tools/kdb/main.c +++ b/src/tools/kdb/main.c @@ -6,6 +6,7 @@ * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) */ +#include #include #include @@ -22,6 +23,7 @@ extern char ** environ; command subcommands[] = { + { "basename", addBasenameSpec, execBasename }, { "get", addGetSpec, execGet }, { "ls", addLsSpec, execLs }, };