Updates how config options are set on command line
This commit is contained in:
parent
8bed39700e
commit
f392af92c3
5 changed files with 56 additions and 22 deletions
18
src/config.c
18
src/config.c
|
@ -63,3 +63,21 @@ char* getDefaultUserConfigLoc(){
|
||||||
return strdup(configFile);
|
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();
|
Config* configDefaults();
|
||||||
char* getConfigDir();
|
char* getConfigDir();
|
||||||
char* getDefaultUserConfigLoc();
|
char* getDefaultUserConfigLoc();
|
||||||
|
void setConfig(Config *config, char option[], char value[]);
|
||||||
|
void printConfig(Config *config);
|
||||||
|
|
||||||
#endif /* ifndef CONFIG_H */
|
#endif /* ifndef CONFIG_H */
|
||||||
|
|
29
src/main.c
29
src/main.c
|
@ -24,10 +24,8 @@ unsigned int validatePortNumber(unsigned int portNumber){
|
||||||
void printHelp(){
|
void printHelp(){
|
||||||
printf("Usage: %s [options]\n", PACKAGE_NAME);
|
printf("Usage: %s [options]\n", PACKAGE_NAME);
|
||||||
printf( "Options are:\n" );
|
printf( "Options are:\n" );
|
||||||
printf( "\t --database DATABASE A sqlite3 database file (Defatult: %s)\n", DEFAULT_DATABASE );
|
printf( "\t --config OPTION VALUE Sets config option OPTION to VALUE\n" );
|
||||||
printf( "\t --config CONFIG A config file (Default: %s)\n", DEFAULT_CONFIG );
|
printf( "\t --print-config Prints current config\n" );
|
||||||
printf( "\t --port PORT The listening port (Default: %i)\n", DEFAULT_PORT );
|
|
||||||
printf( "\t --help Display version information.\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char**argv){
|
int main(int argc, char**argv){
|
||||||
|
@ -39,24 +37,15 @@ int main(int argc, char**argv){
|
||||||
|
|
||||||
|
|
||||||
for ( unsigned int i = 1; i < argc; i++ ){
|
for ( unsigned int i = 1; i < argc; i++ ){
|
||||||
if ( strcmp( argv[i], "--database" ) == 0 ){
|
if ( strcmp( argv[i], "--config" ) == 0 ){
|
||||||
if ( i + 1 < argc ){
|
if ( i + 2 < argc ){
|
||||||
database = argv[++i];
|
setConfig( defaultconfig, argv[i + 1], argv[i + 2] );
|
||||||
} else {
|
} else {
|
||||||
printf("--database requires a positional argument\n");
|
printf("--config requires 2 positional arguments\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");
|
|
||||||
}
|
}
|
||||||
|
} else if ( strcmp( argv[i], "--print-config" ) == 0 ){
|
||||||
|
printConfig(defaultconfig);
|
||||||
|
return 0;
|
||||||
} else if ( strcmp( argv[i], "--help" ) == 0 ){
|
} else if ( strcmp( argv[i], "--help" ) == 0 ){
|
||||||
printHelp();
|
printHelp();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -63,6 +63,23 @@ MunitResult checkDefaults(const MunitParameter params[],
|
||||||
return MUNIT_OK;
|
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[] = {
|
MunitTest tests[] = {
|
||||||
{
|
{
|
||||||
"/path_exists/yes", /* name */
|
"/path_exists/yes", /* name */
|
||||||
|
@ -112,6 +129,14 @@ MunitTest tests[] = {
|
||||||
MUNIT_TEST_OPTION_NONE, /* options */
|
MUNIT_TEST_OPTION_NONE, /* options */
|
||||||
NULL /* parameters */
|
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
|
/* Mark the end of the array with an entry where the test
|
||||||
* function is NULL */
|
* function is NULL */
|
||||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
|
|
BIN
yaip
Executable file
BIN
yaip
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue