cli: print_fields() adjustments; error messages fixes
This commit is contained in:
@@ -215,7 +215,10 @@ do_connections_list (NmCli *nmc, int argc, char **argv)
|
|||||||
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
g_string_printf (nmc->return_text, error->message);
|
if (error->code == 0)
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'con list': %s"), error->message);
|
||||||
|
else
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'con list': %s; allowed fields: %s"), error->message, NMC_FIELDS_CON_LIST_ALL);
|
||||||
g_error_free (error);
|
g_error_free (error);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
goto error;
|
goto error;
|
||||||
@@ -391,7 +394,10 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
|
|||||||
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
g_string_printf (nmc->return_text, error->message);
|
if (error->code == 0)
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'con status': %s"), error->message);
|
||||||
|
else
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'con status': %s; allowed fields: %s"), error->message, NMC_FIELDS_CON_STATUS_ALL);
|
||||||
g_error_free (error);
|
g_error_free (error);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
goto error;
|
goto error;
|
||||||
@@ -824,9 +830,9 @@ find_device_for_connection (NmCli *nmc, NMConnection *connection, const char *if
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
} else {
|
} else {
|
||||||
if (iface)
|
if (iface)
|
||||||
g_set_error (error, 0, 0, "device '%s' not compatible with connection '%s'", iface, nm_setting_connection_get_id (s_con));
|
g_set_error (error, 0, 0, _("device '%s' not compatible with connection '%s'"), iface, nm_setting_connection_get_id (s_con));
|
||||||
else
|
else
|
||||||
g_set_error (error, 0, 0, "no device found for connection '%s'", nm_setting_connection_get_id (s_con));
|
g_set_error (error, 0, 0, _("no device found for connection '%s'"), nm_setting_connection_get_id (s_con));
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -540,7 +540,10 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
|
|||||||
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
g_string_printf (nmc->return_text, error->message);
|
if (error->code == 0)
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'dev status': %s"), error->message);
|
||||||
|
else
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'dev status': %s; allowed fields: %s"), error->message, NMC_FIELDS_DEV_STATUS_ALL);
|
||||||
g_error_free (error);
|
g_error_free (error);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
goto error;
|
goto error;
|
||||||
@@ -840,7 +843,10 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
|
|||||||
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
g_string_printf (nmc->return_text, error->message);
|
if (error->code == 0)
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'dev wifi': %s"), error->message);
|
||||||
|
else
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'dev wifi': %s; allowed fields: %s"), error->message, NMC_FIELDS_DEV_WIFI_LIST_ALL);
|
||||||
g_error_free (error);
|
g_error_free (error);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
goto error;
|
goto error;
|
||||||
|
@@ -121,7 +121,10 @@ show_nm_status (NmCli *nmc)
|
|||||||
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
nmc->print_fields.indices = parse_output_fields (fields_str, nmc->allowed_fields, &error);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
g_string_printf (nmc->return_text, error->message);
|
if (error->code == 0)
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'nm status': %s"), error->message);
|
||||||
|
else
|
||||||
|
g_string_printf (nmc->return_text, _("Error: 'nm status': %s; allowed fields: %s"), error->message, NMC_FIELDS_NM_STATUS_ALL);
|
||||||
g_error_free (error);
|
g_error_free (error);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
return nmc->return_value;
|
return nmc->return_value;
|
||||||
@@ -188,7 +191,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
|
|||||||
if (next_arg (&argc, &argv) != 0) {
|
if (next_arg (&argc, &argv) != 0) {
|
||||||
/* no argument, show current WiFi state */
|
/* no argument, show current WiFi state */
|
||||||
if (nmc->required_fields && strcasecmp (nmc->required_fields, "WIFI")) {
|
if (nmc->required_fields && strcasecmp (nmc->required_fields, "WIFI")) {
|
||||||
g_string_printf (nmc->return_text, _("Error: '--fields' value '%s' is not valid here; allowed fields: '%s'"),
|
g_string_printf (nmc->return_text, _("Error: '--fields' value '%s' is not valid here; allowed fields: %s"),
|
||||||
nmc->required_fields, NMC_FIELDS_NM_WIFI);
|
nmc->required_fields, NMC_FIELDS_NM_WIFI);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
goto end;
|
goto end;
|
||||||
@@ -218,7 +221,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
|
|||||||
if (next_arg (&argc, &argv) != 0) {
|
if (next_arg (&argc, &argv) != 0) {
|
||||||
/* no argument, show current WWAN state */
|
/* no argument, show current WWAN state */
|
||||||
if (nmc->required_fields && strcasecmp (nmc->required_fields, "WWAN")) {
|
if (nmc->required_fields && strcasecmp (nmc->required_fields, "WWAN")) {
|
||||||
g_string_printf (nmc->return_text, _("Error: '--fields' value '%s' is not valid here; allowed fields: '%s'"),
|
g_string_printf (nmc->return_text, _("Error: '--fields' value '%s' is not valid here; allowed fields: %s"),
|
||||||
nmc->required_fields, NMC_FIELDS_NM_WWAN);
|
nmc->required_fields, NMC_FIELDS_NM_WWAN);
|
||||||
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
|
||||||
goto end;
|
goto end;
|
||||||
|
@@ -52,6 +52,7 @@ next_arg (int *argc, char ***argv)
|
|||||||
* IN: 'field_str': comma-separated fields names
|
* IN: 'field_str': comma-separated fields names
|
||||||
* 'fields_array': array of allowed fields
|
* 'fields_array': array of allowed fields
|
||||||
* RETURN: GArray with indices representing fields in 'fields_array'.
|
* RETURN: GArray with indices representing fields in 'fields_array'.
|
||||||
|
* Caller is responsible to free it.
|
||||||
*/
|
*/
|
||||||
GArray *
|
GArray *
|
||||||
parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error)
|
parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error)
|
||||||
@@ -75,10 +76,10 @@ parse_output_fields (const char *fields_str, const NmcOutputField fields_array[]
|
|||||||
}
|
}
|
||||||
if (fields_array[i].name == NULL) {
|
if (fields_array[i].name == NULL) {
|
||||||
if (!strcasecmp (*iter, "all") || !strcasecmp (*iter, "common"))
|
if (!strcasecmp (*iter, "all") || !strcasecmp (*iter, "common"))
|
||||||
g_set_error (error, 0, 0, _("Error: 'con status': field '%s' has to be alone."), *iter);
|
g_set_error (error, 0, 0, _("field '%s' has to be alone"), *iter);
|
||||||
|
|
||||||
else
|
else
|
||||||
g_set_error (error, 0, 0, _("Error: 'con status': invalid field '%s'."), *iter);
|
g_set_error (error, 0, 1, _("invalid field '%s'"), *iter);
|
||||||
g_array_free (array, TRUE);
|
g_array_free (array, TRUE);
|
||||||
array = NULL;
|
array = NULL;
|
||||||
goto done;
|
goto done;
|
||||||
@@ -90,6 +91,12 @@ done:
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print both headers or values of 'field_values' array.
|
||||||
|
* Entries to print and their order are specified via indices
|
||||||
|
* in 'fields.indices' array.
|
||||||
|
* 'fields.flags' specify various aspects influencing the output.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
||||||
{
|
{
|
||||||
@@ -106,16 +113,21 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
gboolean header = fields.flags & NMC_PF_FLAG_HEADER;
|
gboolean header = fields.flags & NMC_PF_FLAG_HEADER;
|
||||||
gboolean escape = fields.flags & NMC_PF_FLAG_ESCAPE;
|
gboolean escape = fields.flags & NMC_PF_FLAG_ESCAPE;
|
||||||
|
|
||||||
/* Headers are not printed in terse mode */
|
/* No headers are printed in terse mode:
|
||||||
|
* - neither whole name header nor field (column) names
|
||||||
|
*/
|
||||||
if (header && terse)
|
if (header && terse)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (multiline) {
|
if (multiline) {
|
||||||
/* --- Multiline mode --- */
|
/* --- Multiline mode --- */
|
||||||
|
enum { ML_HEADER_WIDTH = 79 };
|
||||||
if (header && pretty) {
|
if (header && pretty) {
|
||||||
/* Print the table header */
|
/* Print the table header */
|
||||||
table_width = g_utf8_strlen (fields.header_name, -1) + 4;
|
int header_width = g_utf8_strlen (fields.header_name, -1) + 4;
|
||||||
line = g_strnfill (79, '=');
|
table_width = header_width < ML_HEADER_WIDTH ? ML_HEADER_WIDTH : header_width;
|
||||||
|
|
||||||
|
line = g_strnfill (ML_HEADER_WIDTH, '=');
|
||||||
width1 = strlen (fields.header_name);
|
width1 = strlen (fields.header_name);
|
||||||
width2 = g_utf8_strlen (fields.header_name, -1);
|
width2 = g_utf8_strlen (fields.header_name, -1);
|
||||||
printf ("%s\n", line);
|
printf ("%s\n", line);
|
||||||
@@ -134,7 +146,7 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
g_free (tmp);
|
g_free (tmp);
|
||||||
}
|
}
|
||||||
if (pretty) {
|
if (pretty) {
|
||||||
line = g_strnfill (79, '-');
|
line = g_strnfill (ML_HEADER_WIDTH, '-');
|
||||||
printf ("%s\n", line);
|
printf ("%s\n", line);
|
||||||
g_free (line);
|
g_free (line);
|
||||||
}
|
}
|
||||||
@@ -175,11 +187,11 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (table_width <= 0)
|
/* Print the table header */
|
||||||
table_width = g_utf8_strlen (fields.header_name, -1) + 4;
|
|
||||||
|
|
||||||
if (header && pretty) {
|
if (header && pretty) {
|
||||||
/* Print the table header */
|
int header_width = g_utf8_strlen (fields.header_name, -1) + 4;
|
||||||
|
table_width = table_width < header_width ? header_width : table_width;
|
||||||
|
|
||||||
line = g_strnfill (table_width, '=');
|
line = g_strnfill (table_width, '=');
|
||||||
width1 = strlen (fields.header_name);
|
width1 = strlen (fields.header_name);
|
||||||
width2 = g_utf8_strlen (fields.header_name, -1);
|
width2 = g_utf8_strlen (fields.header_name, -1);
|
||||||
@@ -189,18 +201,18 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
g_free (line);
|
g_free (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Print actual values */
|
||||||
/* Print the line */
|
|
||||||
if (str->len > 0) {
|
if (str->len > 0) {
|
||||||
g_string_truncate (str, str->len-1); /* Chop off last column separator */
|
g_string_truncate (str, str->len-1); /* Chop off last column separator */
|
||||||
if (fields.indent > 0) {
|
if (fields.indent > 0) {
|
||||||
indent_str = g_strnfill (fields.indent, ' ');
|
indent_str = g_strnfill (fields.indent, ' ');
|
||||||
g_string_prepend (str, indent_str);
|
g_string_prepend (str, indent_str);
|
||||||
g_free (indent_str);
|
g_free (indent_str);
|
||||||
}
|
}
|
||||||
printf ("%s\n", str->str);
|
printf ("%s\n", str->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Print horizontal separator */
|
||||||
if (header && pretty) {
|
if (header && pretty) {
|
||||||
if (str->len > 0) {
|
if (str->len > 0) {
|
||||||
line = g_strnfill (table_width, '-');
|
line = g_strnfill (table_width, '-');
|
||||||
|
Reference in New Issue
Block a user