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 meta-rm
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes99 committed Mar 1, 2023
1 parent 97a7c5c commit df939f1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 103 deletions.
2 changes: 0 additions & 2 deletions src/tools/kdb/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <listcommands.hpp>
#include <memory>
#include <merge.hpp>
#include <metaremove.hpp>
#include <mount.hpp>
#include <mv.hpp>
#include <plugincheck.hpp>
Expand Down Expand Up @@ -88,7 +87,6 @@ class Factory
m_factory.insert (std::make_pair ("remount", std::make_shared<Cnstancer<RemountCommand>> ()));
m_factory.insert (std::make_pair ("shell", std::make_shared<Cnstancer<ShellCommand>> ()));
m_factory.insert (std::make_pair ("find", std::make_shared<Cnstancer<FindCommand>> ()));
m_factory.insert (std::make_pair ("meta-rm", std::make_shared<Cnstancer<MetaRemoveCommand>> ()));
m_factory.insert (std::make_pair ("plugin-info", std::make_shared<Cnstancer<PluginInfoCommand>> ()));
m_factory.insert (std::make_pair ("test", std::make_shared<Cnstancer<TestCommand>> ()));
m_factory.insert (std::make_pair ("plugin-check", std::make_shared<Cnstancer<PluginCheckCommand>> ()));
Expand Down
84 changes: 84 additions & 0 deletions src/tools/kdb/meta-rm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @file
*
* @brief Implementation of kdb meta-rm command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <meta-rm.h>

#include <command.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define COMMAND_NAME "meta/rm"

#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 addMetaRmSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Remove a metakey.", KEY_META, "command", "rm",
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));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/metaname", KEY_META, "description", "The meta name", KEY_META, "args",
"indexed", KEY_META, "args/index", "1", KEY_END));

ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME))
}

int execMetaRm (KeySet * options, Key * errorKey)
{
int ret = 0;
GET_BASIC_OPTIONS

const char * keyName = getKeyNameFromOptions (options, GET_OPTION (options, "keyname"), errorKey, verbose);
if (keyName == NULL) return 1;

const char * metaName = GET_OPTION (options, "metaname");

Key * parentKey = keyNew (keyName, KEY_END);

KeySet * conf = ksNew (0, KS_END);
KDB * handle = kdbOpen (NULL, errorKey);

if (kdbGet (handle, conf, parentKey) == -1)
{
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not load '%s': %s", keyName, GET_ERR (parentKey));
ret = 1;
goto cleanup;
}

Key * key = ksLookup (conf, parentKey, KDB_O_NONE);
if (key == NULL)
{
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not load '%s': %s", keyName, GET_ERR (parentKey));
ret = 1;
goto cleanup;
}

if (keySetMeta (key, metaName, NULL) != 0 || kdbSet (handle, conf, parentKey) == -1)
{
ret = 1;
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not remove meta-key '%s' for '%s': %s", metaName, keyName, GET_ERR (parentKey));
}

keyDel (key);

cleanup:
if (!noNewLine)
{
printf ("\n");
}
elektraFree ((void*) keyName);
keyDel (parentKey);
ksDel (conf);
kdbClose (handle, errorKey);
return ret;
}
33 changes: 33 additions & 0 deletions src/tools/kdb/meta-rm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file
*
* @brief Header for meta-rm command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#ifndef ELEKTRA_KDB_META_RM_H
#define ELEKTRA_KDB_META_RM_H

#include <kdb.h>

/**
* Adds options specification of meta-rm command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
void addMetaRmSpec (KeySet * spec);

/**
* Executes the meta-rm command
*
* @param options cli options and arguments as specified in @addMetaRmSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 rm command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
int execMetaRm (KeySet * options, Key * errorKey);

#endif // ELEKTRA_KDB_META_RM_H
2 changes: 2 additions & 0 deletions src/tools/kdb/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <meta.h>
#include <meta-get.h>
#include <meta-ls.h>
#include <meta-rm.h>
#include <meta-set.h>
#include <meta-show.h>

Expand All @@ -24,6 +25,7 @@
command metaSubcommands[] = {
{ "get", addMetaGetSpec, execMetaGet },
{ "ls", addMetaLsSpec, execMetaLs },
{ "rm", addMetaRmSpec, execMetaRm },
{ "set", addMetaSetSpec, execMetaSet },
{ "show", addMetaShowSpec, execMetaShow },
};
Expand Down
54 changes: 0 additions & 54 deletions src/tools/kdb/metaremove.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions src/tools/kdb/metaremove.hpp

This file was deleted.

0 comments on commit df939f1

Please sign in to comment.