cli: make id|uuid optional for 'nmcli connection delete'

and also allow identifying connetions with 'path' that accepts either the whole
D-Dus path or just an index.

nmcli connection delete [id|uuid|path] <ID>

Example:
nmcli connection delete "Wired 1" "Wired 2" "Wired 3"
nmcli connection delete id MyMain id "Quick Test 1" "Quick Test 2" path 23 path 47
This commit is contained in:
Jiří Klimeš
2012-11-19 13:53:24 +01:00
committed by Dan Williams
parent 1e106e31b8
commit 4eef48d4aa

View File

@@ -202,7 +202,7 @@ usage (void)
" up id <id> | uuid <id> [iface <iface>] [ap <BSSID>] [--nowait] [--timeout <timeout>]\n" " up id <id> | uuid <id> [iface <iface>] [ap <BSSID>] [--nowait] [--timeout <timeout>]\n"
#endif #endif
" down id <id> | uuid <id>\n" " down id <id> | uuid <id>\n"
" delete id <id> | uuid <id>\n" " delete [id|uuid|path] <ID>\n"
"\n" "\n"
)); ));
} }
@@ -1598,56 +1598,36 @@ error:
return nmc->return_value; return nmc->return_value;
} }
typedef struct {
NmCli *nmc;
int counter;
} DeleteStateInfo;
static void static void
delete_cb (NMRemoteConnection *con, GError *err, gpointer user_data) delete_cb (NMRemoteConnection *con, GError *err, gpointer user_data)
{ {
NmCli *nmc = (NmCli *) user_data; DeleteStateInfo *info = (DeleteStateInfo *) user_data;
if (err) { if (err) {
g_string_printf (nmc->return_text, _("Error: Connection deletion failed: %s"), err->message); g_string_printf (info->nmc->return_text, _("Error: Connection deletion failed: %s"), err->message);
nmc->return_value = NMC_RESULT_ERROR_CON_DEL; info->nmc->return_value = NMC_RESULT_ERROR_CON_DEL;
} }
quit ();
}
static void info->counter--;
connection_removed_cb (NMRemoteConnection *con, gpointer user_data) if (info->counter == 0) {
{ g_free (info);
NmCli *nmc = (NmCli *) user_data; quit ();
}
nmc->return_value = NMC_RESULT_SUCCESS;
quit ();
} }
static NMCResultCode static NMCResultCode
do_connection_delete (NmCli *nmc, int argc, char **argv) do_connection_delete (NmCli *nmc, int argc, char **argv)
{ {
NMConnection *connection = NULL; NMConnection *connection = NULL;
const char *selector = NULL; DeleteStateInfo *del_info = NULL;
const char *id = NULL;
while (argc > 0) { nmc->return_value = NMC_RESULT_SUCCESS;
if (strcmp (*argv, "id") == 0 || strcmp (*argv, "uuid") == 0) { nmc->should_wait = FALSE;
selector = *argv;
if (next_arg (&argc, &argv) != 0) {
g_string_printf (nmc->return_text, _("Error: %s argument is missing."), *(argv-1));
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto error;
}
id = *argv;
}
else
fprintf (stderr, _("Unknown parameter: %s\n"), *argv);
argc--;
argv++;
}
if (!id) {
g_string_printf (nmc->return_text, _("Error: id or uuid has to be specified."));
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto error;
}
/* create NMClient */ /* create NMClient */
nmc->get_client (nmc); nmc->get_client (nmc);
@@ -1658,30 +1638,56 @@ do_connection_delete (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
connection = find_connection (nmc->system_connections, selector, id); if (argc == 0) {
g_string_printf (nmc->return_text, _("Error: No connection specified."));
if (!connection) { nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
g_string_printf (nmc->return_text, _("Error: Unknown connection: %s."), id);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
goto error; goto error;
} }
/* We need to wait a bit so that nmcli's permissions can be queried. del_info = g_malloc0 (sizeof (DeleteStateInfo));
* We will exit either on "Removed" signal or when D-Bus return (error) del_info->nmc = nmc;
* message is received. del_info->counter = 0;
*/
nmc->should_wait = TRUE;
/* Connect to "Removed" signal to be able to exit when connection was removed */ while (argc > 0) {
g_signal_connect (connection, NM_REMOTE_CONNECTION_REMOVED, G_CALLBACK (connection_removed_cb), nmc); const char *selector = NULL;
/* Delete the connection */ if ( strcmp (*argv, "id") == 0
nm_remote_connection_delete (NM_REMOTE_CONNECTION (connection), delete_cb, nmc); || strcmp (*argv, "uuid") == 0
|| strcmp (*argv, "path") == 0) {
selector = *argv;
if (next_arg (&argc, &argv) != 0) {
g_string_printf (nmc->return_text, _("Error: %s argument is missing."), selector);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto error;
}
}
connection = find_connection (nmc->system_connections, selector, *argv);
if (!connection) {
g_string_printf (nmc->return_text, _("Error: Unknown connection: %s."), *argv);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
goto error;
}
/* We need to wait a bit so that nmcli's permissions can be checked.
* We will exit when D-Bus return (error) messages are received.
*/
nmc->should_wait = TRUE;
del_info->counter++;
/* Delete the connection */
nm_remote_connection_delete (NM_REMOTE_CONNECTION (connection), delete_cb, del_info);
argc--;
argv++;
}
return nmc->return_value; return nmc->return_value;
error: error:
nmc->should_wait = FALSE; nmc->should_wait = FALSE;
g_free (del_info);
return nmc->return_value; return nmc->return_value;
} }