diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 2b7491d1db56d..6ab360f5c08cc 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -2543,6 +2543,7 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; _cleanup_(link_info_array_freep) LinkInfo *links = NULL; _cleanup_(table_unrefp) Table *table = NULL; + _cleanup_(json_variant_unrefp) JsonVariant *json = NULL; TableCell *cell; int neighbors_count = 0; uint16_t capabilities_all = 0; @@ -2563,23 +2564,30 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { pager_open(arg_pager_flags); - table = table_new("link", - "chassis-id", - "system-name", - "caps", - "port-id", - "port-description"); - if (!table) - return log_oom(); + if (arg_json_format_flags == JSON_FORMAT_OFF) { + table = table_new("link", + "chassis-id", + "system-name", + "caps", + "port-id", + "port-description"); + if (!table) + return log_oom(); - if (arg_full) - table_set_width(table, 0); + if (arg_full) + table_set_width(table, 0); - table_set_header(table, arg_legend); + table_set_header(table, arg_legend); - assert_se(cell = table_get_cell(table, 0, 3)); - table_set_minimum_width(table, cell, 11); - table_set_ersatz_string(table, TABLE_ERSATZ_DASH); + assert_se(cell = table_get_cell(table, 0, 3)); + table_set_minimum_width(table, cell, 11); + table_set_ersatz_string(table, TABLE_ERSATZ_DASH); + } else { + r = json_build(&json, JSON_BUILD_EMPTY_OBJECT); + if (r < 0) + return r; + + } FOREACH_ARRAY(link, links, c) { _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; @@ -2610,30 +2618,59 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { if (r < 0) return r; - _cleanup_free_ char *capabilities = lldp_capabilities_to_string(lldp.capabilities, /* dots = */ true); + _cleanup_free_ char *capabilities = lldp_capabilities_to_string(lldp.capabilities, /* dots = */ arg_json_format_flags == JSON_FORMAT_OFF); - r = table_add_many(table, - TABLE_STRING, link->name, - TABLE_STRING, lldp.chassis_id, - TABLE_STRING, lldp.system_name, - TABLE_STRING, capabilities, - TABLE_STRING, lldp.port_id, - TABLE_STRING, lldp.port_description); - if (r < 0) - return table_log_add_error(r); + if (arg_json_format_flags == JSON_FORMAT_OFF) { + r = table_add_many(table, + TABLE_STRING, link->name, + TABLE_STRING, lldp.chassis_id, + TABLE_STRING, lldp.system_name, + TABLE_STRING, capabilities, + TABLE_STRING, lldp.port_id, + TABLE_STRING, lldp.port_description); + if (r < 0) + return table_log_add_error(r); + + neighbors_count += 1; + capabilities_all |= lldp.capabilities; + } else { + _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; + _cleanup_(json_variant_unrefp) JsonVariant *neighbors_on_link = NULL; + + r = json_build(&v, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR_CONDITION(lldp.chassis_id, "chassisId", JSON_BUILD_STRING(lldp.chassis_id)), + JSON_BUILD_PAIR_CONDITION(lldp.port_id, "portId", JSON_BUILD_STRING(lldp.port_id)), + JSON_BUILD_PAIR_CONDITION(lldp.system_name, "systemName", JSON_BUILD_STRING(lldp.system_name)), + JSON_BUILD_PAIR_CONDITION(lldp.port_description, "portDescription", JSON_BUILD_STRING(lldp.port_description)), + JSON_BUILD_PAIR_CONDITION(lldp.capabilities, "enabledCapabilities", JSON_BUILD_STRING(capabilities)))); + if (r < 0) + return r; + + neighbors_on_link = json_variant_ref(json_variant_by_key(json, link->name)); + + r = json_variant_append_array(&neighbors_on_link, v); + if (r < 0) + return r; + + r = json_variant_set_field(&json, link->name, neighbors_on_link); + if (r < 0) + return r; - neighbors_count += 1; - capabilities_all |= lldp.capabilities; + } } } - r = table_print(table, NULL); - if (r < 0) - return table_log_print_error(r); + if (arg_json_format_flags == JSON_FORMAT_OFF) { + r = table_print(table, NULL); + if (r < 0) + return table_log_print_error(r); - if (arg_legend) { - lldp_capabilities_legend(capabilities_all); - printf("\n%i neighbors listed.\n", neighbors_count); + if (arg_legend) { + lldp_capabilities_legend(capabilities_all); + printf("\n%i neighbors listed.\n", neighbors_count); + } + } else { + json_variant_dump(json, arg_json_format_flags, NULL, NULL); } return 0;