cli: sort active-connection for nmcli connection show $PROFILE output

There might be multiple active connections. Output them in a defined order.
This commit is contained in:
Thomas Haller 2018-04-26 11:46:16 +02:00
parent a1b25a47b0
commit b990cee00c
5 changed files with 44 additions and 38 deletions

View file

@ -865,36 +865,42 @@ construct_header_name (const char *base, const char *spec)
return g_strdup_printf ("%s (%s)", base, spec);
}
static int
get_ac_for_connection_cmp (gconstpointer pa, gconstpointer pb, gpointer user_data)
{
NMActiveConnection *ac_a = *((NMActiveConnection *const*) pa);
NMActiveConnection *ac_b = *((NMActiveConnection *const*) pb);
return active_connection_cmp (ac_a, ac_b);
}
static NMActiveConnection *
get_ac_for_connection (const GPtrArray *active_cons, NMConnection *connection, GPtrArray **out_result)
{
const char *con_path, *ac_con_path;
guint i;
NMActiveConnection *best_candidate = NULL;
GPtrArray *result = out_result ? *out_result : NULL;
con_path = nm_connection_get_path (connection);
for (i = 0; i < active_cons->len; i++) {
NMActiveConnection *candidate = g_ptr_array_index (active_cons, i);
NMRemoteConnection *con;
con = nm_active_connection_get_connection (candidate);
if (NM_CONNECTION (con) != connection) {
/* also compare the D-Bus paths. Why? I don't know. */
ac_con_path = con ? nm_connection_get_path (NM_CONNECTION (con)) : NULL;
if (!nm_streq0 (ac_con_path, con_path))
continue;
}
if (NM_CONNECTION (con) != connection)
continue;
if (!out_result)
return candidate;
if (!best_candidate)
best_candidate = candidate;
if (!result)
result = g_ptr_array_new_with_free_func (g_object_unref);
g_ptr_array_add (result, g_object_ref (candidate));
}
if (result) {
g_ptr_array_sort_with_data (result, get_ac_for_connection_cmp, NULL);
best_candidate = result->pdata[0];
}
NM_SET_OUT (out_result, result);
return best_candidate;
}

View file

@ -82,19 +82,6 @@ proxy.method: none
proxy.browser-only: no
proxy.pac-url: --
proxy.pac-script: --
GENERAL.NAME: ethernet
GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL
GENERAL.DEVICES: eth0
GENERAL.STATE: deactivating
GENERAL.DEFAULT: no
GENERAL.DEFAULT6: no
GENERAL.SPEC-OBJECT: --
GENERAL.VPN: no
GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
GENERAL.NAME: ethernet
GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL
GENERAL.DEVICES: eth1
@ -108,6 +95,19 @@ GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
GENERAL.NAME: ethernet
GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL
GENERAL.DEVICES: eth0
GENERAL.STATE: deactivating
GENERAL.DEFAULT: no
GENERAL.DEFAULT6: no
GENERAL.SPEC-OBJECT: --
GENERAL.VPN: no
GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
<<<
stderr: 0 bytes
>>>

View file

@ -82,19 +82,6 @@ proxy.method: none
proxy.browser-only: nie
proxy.pac-url: --
proxy.pac-script: --
GENERAL.NAME: ethernet
GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL
GENERAL.DEVICES: eth0
GENERAL.STATE: deactivating
GENERAL.DEFAULT: nie
GENERAL.DEFAULT6: nie
GENERAL.SPEC-OBJECT: --
GENERAL.VPN: nie
GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
GENERAL.NAME: ethernet
GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL
GENERAL.DEVICES: eth1
@ -108,6 +95,19 @@ GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
GENERAL.NAME: ethernet
GENERAL.UUID: UUID-ethernet-REPLACED-REPLACED-REPL
GENERAL.DEVICES: eth0
GENERAL.STATE: deactivating
GENERAL.DEFAULT: nie
GENERAL.DEFAULT6: nie
GENERAL.SPEC-OBJECT: --
GENERAL.VPN: nie
GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/Connection/3
GENERAL.ZONE: --
GENERAL.MASTER-PATH: --
<<<
stderr: 0 bytes
>>>

View file

@ -9,6 +9,6 @@ stdout: 0 bytes
stderr: 103 bytes
>>>
**
nmcli:ERROR:clients/cli/connections.c:1062:fill_output_active_connection: assertion failed: (s_con)
nmcli:ERROR:clients/cli/connections.c:1068:fill_output_active_connection: assertion failed: (s_con)
<<<

View file

@ -9,6 +9,6 @@ stdout: 0 bytes
stderr: 103 bytes
>>>
**
nmcli:ERROR:clients/cli/connections.c:1062:fill_output_active_connection: assertion failed: (s_con)
nmcli:ERROR:clients/cli/connections.c:1068:fill_output_active_connection: assertion failed: (s_con)
<<<