cli: get NMClient early and remove duplicated code

This commit is contained in:
Jiří Klimeš
2014-09-19 11:31:19 +02:00
parent f27a259ee6
commit dc1c17fd30
2 changed files with 13 additions and 132 deletions

View File

@@ -1284,13 +1284,6 @@ do_connections_show (NmCli *nmc, gboolean active_only, int argc, char **argv)
char *profile_flds = NULL, *active_flds = NULL; char *profile_flds = NULL, *active_flds = NULL;
nmc->should_wait = FALSE; nmc->should_wait = FALSE;
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto finish;
}
if (argc == 0) { if (argc == 0) {
char *fields_str; char *fields_str;
@@ -1997,15 +1990,6 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
argv++; argv++;
} }
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
/* Use nowait_flag instead of should_wait because exiting has to be postponed till /* Use nowait_flag instead of should_wait because exiting has to be postponed till
* active_connection_state_cb() is called. That gives NM time to check our permissions * active_connection_state_cb() is called. That gives NM time to check our permissions
* and we can follow activation progress. * and we can follow activation progress.
@@ -2057,15 +2041,6 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
} }
} }
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
/* Get active connections */ /* Get active connections */
active_cons = nm_client_get_active_connections (nmc->client); active_cons = nm_client_get_active_connections (nmc->client);
while (arg_num > 0) { while (arg_num > 0) {
@@ -7459,7 +7434,6 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t
g_clear_error (&tmp_err); g_clear_error (&tmp_err);
break; break;
} }
nmc->get_client (nmc);
nmc->nowait_flag = FALSE; nmc->nowait_flag = FALSE;
nmc->should_wait = TRUE; nmc->should_wait = TRUE;
@@ -7601,7 +7575,6 @@ get_ethernet_device_name (NmCli *nmc)
const GPtrArray *devices; const GPtrArray *devices;
int i; int i;
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client); devices = nm_client_get_devices (nmc->client);
for (i = 0; i < devices->len; i++) { for (i = 0; i < devices->len; i++) {
NMDevice *dev = g_ptr_array_index (devices, i); NMDevice *dev = g_ptr_array_index (devices, i);
@@ -7966,15 +7939,6 @@ do_connection_modify (NmCli *nmc,
nmc->should_wait = FALSE; nmc->should_wait = FALSE;
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto finish;
}
if (argc == 0) { if (argc == 0) {
g_string_printf (nmc->return_text, _("Error: No arguments provided.")); g_string_printf (nmc->return_text, _("Error: No arguments provided."));
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
@@ -8174,15 +8138,6 @@ do_connection_delete (NmCli *nmc, int argc, char **argv)
nmc->return_value = NMC_RESULT_SUCCESS; nmc->return_value = NMC_RESULT_SUCCESS;
nmc->should_wait = FALSE; nmc->should_wait = FALSE;
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto finish;
}
if (argc == 0) { if (argc == 0) {
if (nmc->ask) { if (nmc->ask) {
line = nmc_readline (PROMPT_CONNECTION); line = nmc_readline (PROMPT_CONNECTION);
@@ -8438,7 +8393,6 @@ do_connections (NmCli *nmc, int argc, char **argv)
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING; nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
return nmc->return_value; return nmc->return_value;
} }
/* Compare NM and nmcli versions */ /* Compare NM and nmcli versions */
if (!nmc_versions_match (nmc)) if (!nmc_versions_match (nmc))
return nmc->return_value; return nmc->return_value;

View File

@@ -1180,17 +1180,6 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
/* Add headers */ /* Add headers */
nmc->print_fields.header_name = _("Status of devices"); nmc->print_fields.header_name = _("Status of devices");
arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES);
@@ -1228,17 +1217,6 @@ do_devices_show (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = get_devices_sorted (nmc->client); devices = get_devices_sorted (nmc->client);
if (ifname) { if (ifname) {
@@ -1392,16 +1370,6 @@ do_device_connect (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = get_devices_sorted (nmc->client); devices = get_devices_sorted (nmc->client);
for (i = 0; devices[i]; i++) { for (i = 0; devices[i]; i++) {
NMDevice *candidate = devices[i]; NMDevice *candidate = devices[i];
@@ -1526,16 +1494,6 @@ do_device_disconnect (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = get_devices_sorted (nmc->client); devices = get_devices_sorted (nmc->client);
for (i = 0; devices[i]; i++) { for (i = 0; devices[i]; i++) {
NMDevice *candidate = devices[i]; NMDevice *candidate = devices[i];
@@ -1620,16 +1578,6 @@ do_device_delete (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = get_devices_sorted (nmc->client); devices = get_devices_sorted (nmc->client);
for (i = 0; devices[i]; i++) { for (i = 0; devices[i]; i++) {
NMDevice *candidate = devices[i]; NMDevice *candidate = devices[i];
@@ -1761,17 +1709,6 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = get_devices_sorted (nmc->client); devices = get_devices_sorted (nmc->client);
if (ifname) { if (ifname) {
/* Device specified - list only APs of this interface */ /* Device specified - list only APs of this interface */
@@ -2219,17 +2156,6 @@ do_device_wifi_connect_network (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = nm_client_get_devices (nmc->client); devices = nm_client_get_devices (nmc->client);
/* Find a device to activate the connection on */ /* Find a device to activate the connection on */
@@ -2393,7 +2319,6 @@ do_device_wifi_rescan (NmCli *nmc, int argc, char **argv)
} }
/* Find Wi-Fi device to scan on. When no ifname is provided, the first Wi-Fi is used. */ /* Find Wi-Fi device to scan on. When no ifname is provided, the first Wi-Fi is used. */
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client); devices = nm_client_get_devices (nmc->client);
devices_idx = 0; devices_idx = 0;
device = find_wifi_device_by_iface (devices, ifname, &devices_idx); device = find_wifi_device_by_iface (devices, ifname, &devices_idx);
@@ -2518,17 +2443,6 @@ do_device_wimax_list (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
nmc->get_client (nmc);
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
devices = nm_client_get_devices (nmc->client); devices = nm_client_get_devices (nmc->client);
if (ifname) { if (ifname) {
/* Device specified - list only NSPs of this interface */ /* Device specified - list only NSPs of this interface */
@@ -2745,6 +2659,19 @@ do_devices (NmCli *nmc, int argc, char **argv)
rl_attempted_completion_function = (rl_completion_func_t *) nmcli_device_tab_completion; rl_attempted_completion_function = (rl_completion_func_t *) nmcli_device_tab_completion;
/* Get NMClient object early */
nmc->get_client (nmc);
/* Check whether NetworkManager is runnung */
if (!nm_client_get_nm_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
return nmc->return_value;
}
/* Compare NM and nmcli versions */
if (!nmc_versions_match (nmc))
return nmc->return_value;
if (argc == 0) { if (argc == 0) {
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error))
goto opt_error; goto opt_error;