-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
163 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @file | ||
* | ||
* @brief KDB set subcommand | ||
* | ||
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org) | ||
*/ | ||
|
||
#include <colors.h> | ||
#include <command.h> | ||
#include <kdbassert.h> | ||
#include <kdbease.h> | ||
#include <kdberrors.h> | ||
#include <set.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define COMMAND_NAME "set" | ||
|
||
#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 addSetSpec (KeySet * spec) | ||
{ | ||
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Set the value of an individual key.", | ||
KEY_META, "command", COMMAND_NAME, KEY_END)); | ||
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/force", KEY_META, "description", "Force setting the value", KEY_META, | ||
"opt", "f", KEY_META, "opt/long", "force", KEY_META, "opt/arg", "none", 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)); | ||
|
||
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/value", KEY_META, "description", "The value that should be set", | ||
KEY_META, "args", "indexed", KEY_META, "args/index", "1", KEY_END)); | ||
|
||
ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME)) | ||
} | ||
|
||
int execSet (KeySet * options, Key * errorKey) | ||
{ | ||
int ret = 0; | ||
GET_BASIC_OPTIONS | ||
|
||
bool force = false; | ||
tmp = GET_OPTION_KEY (options, "force"); | ||
if (tmp != NULL) | ||
{ | ||
elektraKeyToBoolean (GET_OPTION_KEY (options, "force"), &force); | ||
} | ||
|
||
const char * name = getKeyNameFromOptions (GET_OPTION (options, "name"), errorKey, verbose); | ||
if (name == NULL) | ||
{ | ||
RETURN (2) | ||
} | ||
|
||
const char * value = GET_OPTION (options, "value"); | ||
|
||
Key * parentKey = keyNew (name, KEY_END); | ||
|
||
if (keyGetNamespace (parentKey) == KEY_NS_NONE || keyGetNamespace (parentKey) == KEY_NS_CASCADING) | ||
{ | ||
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, "key does not specify a namespace"); | ||
elektraFree ((void *) name); | ||
keyDel (parentKey); | ||
RETURN (2) | ||
} | ||
|
||
Key * maybeCascadingParent = keyDup (parentKey, KEY_CP_NAME); | ||
if (!force) | ||
{ | ||
keySetNamespace (maybeCascadingParent, KEY_NS_CASCADING); | ||
} | ||
KeySet * conf = ksNew (0, KS_END); | ||
KDB * handle = kdbOpen (NULL, errorKey); | ||
|
||
if (kdbGet (handle, conf, maybeCascadingParent) == -1) | ||
{ | ||
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not load '%s': %s", keyName (parentKey), | ||
GET_ERR (maybeCascadingParent)); | ||
ret = 5; | ||
goto cleanup; | ||
} | ||
keyCopyAllMeta (errorKey, parentKey); | ||
|
||
Key * key = ksLookup (conf, parentKey, KDB_O_NONE); | ||
if (key == NULL) | ||
{ | ||
key = keyNew (name, KEY_END); | ||
ksAppendKey (conf, key); | ||
CLI_PRINT (CLI_LOG_NONE, "Create a new key %s with string \"%s\"", keyName (key), value); | ||
} | ||
else | ||
{ | ||
CLI_PRINT (CLI_LOG_NONE, "Set string to \"%s\"", value); | ||
} | ||
keySetString (key, value); // can't fail, since neither value or key can be null | ||
|
||
if (kdbSet (handle, conf, parentKey) == -1) | ||
{ | ||
ret = 5; | ||
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not set value for '%s': %s", name, GET_ERR (parentKey)); | ||
} | ||
keyCopyAllMeta (errorKey, parentKey); | ||
|
||
keyDel (key); | ||
|
||
cleanup: | ||
if (!noNewLine) | ||
{ | ||
printf ("\n"); | ||
} | ||
elektraFree ((void *) name); | ||
keyDel (parentKey); | ||
keyDel (maybeCascadingParent); | ||
ksDel (conf); | ||
kdbClose (handle, errorKey); | ||
|
||
RETURN (ret) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @file | ||
* | ||
* @brief KDB set subcommand header | ||
* | ||
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org) | ||
*/ | ||
|
||
#ifndef ELEKTRA_KDB_SET_H | ||
#define ELEKTRA_KDB_SET_H | ||
|
||
#include <kdb.h> | ||
|
||
/** | ||
* Adds options specification of set command to keySet | ||
* | ||
* @param spec the base spec where the commands spec should be added | ||
*/ | ||
void addSetSpec (KeySet * spec); | ||
|
||
/** | ||
* Runs the set command | ||
* | ||
* @param options cli options and arguments as specified in addSetSpec() | ||
* @param errorKey key where errors and warnings should be saved | ||
* | ||
* @retval 0 set command ran without errors | ||
* @retval 1 errors occurred, keySetMeta (errorKey, "error/reason") for info | ||
* | ||
*/ | ||
int execSet (KeySet * options, Key * errorKey); | ||
|
||
#endif // ELEKTRA_KDB_SET_H |
Oops, something went wrong.