Commit 3045daf1 authored by Thomas Haller's avatar Thomas Haller

cli: use nmc_print() to output setting data

parent fdd75811
......@@ -709,7 +709,7 @@ update_secrets_in_connection (NMRemoteConnection *remote, NMConnection *local)
}
static gboolean
nmc_connection_profile_details (NMConnection *connection, NmCli *nmc, gboolean secrets)
nmc_connection_profile_details (NMConnection *connection, NmCli *nmc)
{
GError *error = NULL;
GArray *print_settings_array;
......@@ -771,7 +771,7 @@ nmc_connection_profile_details (NMConnection *connection, NmCli *nmc, gboolean s
setting = nm_connection_get_setting_by_name (connection, nm_meta_setting_infos_editor[section_idx].general->setting_name);
if (setting) {
setting_details (&nmc->nmc_config, setting, prop_name, secrets);
setting_details (&nmc->nmc_config, setting, prop_name);
was_output = TRUE;
}
}
......@@ -1875,7 +1875,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
nmc->required_fields = profile_flds;
if (nmc->nmc_config.show_secrets)
update_secrets_in_connection (NM_REMOTE_CONNECTION (con), con);
res = nmc_connection_profile_details (con, nmc, nmc->nmc_config.show_secrets);
res = nmc_connection_profile_details (con, nmc);
nmc->required_fields = NULL;
if (!res)
goto finish;
......@@ -1893,7 +1893,7 @@ do_connections_show (NmCli *nmc, int argc, char **argv)
}
}
new_line = TRUE;
/* Take next argument.
* But for pos != NULL we have more connections of the same name,
* so process the same argument again.
......@@ -3187,14 +3187,11 @@ get_valid_properties_string (const NameItem *array,
/* Search the array with the arguments of the current property */
setting_info = nm_meta_setting_info_editor_find_by_name (iter->name);
j = 0;
while (TRUE) {
for (j = 0; j < setting_info->properties_num; j++) {
gchar *new;
const char *arg_name;
if (j + 1 >= setting_info->properties_num)
break;
arg_name = setting_info->properties[j + 1].property_name;
arg_name = setting_info->properties[j].property_name;
/* If required, expand the alias too */
if (!postfix && iter->alias) {
......@@ -3207,10 +3204,8 @@ get_valid_properties_string (const NameItem *array,
g_free (new);
}
if (postfix && !g_str_has_prefix (arg_name, postfix)) {
j++;
if (postfix && !g_str_has_prefix (arg_name, postfix))
continue;
}
if (modifier)
g_string_append_c (str, modifier);
......@@ -3219,7 +3214,6 @@ get_valid_properties_string (const NameItem *array,
arg_name);
g_string_append (str, new);
g_free (new);
j++;
}
iter++;
}
......@@ -5969,7 +5963,7 @@ editor_show_connection (NMConnection *connection, NmCli *nmc)
nmc->nmc_config_mutable.multiline_output = TRUE;
nmc->nmc_config_mutable.escape_values = 0;
nmc_connection_profile_details (connection, nmc, nmc->nmc_config.show_secrets);
nmc_connection_profile_details (connection, nmc);
}
static void
......@@ -5982,7 +5976,7 @@ editor_show_setting (NMSetting *setting, NmCli *nmc)
nmc->nmc_config_mutable.multiline_output = TRUE;
nmc->nmc_config_mutable.escape_values = 0;
setting_details (&nmc->nmc_config, setting, NULL, nmc->nmc_config.show_secrets);
setting_details (&nmc->nmc_config, setting, NULL);
}
typedef enum {
......
......@@ -75,8 +75,6 @@ complete_field_setting (GHashTable *h, NMMetaSettingType setting_type)
guint i;
for (i = 0; i < setting_info->properties_num; i++) {
if (setting_info->properties[i].is_name)
continue;
g_hash_table_add (h, g_strdup_printf ("%s.%s",
setting_info->general->setting_name,
setting_info->properties[i].property_name));
......
......@@ -471,10 +471,7 @@ get_property_val (NMSetting *setting, const char *prop, NMMetaAccessorGetType ge
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if ((property_info = nm_meta_property_info_find_by_setting (setting, prop))) {
if (property_info->is_name) {
/* Traditionally, the "name" property was not handled here.
* For the moment, skip it from get_property_val(). */
} else if (property_info->property_type->get_fcn) {
if (property_info->property_type->get_fcn) {
return property_info->property_type->get_fcn (&meta_environment,
NULL,
property_info,
......@@ -549,10 +546,7 @@ nmc_setting_set_property (NMSetting *setting, const char *prop, const char *valu
return TRUE;
}
if (property_info->is_name) {
/* Traditionally, the "name" property was not handled here.
* For the moment, skip it from get_property_val(). */
} else if (property_info->property_type->set_fcn) {
if (property_info->property_type->set_fcn) {
switch (property_info->setting_info->general->meta_type) {
case NM_META_SETTING_TYPE_CONNECTION:
if (nm_streq (property_info->property_name, NM_SETTING_CONNECTION_SECONDARIES)) {
......@@ -613,10 +607,7 @@ nmc_setting_reset_property (NMSetting *setting, const char *prop, GError **error
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if ((property_info = nm_meta_property_info_find_by_setting (setting, prop))) {
if (property_info->is_name) {
/* Traditionally, the "name" property was not handled here.
* For the moment, skip it from get_property_val(). */
} else if (property_info->property_type->set_fcn) {
if (property_info->property_type->set_fcn) {
nmc_property_set_default_value (setting, prop);
return TRUE;
}
......@@ -648,10 +639,7 @@ nmc_setting_remove_property_option (NMSetting *setting,
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if ((property_info = nm_meta_property_info_find_by_setting (setting, prop))) {
if (property_info->is_name) {
/* Traditionally, the "name" property was not handled here.
* For the moment, skip it from get_property_val(). */
} else if (property_info->property_type->remove_fcn) {
if (property_info->property_type->remove_fcn) {
return property_info->property_type->remove_fcn (&meta_environment,
NULL,
property_info,
......@@ -707,10 +695,7 @@ nmc_setting_get_property_allowed_values (NMSetting *setting, const char *prop, c
*out_to_free = NULL;
if ((property_info = nm_meta_property_info_find_by_setting (setting, prop))) {
if (property_info->is_name) {
/* Traditionally, the "name" property was not handled here.
* For the moment, skip it from get_property_val(). */
} else if (property_info->property_type->values_fcn) {
if (property_info->property_type->values_fcn) {
return property_info->property_type->values_fcn (property_info,
out_to_free);
} else if (property_info->property_typ_data && property_info->property_typ_data->values_static)
......@@ -751,10 +736,7 @@ nmc_setting_get_property_desc (NMSetting *setting, const char *prop)
setting_desc_title = _("[NM property description]");
}
if (property_info->is_name) {
/* Traditionally, the "name" property was not handled here.
* For the moment, skip it from get_property_val(). */
} else if (property_info->property_type->describe_fcn) {
if (property_info->property_type->describe_fcn) {
desc = property_info->property_type->describe_fcn (property_info, &desc_to_free);
} else
desc = property_info->describe_message;
......@@ -809,29 +791,12 @@ nmc_property_set_gvalue (NMSetting *setting, const char *prop, GValue *value)
/*****************************************************************************/
static NmcOutputField *
_dup_fields_array (const NMMetaSettingInfoEditor *setting_info, NmcOfFlags flags)
{
NmcOutputField *row;
gsize l;
l = setting_info->properties_num;
row = g_malloc0 ((l + 1) * sizeof (NmcOutputField));
for (l = 0; l < setting_info->properties_num; l++)
row[l].info = (const NMMetaAbstractInfo *) &setting_info->properties[l];
row[0].flags = flags;
return row;
}
gboolean
setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *one_prop, gboolean show_secrets)
setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *one_prop)
{
const NMMetaSettingInfoEditor *setting_info;
NmcOutputField *arr;
guint i;
NMMetaAccessorGetType type = NM_META_ACCESSOR_GET_TYPE_PRETTY;
NMC_OUTPUT_DATA_DEFINE_SCOPED (out);
gs_free_error GError *error = NULL;
gs_free char *fields_str = NULL;
g_return_val_if_fail (NM_IS_SETTING (setting), FALSE);
......@@ -839,36 +804,20 @@ setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *on
if (!setting_info)
return FALSE;
if (nmc_config->print_output == NMC_PRINT_TERSE)
type = NM_META_ACCESSOR_GET_TYPE_PARSABLE;
out_indices = parse_output_fields (one_prop,
(const NMMetaAbstractInfo *const*) nm_property_infos_for_setting_type (setting_info->general->meta_type),
FALSE, NULL, NULL);
arr = _dup_fields_array (setting_info, NMC_OF_FLAG_FIELD_NAMES);
g_ptr_array_add (out.output_data, arr);
arr = _dup_fields_array (setting_info, NMC_OF_FLAG_SECTION_PREFIX);
for (i = 0; i < setting_info->properties_num; i++) {
const NMMetaPropertyInfo *property_info = &setting_info->properties[i];
nm_assert (property_info->setting_info == setting_info);
if (!property_info->is_secret || show_secrets) {
set_val_str (arr, i, property_info->property_type->get_fcn (&meta_environment,
NULL,
property_info,
setting,
type,
show_secrets ? NM_META_ACCESSOR_GET_FLAGS_SHOW_SECRETS : 0));
} else
set_val_str (arr, i, g_strdup (_(NM_META_TEXT_HIDDEN)));
if (one_prop) {
/* hack around setting-details being called for one setting. Must prefix the
* property name with the setting name. Later we should remove setting_details()
* and merge it into the caller. */
fields_str = g_strdup_printf ("%s.%s", nm_setting_get_name (setting), one_prop);
}
g_ptr_array_add (out.output_data, arr);
print_data_prepare_width (out.output_data);
print_data (nmc_config, out_indices, NULL, 0, &out);
if (!nmc_print (nmc_config,
(gpointer[]) { setting, NULL },
NULL,
(const NMMetaAbstractInfo *const[]) { (const NMMetaAbstractInfo *) setting_info, NULL },
fields_str,
&error))
return FALSE;
return TRUE;
}
......@@ -60,6 +60,6 @@ void nmc_property_set_default_value (NMSetting *setting, const char *prop);
gboolean nmc_property_get_gvalue (NMSetting *setting, const char *prop, GValue *value);
gboolean nmc_property_set_gvalue (NMSetting *setting, const char *prop, GValue *value);
gboolean setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *one_prop, gboolean secrets);
gboolean setting_details (const NmcConfig *nmc_config, NMSetting *setting, const char *one_prop);
#endif /* NMC_SETTINGS_H */
......@@ -834,7 +834,7 @@ _output_selection_select_one (const NMMetaAbstractInfo *const* fields_array,
if (fi->meta_type == &nm_meta_type_setting_info_editor) {
const NMMetaSettingInfoEditor *fi_s = &fi->as.setting_info;
for (j = 1; j < fi_s->properties_num; j++) {
for (j = 0; j < fi_s->properties_num; j++) {
if (g_ascii_strcasecmp (right, fi_s->properties[j].property_name) == 0) {
found = TRUE;
break;
......@@ -1037,7 +1037,9 @@ _output_selection_append (GArray *cols,
si, gfree_keeper, error))
return FALSE;
}
g_array_index (cols, PrintDataCol, col_idx).is_leaf = FALSE;
if (selection_item->info->meta_type != &nm_meta_type_setting_info_editor)
g_array_index (cols, PrintDataCol, col_idx).is_leaf = FALSE;
}
return TRUE;
......@@ -1282,6 +1284,7 @@ typedef struct {
const PrintDataCol *col;
bool is_nested;
const char *title;
bool title_to_free:1;
int width;
} PrintDataHeaderCell;
......@@ -1297,6 +1300,13 @@ typedef struct {
static void
_print_data_header_cell_clear (gpointer cell_p)
{
PrintDataHeaderCell *cell = cell_p;
if (cell->title_to_free) {
g_free ((char *) cell->title);
cell->title_to_free = FALSE;
}
cell->title = NULL;
}
static void
......@@ -1331,6 +1341,7 @@ _print_fill (const NmcConfig *nmc_config,
guint targets_len;
gboolean pretty;
NMMetaAccessorGetType text_get_type;
NMMetaAccessorGetFlags text_get_flags;
pretty = (nmc_config->print_output != NMC_PRINT_TERSE);
......@@ -1341,11 +1352,15 @@ _print_fill (const NmcConfig *nmc_config,
const PrintDataCol *col;
PrintDataHeaderCell *header_cell;
guint col_idx;
const NMMetaAbstractInfo *info;
gboolean translate_title;
col = &cols[i_col];
if (!col->is_leaf)
continue;
info = col->selection_item->info;
col_idx = header_row->len;
g_array_set_size (header_row, col_idx + 1);
......@@ -1354,8 +1369,23 @@ _print_fill (const NmcConfig *nmc_config,
header_cell->col_idx = col_idx;
header_cell->col = col;
header_cell->is_nested = FALSE;
header_cell->title = nm_meta_abstract_info_get_name (col->selection_item->info);
if (pretty)
translate_title = pretty;
if (info->meta_type == &nm_meta_type_property_info) {
header_cell->title = nm_meta_abstract_info_get_name (info);
if (nmc_config->multiline_output) {
header_cell->title = g_strdup_printf ("%s.%s",
((const NMMetaPropertyInfo *) info)->setting_info->general->setting_name,
header_cell->title);
header_cell->title_to_free = TRUE;
}
} else if (info->meta_type == &nm_meta_type_setting_info_editor)
header_cell->title = N_("name");
else
header_cell->title = nm_meta_abstract_info_get_name (info);
if (translate_title)
header_cell->title = _(header_cell->title);
}
......@@ -1368,6 +1398,9 @@ _print_fill (const NmcConfig *nmc_config,
text_get_type = pretty
? NM_META_ACCESSOR_GET_TYPE_PRETTY
: NM_META_ACCESSOR_GET_TYPE_PARSABLE;
text_get_flags = NM_META_ACCESSOR_GET_FLAGS_NONE;
if (nmc_config->show_secrets)
text_get_flags |= NM_META_ACCESSOR_GET_FLAGS_SHOW_SECRETS;
for (i_row = 0; i_row < targets_len; i_row++) {
gpointer target = targets[i_row];
......@@ -1389,7 +1422,7 @@ _print_fill (const NmcConfig *nmc_config,
NULL,
target,
text_get_type,
NM_META_ACCESSOR_GET_FLAGS_NONE,
text_get_flags,
(gpointer *) &to_free);
cell->text_to_free = !!to_free;
......@@ -1434,6 +1467,26 @@ _print_fill (const NmcConfig *nmc_config,
*out_cells = cells;
}
static gboolean
_print_skip_column (const NmcConfig *nmc_config,
const PrintDataHeaderCell *header_cell)
{
if (nmc_config->multiline_output) {
if (header_cell->col->selection_item->info->meta_type == &nm_meta_type_setting_info_editor) {
/* we skip the "name" entry for the setting in multiline output. */
return TRUE;
}
} else {
if ( header_cell->col->selection_item->info->meta_type == &nm_meta_type_setting_info_editor
&& header_cell->col->selection_item->sub_selection) {
/* in tabular form, we skip the "name" entry for sections that have sub-selections.
* That is, for "ipv4.may-fail", but not for "ipv4". */
return TRUE;
}
}
return FALSE;
}
static void
_print_do (const NmcConfig *nmc_config,
const char *header_name_no_l10n,
......@@ -1453,7 +1506,7 @@ _print_do (const NmcConfig *nmc_config,
g_assert (col_len && row_len);
/* Main header */
if (pretty) {
if (pretty && header_name_no_l10n) {
gs_free char *line = NULL;
int header_width;
const char *header_name = _(header_name_no_l10n);
......@@ -1485,6 +1538,9 @@ _print_do (const NmcConfig *nmc_config,
const PrintDataHeaderCell *header_cell = &header_row[i_col];
const char *title;
if (_print_skip_column (nmc_config, header_cell))
continue;
title = header_cell->title;
width1 = strlen (title);
......@@ -1515,6 +1571,9 @@ _print_do (const NmcConfig *nmc_config,
gs_free char *text_to_free = NULL;
const char *text;
if (_print_skip_column (nmc_config, cell->header_cell))
continue;
if (cell->header_cell->is_nested) {
g_assert_not_reached (/*TODO*/);
} else {
......@@ -1522,7 +1581,6 @@ _print_do (const NmcConfig *nmc_config,
cell->term_color, cell->term_format,
cell->text, &text_to_free);
}
if (multiline) {
gs_free char *prefix = NULL;
......
This diff is collapsed.
......@@ -167,12 +167,6 @@ struct _NMMetaPropertyInfo {
const char *property_name;
/* the property list for now must contain as first field the
* "name", which isn't a regular property. This is required by
* NmcOutputField and this first field is ignored for the
* group_list/setting_info. */
bool is_name:1;
bool is_secret:1;
const char *describe_doc;
......
......@@ -85,11 +85,6 @@ test_client_meta_check (void)
g_assert (pi->property_name == pi->meta_type->get_name ((const NMMetaAbstractInfo *) pi));
if (pi->is_name)
g_assert (p == 0);
else
g_assert (p != 0);
g_assert (pi->property_type);
g_assert (pi->property_type->get_fcn);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment