]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkctl: Implement scaffolding to show ports
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 14:14:42 +0000 (14:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 9 Jun 2023 14:14:42 +0000 (14:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkctl/port.c
src/networkd/json.h

index 95fff11af0f063f091ff0908dba6aae75943f12d..2632ca73daa09c390cff4c2b412b9e10582a68e1 100644 (file)
@@ -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)
 
 # ------------------------------------------------------------------------------
index b6a95c55265c148c65dd525e2e98984576036558..439177d415cf8176786ed56ec27efceba7ecb3a4 100644 (file)
 
 #include <systemd/sd-bus.h>
 
+#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 },
        };
 
index 19ced9adf81ce542913242934918de2b43d94264..6c2f66f19fca0c2923bf075d58a3f609a150f32a 100644 (file)
@@ -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 */