/* edit_config.c -- eurephiadm command: * Sets, deletes or prints a config setting in the database * * GPLv2 - Copyright (C) 2008 David Sommerseth * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ #include #include #include #include #include #include #include #include #include #include #include #define MODULE "eurephiadm::config" #include "../argparser.h" void help_EditConfig() { printf("%s - Edit eurephia configuration\n\n", MODULE); printf(" The config command let you add, change or delete\n" " configuration parameters in the eurephia database\n" " in an easy way. To be allowed to do so, you must\n" " have been granted 'config' access.\n\n"); printf(" The following arguments are accepted:\n" " -l | --list List all parameters in db\n" " -s | --set Add/change a parameter.\n" " -D | --delete Remove the parameter.\n" "\n" " If no arguments is given, you can give a key name, and the\n" " associated value will be printed.\n\n" " eurephiadm config \n\n"); printf(" The command will exit with exit code 0 on success.\n\n"); } // Reusing this function for the --list argument int cmd_ShowCfg(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv); int cmd_EditConfig(eurephiaCTX *ctx, eurephiaSESSION *sess, eurephiaVALUES *cfg, int argc, char **argv) { int rc = 0, i = 0; e_options editargs[] = { {"--set", "-s", 2}, {"--delete", "-D", 1}, {"--list", "-l", 0}, {"--help", "-h", 0}, {NULL, NULL, 0} }; assert((ctx != NULL) && (ctx->dbc != NULL) && (ctx->dbc->config != NULL)); for( i = 1; i < argc; i++ ) { if( *argv[i] != '-' ) { break; } switch( eurephia_getopt(&i, argc, argv, editargs) ) { case 's': fprintf(stderr, "Setting config parameter '%s' to '%s'\n", optargs[0], optargs[1]); rc = eDBadminConfigSet(ctx, optargs[0], optargs[1]); break; case 'D': fprintf(stderr, "Deleting config parameter '%s'\n", optargs[0]); rc = eDBadminConfigDelete(ctx, optargs[0]); break; case 'l': cmd_ShowCfg(ctx, NULL, NULL, 0, NULL); return 0; case 'h': help_EditConfig(); default: return 1; } } if( argc == 2 ) { char *val = eGet_value(ctx->dbc->config, argv[1]); fprintf(stdout, "%s\n", (val != NULL ? val : "")); rc = 1; } else if( argc == 1 ){ fprintf(stderr, "No config action performed\n"); rc = 1; } if( rc != 1 ) { fprintf(stderr, "Failed to update the configuration\n"); } return (rc != 1); }