Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
kdb-cli: rewrite namespace command
Browse files Browse the repository at this point in the history
... and remove cpp implementation
  • Loading branch information
hannes99 committed Sep 23, 2022
1 parent 38873a0 commit 7641926
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 77 deletions.
2 changes: 0 additions & 2 deletions src/tools/kdb/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <metaset.hpp>
#include <mount.hpp>
#include <mv.hpp>
#include <namespace.hpp>
#include <plugincheck.hpp>
#include <plugininfo.hpp>
#include <pluginlist.hpp>
Expand Down Expand Up @@ -122,7 +121,6 @@ class Factory
m_factory.insert (std::make_pair ("gumount", std::make_shared<Cnstancer<GlobalUmountCommand>> ()));
m_factory.insert (std::make_pair ("list-commands", std::make_shared<Cnstancer<ListCommandsCommand>> ()));
m_factory.insert (std::make_pair ("gen", std::make_shared<Cnstancer<GenCommand>> ()));
m_factory.insert (std::make_pair ("namespace", std::make_shared<Cnstancer<NamespaceCommand>> ()));
m_factory.insert (std::make_pair ("dirname", std::make_shared<Cnstancer<DirnameCommand>> ()));
}

Expand Down
2 changes: 2 additions & 0 deletions src/tools/kdb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <basename.h>
#include <get.h>
#include <ls.h>
#include <namespace.h>

#include <command.h>
#include <kdb.h>
Expand All @@ -26,6 +27,7 @@ command subcommands[] = {
{ "basename", addBasenameSpec, execBasename },
{ "get", addGetSpec, execGet },
{ "ls", addLsSpec, execLs },
{ "namespace", addNamespaceSpec, execNamespace },
};

void printWarnings (Key * errorKey)
Expand Down
73 changes: 73 additions & 0 deletions src/tools/kdb/namespace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @file
*
* @brief Implementation of kdb namespace command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <command.h>
#include <namespace.h>

#include <kdb.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <stdio.h>
#include <string.h>

#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;
}
27 changes: 0 additions & 27 deletions src/tools/kdb/namespace.cpp

This file was deleted.

33 changes: 33 additions & 0 deletions src/tools/kdb/namespace.h
Original file line number Diff line number Diff line change
@@ -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 <kdb.h>

/**
* 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
48 changes: 0 additions & 48 deletions src/tools/kdb/namespace.hpp

This file was deleted.

0 comments on commit 7641926

Please sign in to comment.