From c01db4ac5124b1a40d63f0c32280c4b5d221b866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Tue, 6 Sep 2011 15:07:58 +0200 Subject: [PATCH] cli: add 'nmcli con delete' command for removing configured connections --- cli/src/connections.c | 73 +++++++++++++++++++++++++++++++++++++++++-- man/nmcli.1.in | 14 ++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/cli/src/connections.c b/cli/src/connections.c index 3ec272de7c..cb0db7905e 100644 --- a/cli/src/connections.c +++ b/cli/src/connections.c @@ -165,7 +165,7 @@ usage (void) { fprintf (stderr, _("Usage: nmcli con { COMMAND | help }\n" - " COMMAND := { list | status | up | down }\n\n" + " COMMAND := { list | status | up | down | delete }\n\n" " list [id | uuid ]\n" " status\n" #if WITH_WIMAX @@ -173,7 +173,8 @@ usage (void) #else " up id | uuid [iface ] [ap ] [--nowait] [--timeout ]\n" #endif - " down id | uuid \n")); + " down id | uuid \n" + " delete id | uuid \n")); } /* The real commands that do something - i.e. not 'help', etc. */ @@ -182,6 +183,7 @@ static const char *real_con_commands[] = { "status", "up", "down", + "delete", NULL }; @@ -1575,6 +1577,66 @@ error: return nmc->return_value; } +static NMCResultCode +do_connection_delete (NmCli *nmc, int argc, char **argv) +{ + NMConnection *connection = NULL; + const char *selector; + const char *id = NULL; + GError *error = NULL; + + nmc->should_wait = FALSE; + + while (argc > 0) { + if (strcmp (*argv, "id") == 0 || strcmp (*argv, "uuid") == 0) { + selector = *argv; + if (next_arg (&argc, &argv) != 0) { + g_string_printf (nmc->return_text, _("Error: %s argument is missing."), *argv); + 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; + } + + if (!nmc_is_nm_running (nmc, &error)) { + if (error) { + g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message); + nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + g_error_free (error); + } else { + g_string_printf (nmc->return_text, _("Error: NetworkManager is not running.")); + nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING; + } + goto error; + } + + connection = find_connection (nmc->system_connections, selector, id); + + if (!connection) { + g_string_printf (nmc->return_text, _("Error: Unknown connection: %s."), id); + nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + goto error; + } + + /* Delete the connection */ + nm_remote_connection_delete (NM_REMOTE_CONNECTION (connection), NULL, NULL); + +error: + return nmc->return_value; +} + static NMCResultCode parse_cmd (NmCli *nmc, int argc, char **argv) { @@ -1600,6 +1662,9 @@ parse_cmd (NmCli *nmc, int argc, char **argv) else if (matches(*argv, "down") == 0) { nmc->return_value = do_connection_down (nmc, argc-1, argv+1); } + else if (matches(*argv, "delete") == 0) { + nmc->return_value = do_connection_delete (nmc, argc-1, argv+1); + } else if (matches (*argv, "help") == 0) { usage (); nmc->should_wait = FALSE; @@ -1672,6 +1737,7 @@ do_connections (NmCli *nmc, int argc, char **argv) if (error || !bus) { g_string_printf (nmc->return_text, _("Error: could not connect to D-Bus.")); nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + nmc->should_wait = FALSE; return nmc->return_value; } @@ -1679,8 +1745,8 @@ do_connections (NmCli *nmc, int argc, char **argv) if (!(nmc->system_settings = nm_remote_settings_new (bus))) { g_string_printf (nmc->return_text, _("Error: Could not get system settings.")); nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + nmc->should_wait = FALSE; return nmc->return_value; - } /* find out whether settings service is running */ @@ -1689,6 +1755,7 @@ do_connections (NmCli *nmc, int argc, char **argv) if (!nmc->system_settings_running) { g_string_printf (nmc->return_text, _("Error: Can't obtain connections: settings service is not running.")); nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + nmc->should_wait = FALSE; return nmc->return_value; } diff --git a/man/nmcli.1.in b/man/nmcli.1.in index f61b99b656..03ed664c43 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -201,7 +201,7 @@ Connections .br Get information about NetworkManager's connections. .TP -.SS \fICOMMAND\fP := { list | status | up | down } +.SS \fICOMMAND\fP := { list | status | up | down | delete } .sp .RS .TP @@ -255,6 +255,18 @@ interface: org.freedesktop.NetworkManager method: DeactivateConnection arguments: according to arguments .fi +.TP +.B delete id | uuid +.br +Delete a configured connection. The connection to delete is specified with +\fIid\fP (connection name) or \fIuuid\fP (connection UUID). +.br +.nf +\fBReference to D-Bus:\fP +interface: org.freedesktop.NetworkManager.Settings.Connection +method: Delete +arguments: none +.fi .RE .TP