Updates how config options are set on command line

master
Jonathan Hodgson 3 years ago
parent 8bed39700e
commit f392af92c3
  1. 18
      src/config.c
  2. 2
      src/config.h
  3. 29
      src/main.c
  4. 25
      tests/config.test.c
  5. BIN
      yaip

@ -63,3 +63,21 @@ char* getDefaultUserConfigLoc(){
return strdup(configFile);
}
void setConfig(Config *config, char option[], char value[]){
if ( strcmp( option, "database" ) == 0 ){
config->database = value;
} else if ( strcmp( option, "localConfig" ) == 0 ){
config->localConfig = value;
} else if ( strcmp( option, "userConfig" ) == 0 ){
config->userConfig = value;
} else if ( strcmp( option, "port" ) == 0 ){
config->port = atoi(value);
}
}
void printConfig(Config *config){
printf("Database: %s\n", config->database);
printf("localConfig: %s\n", config->localConfig);
printf("userConfig: %s\n", config->userConfig);
printf("port: %i\n", config->port);
}

@ -20,5 +20,7 @@ static char* resolveTilde(const char *path);
Config* configDefaults();
char* getConfigDir();
char* getDefaultUserConfigLoc();
void setConfig(Config *config, char option[], char value[]);
void printConfig(Config *config);
#endif /* ifndef CONFIG_H */

@ -24,10 +24,8 @@ unsigned int validatePortNumber(unsigned int portNumber){
void printHelp(){
printf("Usage: %s [options]\n", PACKAGE_NAME);
printf( "Options are:\n" );
printf( "\t --database DATABASE A sqlite3 database file (Defatult: %s)\n", DEFAULT_DATABASE );
printf( "\t --config CONFIG A config file (Default: %s)\n", DEFAULT_CONFIG );
printf( "\t --port PORT The listening port (Default: %i)\n", DEFAULT_PORT );
printf( "\t --help Display version information.\n");
printf( "\t --config OPTION VALUE Sets config option OPTION to VALUE\n" );
printf( "\t --print-config Prints current config\n" );
}
int main(int argc, char**argv){
@ -39,24 +37,15 @@ int main(int argc, char**argv){
for ( unsigned int i = 1; i < argc; i++ ){
if ( strcmp( argv[i], "--database" ) == 0 ){
if ( i + 1 < argc ){
database = argv[++i];
if ( strcmp( argv[i], "--config" ) == 0 ){
if ( i + 2 < argc ){
setConfig( defaultconfig, argv[i + 1], argv[i + 2] );
} else {
printf("--database requires a positional argument\n");
}
} else if ( strcmp( argv[i], "--config" ) == 0 ){
if ( i + 1 < argc ){
config = argv[++i];
} else {
printf("--config requires a positional argument\n");
}
} else if ( strcmp( argv[i], "--port" ) == 0 ){
if ( i + 1 < argc ){
port = validatePortNumber( atoi(argv[++i]) );
} else {
printf("--port requires a positional argument\n");
printf("--config requires 2 positional arguments\n");
}
} else if ( strcmp( argv[i], "--print-config" ) == 0 ){
printConfig(defaultconfig);
return 0;
} else if ( strcmp( argv[i], "--help" ) == 0 ){
printHelp();
return 0;

@ -63,6 +63,23 @@ MunitResult checkDefaults(const MunitParameter params[],
return MUNIT_OK;
}
MunitResult checkSetConfig(const MunitParameter params[],
void* user_data_or_fixture){
Config *conf = configDefaults();
setConfig(conf, "database", "test.blar");
munit_assert_string_equal(conf->database, "test.blar");
setConfig(conf, "localConfig", "test.blar");
munit_assert_string_equal(conf->localConfig, "test.blar");
setConfig(conf, "userConfig", "test.blar");
munit_assert_string_equal(conf->userConfig, "test.blar");
setConfig(conf, "port", "1234");
munit_assert_int(conf->port, ==, 1234);
return MUNIT_OK;
}
MunitTest tests[] = {
{
"/path_exists/yes", /* name */
@ -112,6 +129,14 @@ MunitTest tests[] = {
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
},
{
"/ConfigDir/SetConfigs", /* name */
checkSetConfig, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
},
/* Mark the end of the array with an entry where the test
* function is NULL */
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }

BIN
yaip

Binary file not shown.
Loading…
Cancel
Save