#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);
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 },
};