From f392af92c3a2072fa98d625e52f453b6fc1f2c8f Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Tue, 28 Dec 2021 10:00:41 +0000 Subject: [PATCH] Updates how config options are set on command line --- src/config.c | 18 ++++++++++++++++++ src/config.h | 2 ++ src/main.c | 33 +++++++++++---------------------- tests/config.test.c | 25 +++++++++++++++++++++++++ yaip | Bin 0 -> 22648 bytes 5 files changed, 56 insertions(+), 22 deletions(-) create mode 100755 yaip 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 0000000000000000000000000000000000000000..cee142cddf121b11ff3de469dac510a9dc35b908 GIT binary patch literal 22648 zcmeHPdvsLQx!*~MM3iJe(12A=RV-K%UP=LrW|9n?xDtp!&{koX%uLAWWM-T>1Bt63 zb_|#4bZn~E)@yZDuU)Nu+}^a@QsjyT@e=BFwUo9ty`?qMSZ55i!B~qe(%hkBLxTuJ!RKl)f#4VaGkQyl%ZI%ZhHEcPX zir-7wCF~sVlQ<^iHG)7Y(m-)OEt7Z&DA`rg!8r6|0%nS;5)vglyVN^XNEBrb!INDn z9ZJ5C&YULn6jgFFzePgM3g~VM0Ft6sI}#17;$k+?luw&wy$2*!b`&nM%SgM7v{Q6M z20~F4Pl^fsEtmcZX$@qQYWOc>g?63Nu8`JCeMKn`N*i7=e1A`V&C;%SiSScMpAs-r zRONRg>}05=#zzy?%CV6(g`MnMZvD+AuWFCzzP;C+y=KL| zlP^B@gXc(x>Onq9hZ14*rxF$aO8k)ykB=w-uNC1d-bkq-1n$G>Tc1JK^_?No9!Ensa6z7jdJ7U{RHXQMXSS;+l z-EXj%5%os8Si2Dp1^I7`wQ+d_M&T&r9x4XUVx4YXsfglS~dEwBA z`r;8HLE`)&AM5aU#E{3fV7L`#a2N`^gJG}72!undH2{sM-{WI#qO={JAcSn2AKtdZ z4`LBzZ9Af<5c3$}0Begx10kc0dAweK#DGU{r^nqE2zi2mJ5gEGq$2_p6S!A~s>2fq z@$B28em@G@DkDPE`6A-IbhM1EX>6#ib1$i0@+tFh=~DCXYV&AG^)=LL#qysx62B!T zDx^wAbGFh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