cli: merge branch 'th/cli-select-connection'

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/108
This commit is contained in:
Thomas Haller 2019-04-16 09:16:14 +02:00
commit 87c2b0e6cd
6 changed files with 81 additions and 57 deletions

View file

@ -425,36 +425,38 @@ nmc_find_connection (const GPtrArray *connections,
GPtrArray **out_result,
gboolean complete)
{
NMConnection *connection;
NMConnection *best_candidate_uuid = NULL;
NMConnection *best_candidate = NULL;
gs_unref_ptrarray GPtrArray *result_allocated = NULL;
GPtrArray *result = out_result ? *out_result : NULL;
const guint result_inital_len = result ? result->len : 0u;
guint i, j;
nm_assert (connections);
nm_assert (filter_val);
for (i = 0; i < connections->len; i++) {
const char *v, *v_num;
gboolean match_by_uuid = FALSE;
NMConnection *connection;
const char *v;
const char *v_num;
connection = NM_CONNECTION (connections->pdata[i]);
/* When filter_type is NULL, compare connection ID (filter_val)
* against all types. Otherwise, only compare against the specific
* type. If 'path' filter type is specified, comparison against
* numeric index (in addition to the whole path) is allowed.
*/
if (NM_IN_STRSET (filter_type, NULL, "id")) {
v = nm_connection_get_id (connection);
if (complete)
nmc_complete_strings (filter_val, v, NULL);
if (nm_streq0 (filter_val, v))
goto found;
}
if (NM_IN_STRSET (filter_type, NULL, "uuid")) {
v = nm_connection_get_uuid (connection);
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, NULL);
nmc_complete_strings (filter_val, v);
if (nm_streq0 (filter_val, v)) {
match_by_uuid = TRUE;
goto found;
}
}
if (NM_IN_STRSET (filter_type, NULL, "id")) {
v = nm_connection_get_id (connection);
if (complete)
nmc_complete_strings (filter_val, v);
if (nm_streq0 (filter_val, v))
goto found;
}
@ -463,7 +465,7 @@ nmc_find_connection (const GPtrArray *connections,
v = nm_connection_get_path (connection);
v_num = nm_utils_dbus_path_get_last_component (v);
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, filter_type ? v_num : NULL, NULL);
nmc_complete_strings (filter_val, v, (*filter_val ? v_num : NULL));
if ( nm_streq0 (filter_val, v)
|| (filter_type && nm_streq0 (filter_val, v_num)))
goto found;
@ -472,29 +474,51 @@ nmc_find_connection (const GPtrArray *connections,
if (NM_IN_STRSET (filter_type, NULL, "filename")) {
v = nm_remote_connection_get_filename (NM_REMOTE_CONNECTION (connections->pdata[i]));
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, NULL);
nmc_complete_strings (filter_val, v);
if (nm_streq0 (filter_val, v))
goto found;
}
continue;
found:
if (!out_result)
return connection;
if (!best_candidate)
best_candidate = connection;
if (!result)
result = g_ptr_array_new_with_free_func (g_object_unref);
for (j = 0; j < result->len; j++) {
if (connection == result->pdata[j])
break;
if (match_by_uuid) {
if ( !complete
&& !out_result)
return connection;
best_candidate_uuid = connection;
} else {
if (!best_candidate)
best_candidate = connection;
}
if (out_result) {
gboolean already_tracked = FALSE;
if (!result) {
result_allocated = g_ptr_array_new_with_free_func (g_object_unref);
result = result_allocated;
} else {
for (j = 0; j < result->len; j++) {
if (connection == result->pdata[j]) {
already_tracked = TRUE;
break;
}
}
}
if (!already_tracked) {
if (match_by_uuid) {
/* the profile is matched exactly (by UUID). We prepend it
* to the list of all found profiles. */
g_ptr_array_insert (result, result_inital_len, g_object_ref (connection));
} else
g_ptr_array_add (result, g_object_ref (connection));
}
}
if (j == result->len)
g_ptr_array_add (result, g_object_ref (connection));
}
NM_SET_OUT (out_result, result);
return best_candidate;
if (result_allocated)
*out_result = g_steal_pointer (&result_allocated);
return best_candidate_uuid ?: best_candidate;
}
NMActiveConnection *
@ -525,7 +549,7 @@ nmc_find_active_connection (const GPtrArray *active_cons,
if (NM_IN_STRSET (filter_type, NULL, "id")) {
v = nm_active_connection_get_id (candidate);
if (complete)
nmc_complete_strings (filter_val, v, NULL);
nmc_complete_strings (filter_val, v);
if (nm_streq0 (filter_val, v))
goto found;
}
@ -533,7 +557,7 @@ nmc_find_active_connection (const GPtrArray *active_cons,
if (NM_IN_STRSET (filter_type, NULL, "uuid")) {
v = nm_active_connection_get_uuid (candidate);
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, NULL);
nmc_complete_strings (filter_val, v);
if (nm_streq0 (filter_val, v))
goto found;
}
@ -542,7 +566,7 @@ nmc_find_active_connection (const GPtrArray *active_cons,
v = con ? nm_connection_get_path (NM_CONNECTION (con)) : NULL;
v_num = nm_utils_dbus_path_get_last_component (v);
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, filter_type ? v_num : NULL, NULL);
nmc_complete_strings (filter_val, v, filter_type ? v_num : NULL);
if ( nm_streq0 (filter_val, v)
|| (filter_type && nm_streq0 (filter_val, v_num)))
goto found;
@ -551,7 +575,7 @@ nmc_find_active_connection (const GPtrArray *active_cons,
if (NM_IN_STRSET (filter_type, NULL, "filename")) {
v = nm_remote_connection_get_filename (con);
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, NULL);
nmc_complete_strings (filter_val, v);
if (nm_streq0 (filter_val, v))
goto found;
}
@ -560,7 +584,7 @@ nmc_find_active_connection (const GPtrArray *active_cons,
v = nm_object_get_path (NM_OBJECT (candidate));
v_num = nm_utils_dbus_path_get_last_component (v);
if (complete && (filter_type || *filter_val))
nmc_complete_strings (filter_val, v, filter_type ? v_num : NULL, NULL);
nmc_complete_strings (filter_val, v, filter_type ? v_num : NULL);
if ( nm_streq0 (filter_val, v)
|| (filter_type && nm_streq0 (filter_val, v_num)))
goto found;
@ -1254,9 +1278,9 @@ call_cmd (NmCli *nmc, GSimpleAsyncResult *simple, const NMCCommand *cmd, int arg
static void
nmc_complete_help (const char *prefix)
{
nmc_complete_strings (prefix, "help", NULL);
nmc_complete_strings (prefix, "help");
if (*prefix == '-')
nmc_complete_strings (prefix, "-help", "--help", NULL);
nmc_complete_strings (prefix, "-help", "--help");
}
/**
@ -1395,7 +1419,7 @@ void
nmc_complete_bool (const char *prefix)
{
nmc_complete_strings (prefix, "true", "yes", "on",
"false", "no", "off", NULL);
"false", "no", "off");
}
/**

View file

@ -1941,7 +1941,7 @@ get_connection (NmCli *nmc,
}
if (*argc == 1 && nmc->complete)
nmc_complete_strings (**argv, "id", "uuid", "path", "filename", NULL);
nmc_complete_strings (**argv, "id", "uuid", "path", "filename");
if (NM_IN_STRSET (**argv, "id", "uuid", "path", "filename")) {
if (*argc == 1) {
@ -2100,7 +2100,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
guint i_found_cons;
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "id", "uuid", "path", "filename", "apath", NULL);
nmc_complete_strings (*argv, "id", "uuid", "path", "filename", "apath");
if (NM_IN_STRSET (*argv, "id", "uuid", "path", "filename", "apath")) {
selector = *argv;
@ -2846,7 +2846,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
while (argc > 0) {
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "ifname", "ap", "passwd-file", NULL);
nmc_complete_strings (*argv, "ifname", "ap", "passwd-file");
if (strcmp (*argv, "ifname") == 0) {
argc--;
@ -3098,7 +3098,7 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
const char *selector = NULL;
if (arg_num == 1 && nmc->complete)
nmc_complete_strings (*arg_ptr, "id", "uuid", "path", "filename", "apath", NULL);
nmc_complete_strings (*arg_ptr, "id", "uuid", "path", "filename", "apath");
if (NM_IN_STRSET (*arg_ptr, "id", "uuid", "path", "filename", "apath")) {
selector = *arg_ptr;
@ -8167,7 +8167,7 @@ do_connection_edit (NmCli *nmc, int argc, char **argv)
next_arg (nmc, &argc, &argv, NULL);
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "type", "con-name", "id", "uuid", "path", "filename", NULL);
nmc_complete_strings (*argv, "type", "con-name", "id", "uuid", "path", "filename");
nmc->return_value = NMC_RESULT_SUCCESS;

View file

@ -2473,7 +2473,7 @@ do_device_set (NmCli *nmc, int argc, char **argv)
gboolean flag;
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "managed", "autoconnect", NULL);
nmc_complete_strings (*argv, "managed", "autoconnect");
if (matches (*argv, "managed")) {
argc--;
@ -2997,7 +2997,7 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
}
rescan = *argv;
if (argc == 1 && nmc->complete)
nmc_complete_strings (rescan, "auto", "no", "yes", NULL);
nmc_complete_strings (rescan, "auto", "no", "yes");
break;
default:
g_assert_not_reached();
@ -3179,7 +3179,7 @@ do_device_wifi_connect_network (NmCli *nmc, int argc, char **argv)
while (argc > 0) {
if (argc == 1 && nmc->complete) {
nmc_complete_strings (*argv, "ifname", "bssid", "password", "wep-key-type",
"name", "private", "hidden", NULL);
"name", "private", "hidden");
}
if (strcmp (*argv, "ifname") == 0) {
@ -3229,7 +3229,7 @@ do_device_wifi_connect_network (NmCli *nmc, int argc, char **argv)
goto finish;
}
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "key", "phrase", NULL);
nmc_complete_strings (*argv, "key", "phrase");
if (strcmp (*argv, "key") == 0)
wep_passphrase = FALSE;
else if (strcmp (*argv, "phrase") == 0)
@ -3711,7 +3711,7 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
while (argc > 0) {
if (argc == 1 && nmc->complete) {
nmc_complete_strings (*argv, "ifname", "con-name", "ssid", "band",
"channel", "password", NULL);
"channel", "password");
}
if (strcmp (*argv, "ifname") == 0) {
@ -3753,7 +3753,7 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv)
}
band = *argv;
if (argc == 1 && nmc->complete)
nmc_complete_strings (band, "a", "bg", NULL);
nmc_complete_strings (band, "a", "bg");
if (strcmp (band, "a") && strcmp (band, "bg")) {
g_string_printf (nmc->return_text, _("Error: band argument value '%s' is invalid; use 'a' or 'bg'."),
band);
@ -3935,7 +3935,7 @@ do_device_wifi_rescan (NmCli *nmc, int argc, char **argv)
/* Get the parameters */
while (argc > 0) {
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "ifname", "ssid", NULL);
nmc_complete_strings (*argv, "ifname", "ssid");
if (strcmp (*argv, "ifname") == 0) {
if (ifname) {
@ -4122,7 +4122,7 @@ do_device_lldp_list (NmCli *nmc, int argc, char **argv)
next_arg (nmc, &argc, &argv, NULL);
while (argc > 0) {
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "ifname", NULL);
nmc_complete_strings (*argv, "ifname");
if (strcmp (*argv, "ifname") == 0) {
argc--;

View file

@ -683,7 +683,7 @@ do_general_logging (NmCli *nmc, int argc, char **argv)
do {
if (argc == 1 && nmc->complete)
nmc_complete_strings (*argv, "level", "domains", NULL);
nmc_complete_strings (*argv, "level", "domains");
if (matches (*argv, "level")) {
argc--;
@ -878,7 +878,7 @@ do_networking_connectivity (NmCli *nmc, int argc, char **argv)
next_arg (nmc, &argc, &argv, NULL);
if (nmc->complete) {
if (argc == 1)
nmc_complete_strings (*argv, "check", NULL);
nmc_complete_strings (*argv, "check");
return nmc->return_value;
}

View file

@ -732,7 +732,7 @@ process_command_line (NmCli *nmc, int argc, char **argv)
nmc_complete_strings (argv[0], "--terse", "--pretty", "--mode", "--overview",
"--colors", "--escape",
"--fields", "--nocheck", "--get-values",
"--wait", "--version", "--help", NULL);
"--wait", "--version", "--help");
}
if (argv[0][1] == '-' && argv[0][2] == '\0') {

View file

@ -189,10 +189,10 @@ next_arg (NmCli *nmc, int *argc, char ***argv, ...)
if (nmc && nmc->complete && *argc == 1) {
while ((cmd_option = va_arg (args, const char *)))
nmc_complete_strings (**argv, cmd_option, NULL);
nmc_complete_strings (**argv, cmd_option);
if (***argv == '-')
nmc_complete_strings (**argv, "--ask", "--show-secrets", NULL);
nmc_complete_strings (**argv, "--ask", "--show-secrets");
va_end (args);
return 0;