Skip to content

Commit 783bb0c

Browse files
committed
show: add --show-keys flag for debugging (small CLI change)
1 parent 48e6c95 commit 783bb0c

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/show.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "encoding.h"
2525
#include "subcommands.h"
2626

27+
static bool show_keys = false;
28+
2729
static int peer_cmp(const void *first, const void *second)
2830
{
2931
time_t diff;
@@ -86,6 +88,8 @@ static const char *masked_key(const uint8_t masked_key[static WG_KEY_LEN])
8688
{
8789
const char *var = getenv("WG_HIDE_KEYS");
8890

91+
if (show_keys)
92+
return key(masked_key);
8993
if (var && !strcmp(var, "never"))
9094
return key(masked_key);
9195
return "(hidden)";
@@ -382,12 +386,28 @@ int show_main(int argc, const char *argv[])
382386

383387
COMMAND_NAME = argv[0];
384388

385-
if (argc > 3) {
389+
/* Build a local argv that filters out recognized flags (currently
390+
* only `--show-keys`) so the remainder of the function can assume
391+
* positional parameters as before. */
392+
const char *local_argv[4];
393+
int local_argc = 0;
394+
395+
local_argv[0] = argv[0];
396+
local_argc = 1;
397+
for (int i = 1; i < argc; ++i) {
398+
if (!strcmp(argv[i], "--show-keys")) {
399+
show_keys = true;
400+
continue;
401+
}
402+
local_argv[local_argc++] = argv[i];
403+
}
404+
405+
if (local_argc > 3) {
386406
show_usage();
387407
return 1;
388408
}
389409

390-
if (argc == 1 || !strcmp(argv[1], "all")) {
410+
if (local_argc == 1 || !strcmp(local_argv[1], "all")) {
391411
char *interfaces = ipc_list_devices(), *interface;
392412

393413
if (!interfaces) {
@@ -403,8 +423,8 @@ int show_main(int argc, const char *argv[])
403423
fprintf(stderr, "Unable to access interface %s: %s\n", interface, strerror(errno));
404424
continue;
405425
}
406-
if (argc == 3) {
407-
if (!ugly_print(device, argv[2], true)) {
426+
if (local_argc == 3) {
427+
if (!ugly_print(device, local_argv[2], true)) {
408428
ret = 1;
409429
free_wgdevice(device);
410430
break;
@@ -418,10 +438,10 @@ int show_main(int argc, const char *argv[])
418438
ret = 0;
419439
}
420440
free(interfaces);
421-
} else if (!strcmp(argv[1], "interfaces")) {
441+
} else if (!strcmp(local_argv[1], "interfaces")) {
422442
char *interfaces, *interface;
423443

424-
if (argc > 2) {
444+
if (local_argc > 2) {
425445
show_usage();
426446
return 1;
427447
}
@@ -434,17 +454,17 @@ int show_main(int argc, const char *argv[])
434454
for (size_t len = 0; (len = strlen(interface)); interface += len + 1)
435455
printf("%s%c", interface, strlen(interface + len + 1) ? ' ' : '\n');
436456
free(interfaces);
437-
} else if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help")))
457+
} else if (local_argc == 2 && (!strcmp(local_argv[1], "-h") || !strcmp(local_argv[1], "--help") || !strcmp(local_argv[1], "help")))
438458
show_usage();
439459
else {
440460
struct wgdevice *device = NULL;
441461

442-
if (ipc_get_device(&device, argv[1]) < 0) {
462+
if (ipc_get_device(&device, local_argv[1]) < 0) {
443463
perror("Unable to access interface");
444464
return 1;
445465
}
446-
if (argc == 3) {
447-
if (!ugly_print(device, argv[2], false))
466+
if (local_argc == 3) {
467+
if (!ugly_print(device, local_argv[2], false))
448468
ret = 1;
449469
} else
450470
pretty_print(device);

0 commit comments

Comments
 (0)