From: Michael Tremer Date: Fri, 9 Jun 2023 14:14:42 +0000 (+0000) Subject: networkctl: Implement scaffolding to show ports X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3363ba36e33d5fc9c455825aeeabf4b615cde3c1;p=network.git networkctl: Implement scaffolding to show ports Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 95fff11a..2632ca73 100644 --- a/Makefile.am +++ b/Makefile.am @@ -409,12 +409,14 @@ dist_networkctl_SOURCES = \ networkctl_CFLAGS = \ $(AM_CFLAGS) \ + $(JSON_C_CFLAGS) \ $(SYSTEMD_CFLAGS) networkctl_LDFLAGS = \ $(AM_LDFLAGS) networkctl_LDADD = \ + $(JSON_C_LIBS) \ $(SYSTEMD_LIBS) # ------------------------------------------------------------------------------ diff --git a/src/networkctl/port.c b/src/networkctl/port.c index b6a95c55..439177d4 100644 --- a/src/networkctl/port.c +++ b/src/networkctl/port.c @@ -23,9 +23,11 @@ #include +#include "../networkd/json.h" #include "../networkd/string.h" #include "command.h" #include "port.h" +#include "terminal.h" typedef int (*networkctl_port_walk_callback) (sd_bus* bus, const char* path, const char* name, void* data); @@ -161,10 +163,76 @@ static int networkctl_port_list(sd_bus* bus, int argc, char* argv[]) { return networkctl_port_walk(bus, __networkctl_port_list, NULL); } +// Show + +#define SHOW_LINE " %-12s : %s\n" + +static int __networkctl_port_show(sd_bus* bus, const char* path, const char* name, void* data) { + struct json_object* object = NULL; + enum json_tokener_error json_error; + char* describe = NULL; + int r; + + // Describe this port + r = networkctl_port_describe(bus, name, &describe); + if (r < 0) + goto ERROR; + + // Parse JSON + object = json_tokener_parse_verbose(describe, &json_error); + if (!object) { + fprintf(stderr, "Could not parse port %s: %s\n", + name, json_tokener_error_desc(json_error)); + return -EINVAL; + } + + // Show headline + printf("Port %s%s%s\n", color_highlight(), name, color_reset()); + + // Show type + const char* type = json_object_fetch_string(object, "Type"); + if (type) + printf(SHOW_LINE, "Type", type); + + // Show address + const char* address = json_object_fetch_string(object, "Address"); + if (address) + printf(SHOW_LINE, "Address", address); + + // Show an empty line at the end + printf("\n"); + + // Success! + r = 0; + +ERROR: + if (describe) + free(describe); + if (object) + json_object_unref(object); + + return r; +} + +static int networkctl_port_show(sd_bus* bus, int argc, char* argv[]) { + switch (argc) { + case 0: + return networkctl_port_walk(bus, __networkctl_port_show, NULL); + + case 1: + return __networkctl_port_show(bus, NULL, argv[0], NULL); + + default: + fprintf(stderr, "Too many arguments\n"); + return 1; + } +} + int networkctl_port(sd_bus* bus, int argc, char* argv[]) { static const struct command commands[] = { { "dump", 0, networkctl_port_dump }, { "list", 0, networkctl_port_list }, + { "show", 0, networkctl_port_show }, { NULL }, }; diff --git a/src/networkd/json.h b/src/networkd/json.h index 19ced9ad..6c2f66f1 100644 --- a/src/networkd/json.h +++ b/src/networkd/json.h @@ -87,4 +87,13 @@ static inline int json_to_string(struct json_object* o, char** s, size_t* l) { return 0; } +static inline const char* json_object_fetch_string( + struct json_object* o, const char* key) { + struct json_object* e = json_object_object_get(o, key); + if (!e) + return NULL; + + return json_object_get_string(e); +} + #endif /* NETWORKD_JSON_H */