cli: move completion for devices names to meta data
This commit is contained in:
@@ -3347,7 +3347,11 @@ _meta_abstract_complete (const NMMetaAbstractInfo *abstract_info, const char *te
|
||||
const char *const*values;
|
||||
char **values_to_free = NULL;
|
||||
|
||||
values = nm_meta_abstract_info_complete (abstract_info, text, &values_to_free);
|
||||
values = nm_meta_abstract_info_complete (abstract_info,
|
||||
nmc_meta_environment,
|
||||
nmc_meta_environment_arg,
|
||||
text,
|
||||
&values_to_free);
|
||||
if (values)
|
||||
return values_to_free ?: g_strdupv ((char **) values);
|
||||
return NULL;
|
||||
@@ -4031,10 +4035,9 @@ _meta_abstract_get_option_info (const NMMetaAbstractInfo *abstract_info)
|
||||
.generator_func = generator_func_, \
|
||||
}
|
||||
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_TYPE, "type", set_connection_type, gen_connection_types),
|
||||
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_INTERFACE_NAME, "ifname", set_connection_iface, nmc_rl_gen_func_ifnames),
|
||||
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_INTERFACE_NAME, "ifname", set_connection_iface, NULL),
|
||||
OPTION_INFO (CONNECTION, NM_SETTING_CONNECTION_MASTER, "master", set_connection_master, gen_func_master_ifnames),
|
||||
OPTION_INFO (BLUETOOTH, NM_SETTING_BLUETOOTH_TYPE, "bt-type", set_bluetooth_type, gen_func_bt_type),
|
||||
OPTION_INFO (VLAN, NM_SETTING_VLAN_PARENT, "dev", NULL, nmc_rl_gen_func_ifnames),
|
||||
OPTION_INFO (BOND, NM_SETTING_BOND_OPTIONS, "mode", set_bond_option, gen_func_bond_mode),
|
||||
OPTION_INFO (BOND, NM_SETTING_BOND_OPTIONS, "primary", set_bond_option, nmc_rl_gen_func_ifnames),
|
||||
OPTION_INFO (BOND, NM_SETTING_BOND_OPTIONS, NULL, set_bond_monitoring_mode, gen_func_bond_mon_mode),
|
||||
@@ -4044,9 +4047,6 @@ _meta_abstract_get_option_info (const NMMetaAbstractInfo *abstract_info)
|
||||
OPTION_INFO (BOND, NM_SETTING_BOND_OPTIONS, "arp-interval", set_bond_option, NULL),
|
||||
OPTION_INFO (BOND, NM_SETTING_BOND_OPTIONS, "arp-ip-target", set_bond_option, NULL),
|
||||
OPTION_INFO (BOND, NM_SETTING_BOND_OPTIONS, "lacp-rate", set_bond_option, gen_func_bond_lacp_rate),
|
||||
OPTION_INFO (MACVLAN, NM_SETTING_MACVLAN_PARENT, "dev", NULL, nmc_rl_gen_func_ifnames),
|
||||
OPTION_INFO (VXLAN, NM_SETTING_VXLAN_PARENT, "dev", NULL, nmc_rl_gen_func_ifnames),
|
||||
OPTION_INFO (IP_TUNNEL, NM_SETTING_IP_TUNNEL_PARENT, "dev", NULL, nmc_rl_gen_func_ifnames),
|
||||
OPTION_INFO (IP4_CONFIG, NM_SETTING_IP_CONFIG_ADDRESSES, "ip4", set_ip4_address, NULL),
|
||||
OPTION_INFO (IP6_CONFIG, NM_SETTING_IP_CONFIG_ADDRESSES, "ip6", set_ip6_address, NULL),
|
||||
{ 0 },
|
||||
@@ -4170,7 +4170,11 @@ complete_option (const NMMetaAbstractInfo *abstract_info, const gchar *prefix)
|
||||
const char *const*values;
|
||||
gs_strfreev char **values_to_free = NULL;
|
||||
|
||||
values = nm_meta_abstract_info_complete (abstract_info, prefix, &values_to_free);
|
||||
values = nm_meta_abstract_info_complete (abstract_info,
|
||||
nmc_meta_environment,
|
||||
nmc_meta_environment_arg,
|
||||
prefix,
|
||||
&values_to_free);
|
||||
if (values) {
|
||||
for (; values[0]; values++)
|
||||
g_print ("%s\n", values[0]);
|
||||
@@ -4202,20 +4206,9 @@ complete_property (const gchar *setting_name, const gchar *property, const gchar
|
||||
run_rl_generator (gen_connection_types, prefix);
|
||||
else if (strcmp (property, NM_SETTING_CONNECTION_MASTER) == 0)
|
||||
run_rl_generator (gen_func_master_ifnames, prefix);
|
||||
else if (strcmp (property, NM_SETTING_CONNECTION_INTERFACE_NAME) == 0)
|
||||
run_rl_generator (nmc_rl_gen_func_ifnames, prefix);
|
||||
} else if ( strcmp (setting_name, NM_SETTING_BLUETOOTH_SETTING_NAME) == 0
|
||||
&& strcmp (property, NM_SETTING_BLUETOOTH_TYPE) == 0)
|
||||
run_rl_generator (gen_func_bt_type, prefix);
|
||||
else if (strcmp (setting_name, NM_SETTING_IP_TUNNEL_SETTING_NAME) == 0) {
|
||||
if (strcmp (property, NM_SETTING_IP_TUNNEL_PARENT) == 0)
|
||||
run_rl_generator (nmc_rl_gen_func_ifnames, prefix);
|
||||
} else if (strcmp (setting_name, NM_SETTING_MACVLAN_SETTING_NAME) == 0) {
|
||||
if (strcmp (property, NM_SETTING_MACVLAN_PARENT) == 0)
|
||||
run_rl_generator (nmc_rl_gen_func_ifnames, prefix);
|
||||
} 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);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -4520,8 +4513,6 @@ nmcli_con_add_tab_completion (const char *text, int start, int end)
|
||||
next:
|
||||
if (g_str_has_prefix (rl_prompt, NM_META_TEXT_PROMPT_CON_TYPE))
|
||||
generator_func = gen_connection_types;
|
||||
else if (g_str_has_prefix (rl_prompt, NM_META_TEXT_PROMPT_IFNAME))
|
||||
generator_func = nmc_rl_gen_func_ifnames;
|
||||
else if (g_str_has_prefix (rl_prompt, NM_META_TEXT_PROMPT_MASTER))
|
||||
generator_func = gen_func_master_ifnames;
|
||||
else if (g_str_has_prefix (rl_prompt, NM_META_TEXT_PROMPT_BT_TYPE))
|
||||
|
@@ -456,10 +456,35 @@ _env_warn_fcn_handle (const NMMetaEnvironment *environment,
|
||||
g_print (_("Error: %s\n"), m);
|
||||
}
|
||||
|
||||
static NMDevice *const*
|
||||
_env_get_nm_devices (const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
guint *out_len)
|
||||
{
|
||||
NmCli *nmc = environment_user_data;
|
||||
const GPtrArray *devices;
|
||||
|
||||
nm_assert (nmc);
|
||||
|
||||
/* the returned list is *not* NULL terminated. Need to
|
||||
* provide and honor the out_len argument. */
|
||||
nm_assert (out_len);
|
||||
|
||||
devices = nm_client_get_devices (nmc->client);
|
||||
if (!devices) {
|
||||
*out_len = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*out_len = devices->len;
|
||||
return (NMDevice *const*) devices->pdata;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
const NMMetaEnvironment *const nmc_meta_environment = &((NMMetaEnvironment) {
|
||||
.warn_fcn = _env_warn_fcn_handle,
|
||||
.get_nm_devices = _env_get_nm_devices,
|
||||
});
|
||||
|
||||
NmCli *const nmc_meta_environment_arg = &nm_cli;
|
||||
|
@@ -261,6 +261,8 @@ nm_meta_abstract_info_get (const NMMetaAbstractInfo *abstract_info,
|
||||
|
||||
const char *const*
|
||||
nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abstract_info,
|
||||
const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
const char *text,
|
||||
char ***out_to_free)
|
||||
{
|
||||
@@ -277,6 +279,8 @@ nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abstract_info,
|
||||
return NULL;
|
||||
|
||||
values = abstract_info->meta_type->complete_fcn (abstract_info,
|
||||
environment,
|
||||
environment_user_data,
|
||||
text,
|
||||
out_to_free);
|
||||
|
||||
|
@@ -60,6 +60,8 @@ gconstpointer nm_meta_abstract_info_get (const NMMetaAbstractInfo *abstract_info
|
||||
gpointer *out_to_free);
|
||||
|
||||
const char *const*nm_meta_abstract_info_complete (const NMMetaAbstractInfo *abstract_info,
|
||||
const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
const char *text,
|
||||
char ***out_to_free);
|
||||
|
||||
|
@@ -524,7 +524,7 @@ _env_warn_fcn (const NMMetaEnvironment *environment,
|
||||
const NMMetaPropertyInfo *property_info, const NMMetaEnvironment *environment, gpointer environment_user_data, NMSetting *setting, const char *value, guint32 idx, GError **error
|
||||
|
||||
#define ARGS_COMPLETE_FCN \
|
||||
const NMMetaPropertyInfo *property_info, const char *text, char ***out_to_free
|
||||
const NMMetaPropertyInfo *property_info, const NMMetaEnvironment *environment, gpointer environment_user_data, const char *text, char ***out_to_free
|
||||
|
||||
#define ARGS_VALUES_FCN \
|
||||
const NMMetaPropertyInfo *property_info, char ***out_to_free
|
||||
@@ -1107,6 +1107,40 @@ _complete_fcn_gobject_bool (ARGS_COMPLETE_FCN)
|
||||
return v;
|
||||
}
|
||||
|
||||
static const char *const*
|
||||
_complete_fcn_gobject_devices (ARGS_COMPLETE_FCN)
|
||||
{
|
||||
NMDevice *const*devices = NULL;
|
||||
guint i, j;
|
||||
guint len = 0;
|
||||
char **ifnames;
|
||||
|
||||
if ( environment
|
||||
&& environment->get_nm_devices) {
|
||||
devices = environment->get_nm_devices (environment,
|
||||
environment_user_data,
|
||||
&len);
|
||||
}
|
||||
|
||||
if (len == 0)
|
||||
return NULL;
|
||||
|
||||
ifnames = g_new (char *, len + 1);
|
||||
for (i = 0, j = 0; i < len; i++) {
|
||||
const char *ifname;
|
||||
|
||||
nm_assert (NM_IS_DEVICE (devices[i]));
|
||||
|
||||
ifname = nm_device_get_iface (devices[i]);
|
||||
if (ifname)
|
||||
ifnames[j++] = g_strdup (ifname);
|
||||
}
|
||||
ifnames[j++] = NULL;
|
||||
|
||||
*out_to_free = ifnames;
|
||||
return (const char *const*) ifnames;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
static char *
|
||||
@@ -4772,6 +4806,12 @@ static const NMMetaPropertyType _pt_gobject_enum = {
|
||||
.values_fcn = _values_fcn_gobject_enum,
|
||||
};
|
||||
|
||||
static const NMMetaPropertyType _pt_gobject_devices = {
|
||||
.get_fcn = _get_fcn_gobject,
|
||||
.set_fcn = _set_fcn_gobject_string,
|
||||
.complete_fcn = _complete_fcn_gobject_devices,
|
||||
};
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
#include "settings-docs.c"
|
||||
@@ -5346,6 +5386,7 @@ static const NMMetaPropertyInfo property_infos_CONNECTION[] = {
|
||||
.property_type = DEFINE_PROPERTY_TYPE (
|
||||
.get_fcn = _get_fcn_gobject,
|
||||
.set_fcn = _set_fcn_gobject_ifname,
|
||||
.complete_fcn = _complete_fcn_gobject_devices,
|
||||
),
|
||||
},
|
||||
{
|
||||
@@ -6040,7 +6081,7 @@ static const NMMetaPropertyInfo property_infos_IP_TUNNEL[] = {
|
||||
.is_cli_option = TRUE,
|
||||
.property_alias = "dev",
|
||||
.prompt = N_("Parent device [none]"),
|
||||
.property_type = &_pt_gobject_string,
|
||||
.property_type = &_pt_gobject_devices,
|
||||
},
|
||||
{
|
||||
PROPERTY_INFO_WITH_DESC (NM_SETTING_IP_TUNNEL_LOCAL),
|
||||
@@ -6173,7 +6214,7 @@ static const NMMetaPropertyInfo property_infos_MACVLAN[] = {
|
||||
.property_alias = "dev",
|
||||
.inf_flags = NM_META_PROPERTY_INF_FLAG_REQD,
|
||||
.prompt = N_("MACVLAN parent device or connection UUID"),
|
||||
.property_type = &_pt_gobject_string,
|
||||
.property_type = &_pt_gobject_devices,
|
||||
},
|
||||
{
|
||||
PROPERTY_INFO_WITH_DESC (NM_SETTING_MACVLAN_MODE),
|
||||
@@ -6519,7 +6560,7 @@ static const NMMetaPropertyInfo property_infos_VLAN[] = {
|
||||
.property_alias = "dev",
|
||||
.inf_flags = NM_META_PROPERTY_INF_FLAG_REQD,
|
||||
.prompt = N_("VLAN parent device or connection UUID"),
|
||||
.property_type = &_pt_gobject_string,
|
||||
.property_type = &_pt_gobject_devices,
|
||||
},
|
||||
{
|
||||
PROPERTY_INFO_WITH_DESC (NM_SETTING_VLAN_ID),
|
||||
@@ -6620,7 +6661,7 @@ static const NMMetaPropertyInfo property_infos_VXLAN[] = {
|
||||
.is_cli_option = TRUE,
|
||||
.property_alias = "dev",
|
||||
.prompt = N_("Parent device [none]"),
|
||||
.property_type = &_pt_gobject_string,
|
||||
.property_type = &_pt_gobject_devices,
|
||||
},
|
||||
{
|
||||
PROPERTY_INFO_WITH_DESC (NM_SETTING_VXLAN_ID),
|
||||
@@ -7467,6 +7508,8 @@ _meta_type_property_info_get_nested (const NMMetaAbstractInfo *abstract_info,
|
||||
|
||||
static const char *const*
|
||||
_meta_type_property_info_complete_fcn (const NMMetaAbstractInfo *abstract_info,
|
||||
const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
const char *text,
|
||||
char ***out_to_free)
|
||||
{
|
||||
@@ -7476,6 +7519,8 @@ _meta_type_property_info_complete_fcn (const NMMetaAbstractInfo *abstract_info,
|
||||
|
||||
if (info->property_type->complete_fcn) {
|
||||
return info->property_type->complete_fcn (info,
|
||||
environment,
|
||||
environment_user_data,
|
||||
text,
|
||||
out_to_free);
|
||||
}
|
||||
|
@@ -22,6 +22,8 @@
|
||||
|
||||
#include "nm-meta-setting.h"
|
||||
|
||||
struct _NMDevice;
|
||||
|
||||
#define NM_META_TEXT_HIDDEN "<hidden>"
|
||||
|
||||
#define NM_META_TEXT_PROMPT_ADSL_PROTO N_("Protocol")
|
||||
@@ -197,6 +199,8 @@ struct _NMMetaPropertyType {
|
||||
char ***out_to_free);
|
||||
|
||||
const char *const*(*complete_fcn) (const NMMetaPropertyInfo *property_info,
|
||||
const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
const char *text,
|
||||
char ***out_to_free);
|
||||
};
|
||||
@@ -313,6 +317,8 @@ struct _NMMetaType {
|
||||
NMMetaAccessorGetOutFlags *out_flags,
|
||||
gpointer *out_to_free);
|
||||
const char *const*(*complete_fcn) (const NMMetaAbstractInfo *info,
|
||||
const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
const char *text,
|
||||
char ***out_to_free);
|
||||
};
|
||||
@@ -349,6 +355,10 @@ struct _NMMetaEnvironment {
|
||||
const char *fmt_l10n, /* the untranslated format string, but it is marked for translation using N_(). */
|
||||
va_list ap);
|
||||
|
||||
struct _NMDevice *const*(*get_nm_devices) (const NMMetaEnvironment *environment,
|
||||
gpointer environment_user_data,
|
||||
guint *out_len);
|
||||
|
||||
};
|
||||
|
||||
/*****************************************************************************/
|
||||
|
Reference in New Issue
Block a user