diff --git a/src/config.c b/src/config.c index d981152..866544a 100644 --- a/src/config.c +++ b/src/config.c @@ -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); +} diff --git a/src/config.h b/src/config.h index 64f9599..0983340 100644 --- a/src/config.h +++ b/src/config.h @@ -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 */ diff --git a/src/main.c b/src/main.c index 0f45d8d..7740c57 100644 --- a/src/main.c +++ b/src/main.c @@ -22,12 +22,10 @@ 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("Usage: %s [options]\n", PACKAGE_NAME); + printf( "Options are:\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; diff --git a/tests/config.test.c b/tests/config.test.c index ec077ad..1828512 100644 --- a/tests/config.test.c +++ b/tests/config.test.c @@ -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 } diff --git a/yaip b/yaip new file mode 100755 index 0000000..cee142c Binary files /dev/null and b/yaip differ