mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-05 13:28:02 +02:00
cli/connections: do connection completion in get_connection()
Start completing by the id if the filter type is not specified
This commit is contained in:
parent
717db4fe01
commit
2895261c91
4 changed files with 45 additions and 24 deletions
|
|
@ -872,6 +872,7 @@ nmc_team_check_config (const char *config, char **out_config, GError **error)
|
|||
* @filter_val: connection to find (connection name, UUID or path)
|
||||
* @start: where to start in @list. The location is updated so that the function
|
||||
* can be called multiple times (for connections with the same name).
|
||||
* @complete: print possible completions
|
||||
*
|
||||
* Find a connection in @list according to @filter_val. @filter_type determines
|
||||
* what property is used for comparison. When @filter_type is NULL, compare
|
||||
|
|
@ -885,7 +886,8 @@ NMConnection *
|
|||
nmc_find_connection (const GPtrArray *connections,
|
||||
const char *filter_type,
|
||||
const char *filter_val,
|
||||
int *start)
|
||||
int *start,
|
||||
gboolean complete)
|
||||
{
|
||||
NMConnection *connection;
|
||||
NMConnection *found = NULL;
|
||||
|
|
@ -907,20 +909,36 @@ nmc_find_connection (const GPtrArray *connections,
|
|||
* type. If 'path' filter type is specified, comparison against
|
||||
* numeric index (in addition to the whole path) is allowed.
|
||||
*/
|
||||
if ( ( (!filter_type || strcmp (filter_type, "id") == 0)
|
||||
&& strcmp (filter_val, id) == 0)
|
||||
|| ( (!filter_type || strcmp (filter_type, "uuid") == 0)
|
||||
&& strcmp (filter_val, uuid) == 0)
|
||||
|| ( (!filter_type || strcmp (filter_type, "path") == 0)
|
||||
&& (g_strcmp0 (filter_val, path) == 0 || (filter_type && g_strcmp0 (filter_val, path_num) == 0)))) {
|
||||
if (!start)
|
||||
return connection;
|
||||
if (found) {
|
||||
*start = i;
|
||||
return found;
|
||||
}
|
||||
found = connection;
|
||||
if (!filter_type || strcmp (filter_type, "id") == 0) {
|
||||
if (complete)
|
||||
nmc_complete_strings (filter_val, id, NULL);
|
||||
if (strcmp (filter_val, id) == 0)
|
||||
goto found;
|
||||
}
|
||||
|
||||
if (!filter_type || strcmp (filter_type, "uuid") == 0) {
|
||||
if (complete && (filter_type || *filter_val))
|
||||
nmc_complete_strings (filter_val, uuid, NULL);
|
||||
if (strcmp (filter_val, uuid) == 0)
|
||||
goto found;
|
||||
}
|
||||
|
||||
if (!filter_type || strcmp (filter_type, "path") == 0) {
|
||||
if (complete && (filter_type || *filter_val))
|
||||
nmc_complete_strings (filter_val, path, filter_type ? path_num : NULL, NULL);
|
||||
if (g_strcmp0 (filter_val, path) == 0 || (filter_type && g_strcmp0 (filter_val, path_num) == 0))
|
||||
goto found;
|
||||
}
|
||||
|
||||
continue;
|
||||
found:
|
||||
if (!start)
|
||||
return connection;
|
||||
if (found) {
|
||||
*start = i;
|
||||
return found;
|
||||
}
|
||||
found = connection;
|
||||
}
|
||||
|
||||
if (start)
|
||||
|
|
@ -1096,7 +1114,7 @@ nmc_secrets_requested (NMSecretAgentSimple *agent,
|
|||
p = strrchr (path, '/');
|
||||
if (p)
|
||||
*p = '\0';
|
||||
connection = nmc_find_connection (nmc->connections, "path", path, NULL);
|
||||
connection = nmc_find_connection (nmc->connections, "path", path, NULL, FALSE);
|
||||
g_free (path);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,8 @@ gboolean nmc_team_check_config (const char *config, char **out_config, GError **
|
|||
NMConnection *nmc_find_connection (const GPtrArray *connections,
|
||||
const char *filter_type,
|
||||
const char *filter_val,
|
||||
int *start);
|
||||
int *start,
|
||||
gboolean complete);
|
||||
|
||||
void nmc_secrets_requested (NMSecretAgentSimple *agent,
|
||||
const char *request_id,
|
||||
|
|
|
|||
|
|
@ -1707,6 +1707,9 @@ get_connection (NmCli *nmc, int *argc, char ***argv, int *pos, GError **error)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (*argc == 1 && nmc->complete)
|
||||
nmc_complete_strings (**argv, "id", "uuid", "path", NULL);
|
||||
|
||||
if ( strcmp (**argv, "id") == 0
|
||||
|| strcmp (**argv, "uuid") == 0
|
||||
|| strcmp (**argv, "path") == 0) {
|
||||
|
|
@ -1718,7 +1721,8 @@ get_connection (NmCli *nmc, int *argc, char ***argv, int *pos, GError **error)
|
|||
}
|
||||
}
|
||||
|
||||
connection = nmc_find_connection (nmc->connections, selector, **argv, pos);
|
||||
connection = nmc_find_connection (nmc->connections, selector, **argv, pos,
|
||||
*argc == 1 && nmc->complete);
|
||||
if (!connection) {
|
||||
g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_NOT_FOUND,
|
||||
_("unknown connection '%s'"), **argv);
|
||||
|
|
@ -1848,7 +1852,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
|
|||
}
|
||||
|
||||
/* Find connection by id, uuid, path or apath */
|
||||
con = nmc_find_connection (nmc->connections, selector, *argv, &pos);
|
||||
con = nmc_find_connection (nmc->connections, selector, *argv, &pos, FALSE);
|
||||
if (!con) {
|
||||
acon = find_active_connection (active_cons, nmc->connections, selector, *argv, NULL, FALSE);
|
||||
if (acon)
|
||||
|
|
@ -4903,7 +4907,7 @@ uuid_display_hook (char **array, int len, int max_len)
|
|||
char *tmp;
|
||||
const char *id;
|
||||
for (i = 1; i <= len; i++) {
|
||||
con = nmc_find_connection (nmc_tab_completion.nmc->connections, "uuid", array[i], NULL);
|
||||
con = nmc_find_connection (nmc_tab_completion.nmc->connections, "uuid", array[i], NULL, FALSE);
|
||||
id = con ? nm_connection_get_id (con) : NULL;
|
||||
if (id) {
|
||||
tmp = g_strdup_printf ("%s (%s)", array[i], id);
|
||||
|
|
@ -7737,7 +7741,7 @@ do_connection_edit_func (NmCli *nmc, int argc, char **argv)
|
|||
/* Existing connection */
|
||||
NMConnection *found_con;
|
||||
|
||||
found_con = nmc_find_connection (nmc->connections, selector, con, NULL);
|
||||
found_con = nmc_find_connection (nmc->connections, selector, con, NULL, FALSE);
|
||||
if (!found_con) {
|
||||
g_string_printf (nmc->return_text, _("Error: Unknown connection '%s'."), con);
|
||||
nmc->return_value = NMC_RESULT_ERROR_NOT_FOUND;
|
||||
|
|
|
|||
|
|
@ -3247,8 +3247,7 @@ nmc_property_connection_set_secondaries (NMSetting *setting, const char *prop, c
|
|||
continue;
|
||||
|
||||
if (nm_utils_is_uuid (*iter)) {
|
||||
con = nmc_find_connection (nm_cli.connections,
|
||||
"uuid", *iter, NULL);
|
||||
con = nmc_find_connection (nm_cli.connections, "uuid", *iter, NULL, FALSE);
|
||||
if (!con)
|
||||
g_print (_("Warning: %s is not an UUID of any existing connection profile\n"), *iter);
|
||||
else {
|
||||
|
|
@ -3260,8 +3259,7 @@ nmc_property_connection_set_secondaries (NMSetting *setting, const char *prop, c
|
|||
}
|
||||
}
|
||||
} else {
|
||||
con = nmc_find_connection (nm_cli.connections,
|
||||
"id", *iter, NULL);
|
||||
con = nmc_find_connection (nm_cli.connections, "id", *iter, NULL, FALSE);
|
||||
if (!con) {
|
||||
g_set_error (error, 1, 0, _("'%s' is not a name of any exiting profile"), *iter);
|
||||
g_strfreev (strv);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue