]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/networkctl/port.c
networkctl: Implement "dump" command for ports which shows the JSON
[people/ms/network.git] / src / networkctl / port.c
index 3a886ff80dd2c7bcefc5aed35293bfff8c1d7b1a..67bc003730b08c34e863032530453660796b03fe 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <limits.h>
+
 #include <systemd/sd-bus.h>
 
+#include "../networkd/string.h"
 #include "command.h"
 #include "port.h"
 
@@ -77,6 +80,53 @@ ERROR:
        return r;
 }
 
+// Dump
+
+static int networkctl_port_dump(sd_bus* bus, int argc, char* argv[]) {
+       sd_bus_message* reply = NULL;
+       sd_bus_error error = SD_BUS_ERROR_NULL;
+       char path[PATH_MAX];
+       const char* text = NULL;
+       int r;
+
+       if (argc < 1) {
+               fprintf(stderr, "Port required\n");
+               return -EINVAL;
+       }
+
+       // Make port path
+       r = nw_string_format(path, "/org/ipfire/network1/port/%s", argv[0]);
+       if (r < 0)
+               goto ERROR;
+
+       // Call Describe
+       r = sd_bus_call_method(bus, "org.ipfire.network1", path,
+               "org.ipfire.network1.Port", "Describe", &error, &reply, "");
+       if (r < 0) {
+               fprintf(stderr, "Describe() call failed: %m\n");
+               goto ERROR;
+       }
+
+       // Read the text
+       r = sd_bus_message_read(reply, "s", &text);
+       if (r < 0) {
+               fprintf(stderr, "Could not parse bus message: %s\n", strerror(-r));
+               goto ERROR;
+       }
+
+       // Print the text
+       if (text)
+               printf("%s\n", text);
+
+ERROR:
+       if (reply)
+               sd_bus_message_unref(reply);
+
+       return r;
+}
+
+// List
+
 static int __networkctl_port_list(sd_bus* bus, const char* path, const char* name, void* data) {
        printf("%s\n", name);
 
@@ -89,6 +139,7 @@ static int networkctl_port_list(sd_bus* bus, int argc, char* argv[]) {
 
 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 },
                { NULL },
        };