cli: add property completion to meta-data

(not used yet).
This commit is contained in:
Thomas Haller 2017-04-10 16:38:35 +02:00
parent 4f19c46a1e
commit cfd9f66ae9
5 changed files with 113 additions and 9 deletions

View file

@ -3846,13 +3846,6 @@ ensure_settings (NMConnection *connection, const NameItem *item)
/*----------------------------------------------------------------------------*/
static char *
gen_func_slave_type (const char *text, int state)
{
const char *words[] = { "bond", "team", "bridge", NULL };
return nmc_rl_gen_func_basic (text, state, words);
}
static char *
gen_func_vpn_types (const char *text, int state)
{
@ -4306,7 +4299,6 @@ _meta_abstract_get_option_info (const NMMetaAbstractInfo *abstract_info)
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_AUTOCONNECT, "autoconnect", NULL, gen_func_bool_values_l10n),
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_INTERFACE_NAME, "ifname", set_connection_iface, nmc_rl_gen_func_ifnames),
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_MASTER, "master", set_connection_master, gen_func_master_ifnames),
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_SLAVE_TYPE, "slave-type", NULL, gen_func_slave_type),
OPTION_INFO (INFINIBAND, NM_SETTING_INFINIBAND_TRANSPORT_MODE, "transport-mode", NULL, gen_func_ib_type),
OPTION_INFO (WIRELESS, NM_SETTING_WIRELESS_MODE, "mode", NULL, gen_func_wifi_mode),
OPTION_INFO (BLUETOOTH, NM_SETTING_BLUETOOTH_TYPE, "bt-type", set_bluetooth_type, gen_func_bt_type),
@ -4462,6 +4454,15 @@ static void
complete_option (const NMMetaAbstractInfo *abstract_info, const gchar *prefix)
{
const OptionInfo *candidate;
const char *const*values;
gs_strfreev char **values_to_free = NULL;
values = nm_meta_abstract_info_complete (abstract_info, prefix, &values_to_free);
if (values) {
for (; values[0]; values++)
g_print ("%s\n", values[0]);
return;
}
candidate = _meta_abstract_get_option_info (abstract_info);
if (candidate && candidate->generator_func)
@ -4513,7 +4514,6 @@ complete_property (const gchar *setting_name, const gchar *property, const gchar
} else if ( strcmp (setting_name, NM_SETTING_VXLAN_SETTING_NAME) == 0
&& strcmp (property, NM_SETTING_VXLAN_PARENT) == 0)
run_rl_generator (nmc_rl_gen_func_ifnames, prefix);
}
/*----------------------------------------------------------------------------*/

View file

@ -253,3 +253,68 @@ nm_meta_abstract_info_get (const NMMetaAbstractInfo *abstract_info,
out_flags,
out_to_free);
}
const char *const*
nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abstract_info,
const char *text,
char ***out_to_free)
{
const char *const*values;
gsize i, j, text_len;
nm_assert (abstract_info);
nm_assert (abstract_info->meta_type);
nm_assert (out_to_free && !*out_to_free);
*out_to_free = NULL;
if (!abstract_info->meta_type->complete_fcn)
return NULL;
values = abstract_info->meta_type->complete_fcn (abstract_info,
text,
out_to_free);
nm_assert (!*out_to_free || values == (const char *const*) *out_to_free);
if (!text || !text[0] || !values || !values[0])
return values;
/* for convenience, we all the complete_fcn() implementations to
* ignore "text". We filter out invalid matches here. */
text_len = strlen (text);
if (*out_to_free) {
char **v = *out_to_free;
for (i =0, j = 0; v[i]; i++) {
if (strncmp (v[i], text, text_len) != 0)
continue;
v[j++] = v[i];
}
v[j++] = NULL;
return (const char *const*) *out_to_free;
} else {
const char *const*v = values;
char **r;
for (i = 0, j = 0; v[i]; i++) {
if (strncmp (v[i], text, text_len) != 0)
continue;
j++;
}
if (j == i)
return values;
r = g_new (char *, j + 1);
v = values;
for (i = 0, j = 0; v[i]; i++) {
if (strncmp (v[i], text, text_len) != 0)
continue;
r[j++] = g_strdup (v[i]);
}
r[j++] = NULL;
return (const char *const*) (*out_to_free = r);
}
}

View file

@ -59,6 +59,10 @@ gconstpointer nm_meta_abstract_info_get (const NMMetaAbstractInfo *abstract_info
NMMetaAccessorGetOutFlags *out_flags,
gpointer *out_to_free);
const char *const*nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abstract_info,
const char *text,
char ***out_to_free);
/*****************************************************************************/
#endif /* _NM_META_SETTING_ACCESS_H__ */

View file

@ -7210,6 +7210,33 @@ _meta_type_property_info_get_nested (const NMMetaAbstractInfo *abstract_info,
return NULL;
}
static const char *const*
_meta_type_property_info_complete_fcn (const NMMetaAbstractInfo *abstract_info,
const char *text,
char ***out_to_free)
{
const NMMetaPropertyInfo *info = (const NMMetaPropertyInfo *) abstract_info;
nm_assert (out_to_free && !*out_to_free);
if (info->property_type->complete_fcn) {
return info->property_type->complete_fcn (info,
text,
out_to_free);
}
if (info->property_type->values_fcn) {
return info->property_type->values_fcn (info,
out_to_free);
}
if ( info->property_typ_data
&& info->property_typ_data->values_static)
return info->property_typ_data->values_static;
return NULL;
}
const NMMetaType nm_meta_type_setting_info_editor = {
.type_name = "setting_info_editor",
.get_name = _meta_type_setting_info_editor_get_name,
@ -7222,6 +7249,7 @@ const NMMetaType nm_meta_type_property_info = {
.get_name = _meta_type_property_info_get_name,
.get_nested = _meta_type_property_info_get_nested,
.get_fcn = _meta_type_property_info_get_fcn,
.complete_fcn = _meta_type_property_info_complete_fcn,
};
const NMMetaType nm_meta_type_nested_property_info = {

View file

@ -195,6 +195,10 @@ struct _NMMetaPropertyType {
const char *const*(*values_fcn) (const NMMetaPropertyInfo *property_info,
char ***out_to_free);
const char *const*(*complete_fcn) (const NMMetaPropertyInfo *property_info,
const char *text,
char ***out_to_free);
};
struct _NMUtilsEnumValueInfo;
@ -284,6 +288,9 @@ struct _NMMetaType {
NMMetaAccessorGetFlags get_flags,
NMMetaAccessorGetOutFlags *out_flags,
gpointer *out_to_free);
const char *const*(*complete_fcn) (const NMMetaAbstractInfo *info,
const char *text,
char ***out_to_free);
};
struct _NMMetaAbstractInfo {