Skip to content

Commit

Permalink
Standardize CLI --config exit codes and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 1, 2023
1 parent 09df180 commit 4ef87a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/.codespellignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ doubleclick
requestor
requestors
punycode
Bitap
25 changes: 17 additions & 8 deletions src/config/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
16 changes: 14 additions & 2 deletions test/test_suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 4ef87a2

Please sign in to comment.