diff --git a/.github/.codespellignore b/.github/.codespellignore index 9d8b4f3ffc..3a366c7b5c 100644 --- a/.github/.codespellignore +++ b/.github/.codespellignore @@ -6,3 +6,4 @@ doubleclick requestor requestors punycode +Bitap diff --git a/src/config/cli.c b/src/config/cli.c index dc9264628e..b89bcfa7dd 100644 --- a/src/config/cli.c +++ b/src/config/cli.c @@ -23,6 +23,15 @@ // suggest_closest_conf_key() #include "config/suggest.h" +enum exit_codes { + OKAY = 0, + FAIL = 1, + VALUE_INVALID = 2, + DNSMASQ_TEST_FAILED = 3, + KEY_UNKNOWN = 4, + ENV_VAR_FORCED = 5, +} __attribute__((packed)); + // Read a TOML value from a table depending on its type static bool readStringValue(struct conf_item *conf_item, const char *value, struct config *newconf) { @@ -372,7 +381,7 @@ int set_config_from_CLI(const char *key, const char *value) { log_err("Config option %s is read-only (set via environmental variable)", key); free_config(&newconf); - return 5; + return ENV_VAR_FORCED; } // This is the config option we are looking for @@ -396,14 +405,14 @@ int set_config_from_CLI(const char *key, const char *value) free(matches); free_config(&newconf); - return 4; + return KEY_UNKNOWN; } // Parse value if(!readStringValue(new_item, value, &newconf)) { free_config(&newconf); - return 2; + return VALUE_INVALID; } // Check if value changed compared to current value @@ -423,7 +432,7 @@ int set_config_from_CLI(const char *key, const char *value) // Test failed log_debug(DEBUG_CONFIG, "Config item %s: dnsmasq config test failed", conf_item->k); free_config(&newconf); - return 3; + return DNSMASQ_TEST_FAILED; } } else if(conf_item == &config.dns.hosts) @@ -453,7 +462,7 @@ int set_config_from_CLI(const char *key, const char *value) putchar('\n'); writeFTLtoml(false); - return EXIT_SUCCESS; + return OKAY; } int get_config_from_CLI(const char *key, const bool quiet) @@ -498,13 +507,13 @@ int get_config_from_CLI(const char *key, const bool quiet) log_err(" - %s", matches[i]); free(matches); - return 4; + return KEY_UNKNOWN; } // Use return status if this is a boolean value // and we are in quiet mode if(quiet && conf_item->t == CONF_BOOL) - return conf_item->v.b ? EXIT_SUCCESS : EXIT_FAILURE; + return conf_item->v.b ? OKAY : FAIL; - return EXIT_SUCCESS; + return OKAY; } diff --git a/test/test_suite.bats b/test/test_suite.bats index c280a766e3..fed16ce354 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -1280,9 +1280,21 @@ } @test "Unknown environmental variable is logged, a useful alternative is suggested" { - run bash -c 'grep -q "FTLCONF_dns_upstrrr is unknown" /var/log/pihole/FTL.log' + run bash -c 'grep -A1 "FTLCONF_dns_upstrrr is unknown" /var/log/pihole/FTL.log' printf "%s\n" "${lines[@]}" - [[ $status == 0 ]] + [[ ${lines[0]} == *"WARNING: [?] FTLCONF_dns_upstrrr is unknown, did you mean any of these?" ]] + [[ ${lines[1]} == *"WARNING: - FTLCONF_dns_upstreams" ]] +} + +@test "CLI complains about unknown config key and offers a suggestion" { + run bash -c './pihole-FTL --config dbg.all' + [[ ${lines[0]} == "Unknown config option dbg.all, did you mean:" ]] + [[ ${lines[1]} == " - debug.all" ]] + [[ $status == 4 ]] + run bash -c './pihole-FTL --config misc.privacyLLL' + [[ ${lines[0]} == "Unknown config option misc.privacyLLL, did you mean:" ]] + [[ ${lines[1]} == " - misc.privacylevel" ]] + [[ $status == 4 ]] } @test "Changing a config option set forced by ENVVAR is not possible via the CLI" {