diff options
author | Thomas Haller <thaller@redhat.com> | 2017-04-06 15:14:23 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-04-12 11:24:04 +0200 |
commit | 16299e5ac0a8882e13d42122278013b74bc6d34d (patch) | |
tree | 9c59f3176fb69985deeb298d386b1cc8ea9a27d0 | |
parent | 3045daf1276df4e629f3478c3b0302370a926b5c (diff) | |
download | NetworkManager-16299e5ac0a8882e13d42122278013b74bc6d34d.tar.gz |
cli: use nmc_print() to output `nmcli general permissions`
Add also colors for the output values.
-rw-r--r-- | clients/cli/general.c | 219 | ||||
-rw-r--r-- | clients/cli/utils.c | 4 | ||||
-rw-r--r-- | clients/cli/utils.h | 6 |
3 files changed, 125 insertions, 104 deletions
diff --git a/clients/cli/general.c b/clients/cli/general.c index 82f878b692..d94512ea4e 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -106,6 +106,67 @@ connectivity_to_color (NMConnectivityState connectivity) } } +static const char * +permission_to_string (NMClientPermission perm) +{ + switch (perm) { + case NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK: + return NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK; + case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI: + return NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI; + case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN: + return NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN; + case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX: + return NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX; + case NM_CLIENT_PERMISSION_SLEEP_WAKE: + return NM_AUTH_PERMISSION_SLEEP_WAKE; + case NM_CLIENT_PERMISSION_NETWORK_CONTROL: + return NM_AUTH_PERMISSION_NETWORK_CONTROL; + case NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED: + return NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED; + case NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN: + return NM_AUTH_PERMISSION_WIFI_SHARE_OPEN; + case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM: + return NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM; + case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN: + return NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN; + case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME: + return NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME; + case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS: + return NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS; + case NM_CLIENT_PERMISSION_RELOAD: + return NM_AUTH_PERMISSION_RELOAD; + case NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK: + return NM_AUTH_PERMISSION_CHECKPOINT_ROLLBACK; + case NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS: + return NM_AUTH_PERMISSION_ENABLE_DISABLE_STATISTICS; + default: + return _("unknown"); + } +} + +NM_UTILS_LOOKUP_STR_DEFINE_STATIC (permission_result_to_string_no_l10n, NMClientPermissionResult, + NM_UTILS_LOOKUP_DEFAULT (N_("unknown")), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, N_("yes")), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, N_("no")), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, N_("auth")), + NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN), +); + +_NM_UTILS_LOOKUP_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaTermColor, + NM_UTILS_LOOKUP_DEFAULT (NM_META_TERM_COLOR_NORMAL), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, NM_META_TERM_COLOR_GREEN), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, NM_META_TERM_COLOR_RED), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_TERM_COLOR_YELLOW), + NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN), +); + +#define HANDLE_TERMFORMAT(color) \ + G_STMT_START { \ + if (get_type == NM_META_ACCESSOR_GET_TYPE_TERMFORMAT) \ + return nm_meta_termformat_pack ((color), NM_META_TERM_FORMAT_NORMAL); \ + } G_STMT_END + /*****************************************************************************/ static const NmcMetaGenericInfo *const metagen_general_status[]; @@ -125,12 +186,6 @@ _metagen_general_status_get_fcn (const NMMetaEnvironment *environment, NMState state; NMConnectivityState connectivity; -#define HANDLE_TERMFORMAT(color) \ - G_STMT_START { \ - if (get_type == NM_META_ACCESSOR_GET_TYPE_TERMFORMAT) \ - return nm_meta_termformat_pack ((color), NM_META_TERM_FORMAT_NORMAL); \ - } G_STMT_END - switch (info->info_type) { case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_RUNNING: HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); @@ -220,15 +275,48 @@ static const NmcMetaGenericInfo *const metagen_general_status[_NMC_GENERIC_INFO_ #define NMC_FIELDS_NM_WIMAX "WIMAX" #define NMC_FIELDS_NM_CONNECTIVITY "CONNECTIVITY" +/*****************************************************************************/ -/* Available fields for 'general permissions' */ -static const NmcMetaGenericInfo *const nmc_fields_nm_permissions[] = { - NMC_META_GENERIC ("PERMISSION"), /* 0 */ - NMC_META_GENERIC ("VALUE"), /* 1 */ - NULL, +static gconstpointer +_metagen_general_permissions_get_fcn (const NMMetaEnvironment *environment, + gpointer environment_user_data, + const NmcMetaGenericInfo *info, + gpointer target, + NMMetaAccessorGetType get_type, + NMMetaAccessorGetFlags get_flags, + gpointer *out_to_free) +{ + NMClientPermission perm = GPOINTER_TO_UINT (target); + NmCli *nmc = environment_user_data; + NMClientPermissionResult perm_result; + const char *s; + + switch (info->info_type) { + case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_PERMISSION: + HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + return permission_to_string (perm); + case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE: + perm_result = nm_client_get_permission_result (nmc->client, perm); + HANDLE_TERMFORMAT (permission_result_to_color (perm_result)); + s = permission_result_to_string_no_l10n (perm_result); + if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) + return _(s); + return s; + default: + break; + } + + g_return_val_if_reached (NULL); +} + +static const NmcMetaGenericInfo *const metagen_general_permissions[_NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_NUM + 1] = { +#define _METAGEN_GENERAL_PERMISSIONS(type, name) \ + [type] = NMC_META_GENERIC(name, .info_type = type, .get_fcn = _metagen_general_permissions_get_fcn) + _METAGEN_GENERAL_PERMISSIONS (NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_PERMISSION, "PERMISSION"), + _METAGEN_GENERAL_PERMISSIONS (NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE, "VALUE"), }; -#define NMC_FIELDS_NM_PERMISSIONS_ALL "PERMISSION,VALUE" -#define NMC_FIELDS_NM_PERMISSIONS_COMMON "PERMISSION,VALUE" + +/*****************************************************************************/ /* Available fields for 'general logging' */ static const NmcMetaGenericInfo *const nmc_fields_nm_logging[] = { @@ -429,60 +517,6 @@ do_general_status (NmCli *nmc, int argc, char **argv) return nmc->return_value; } -static const char * -permission_to_string (NMClientPermission perm) -{ - switch (perm) { - case NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK: - return NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK; - case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI: - return NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI; - case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN: - return NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN; - case NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX: - return NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX; - case NM_CLIENT_PERMISSION_SLEEP_WAKE: - return NM_AUTH_PERMISSION_SLEEP_WAKE; - case NM_CLIENT_PERMISSION_NETWORK_CONTROL: - return NM_AUTH_PERMISSION_NETWORK_CONTROL; - case NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED: - return NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED; - case NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN: - return NM_AUTH_PERMISSION_WIFI_SHARE_OPEN; - case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM: - return NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM; - case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN: - return NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN; - case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME: - return NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME; - case NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS: - return NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS; - case NM_CLIENT_PERMISSION_RELOAD: - return NM_AUTH_PERMISSION_RELOAD; - case NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK: - return NM_AUTH_PERMISSION_CHECKPOINT_ROLLBACK; - case NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS: - return NM_AUTH_PERMISSION_ENABLE_DISABLE_STATISTICS; - default: - return _("unknown"); - } -} - -static const char * -permission_result_to_string (NMClientPermissionResult perm_result) -{ - switch (perm_result) { - case NM_CLIENT_PERMISSION_RESULT_YES: - return _("yes"); - case NM_CLIENT_PERMISSION_RESULT_NO: - return _("no"); - case NM_CLIENT_PERMISSION_RESULT_AUTH: - return _("auth"); - default: - return _("unknown"); - } -} - static gboolean timeout_cb (gpointer user_data) { @@ -498,49 +532,30 @@ static int print_permissions (void *user_data) { NmCli *nmc = user_data; + gs_free_error GError *error = NULL; + const char *fields_str = NULL; NMClientPermission perm; - GError *error = NULL; - const char *fields_str; - const char *fields_all = NMC_FIELDS_NM_PERMISSIONS_ALL; - const char *fields_common = NMC_FIELDS_NM_PERMISSIONS_COMMON; - const NMMetaAbstractInfo *const*tmpl; - NmcOutputField *arr; - NMC_OUTPUT_DATA_DEFINE_SCOPED (out); + guint i; + gpointer permissions[NM_CLIENT_PERMISSION_LAST + 1]; - if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) - fields_str = fields_common; - else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0) - fields_str = fields_all; - else + if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) { + } else if (strcasecmp (nmc->required_fields, "all") == 0) { + } else fields_str = nmc->required_fields; - tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_nm_permissions; - out_indices = parse_output_fields (fields_str, tmpl, FALSE, NULL, &error); + for (i = 0, perm = NM_CLIENT_PERMISSION_NONE + 1; perm <= NM_CLIENT_PERMISSION_LAST; perm++) + permissions[i++] = GINT_TO_POINTER (perm); + permissions[i++] = NULL; - if (error) { + if (!nmc_print (&nmc->nmc_config, + permissions, + _("NetworkManager permissions"), + (const NMMetaAbstractInfo *const*) metagen_general_permissions, + fields_str, + &error)) { g_string_printf (nmc->return_text, _("Error: 'general permissions': %s"), error->message); - g_error_free (error); nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; - return FALSE; - } - - arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_MAIN_HEADER_ADD | NMC_OF_FLAG_FIELD_NAMES); - g_ptr_array_add (out.output_data, arr); - - - for (perm = NM_CLIENT_PERMISSION_NONE + 1; perm <= NM_CLIENT_PERMISSION_LAST; perm++) { - NMClientPermissionResult perm_result = nm_client_get_permission_result (nmc->client, perm); - - arr = nmc_dup_fields_array (tmpl, 0); - set_val_strc (arr, 0, permission_to_string (perm)); - set_val_strc (arr, 1, permission_result_to_string (perm_result)); - g_ptr_array_add (out.output_data, arr); } - print_data_prepare_width (out.output_data); - print_data (&nmc->nmc_config, - out_indices, - _("NetworkManager permissions"), - 0, &out); quit (); return G_SOURCE_REMOVE; diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 84b90c0a10..fab1cc0fb1 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -1419,7 +1419,7 @@ _print_fill (const NmcConfig *nmc_config, cell->header_cell = header_cell; cell->text = nm_meta_abstract_info_get (info, NULL, - NULL, + &nm_cli, target, text_get_type, text_get_flags, @@ -1428,7 +1428,7 @@ _print_fill (const NmcConfig *nmc_config, nm_meta_termformat_unpack (nm_meta_abstract_info_get (info, NULL, - NULL, + &nm_cli, target, NM_META_ACCESSOR_GET_TYPE_TERMFORMAT, NM_META_ACCESSOR_GET_FLAGS_NONE, diff --git a/clients/cli/utils.h b/clients/cli/utils.h index 1a4c796539..adc7bc4368 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -104,6 +104,7 @@ void print_data (const NmcConfig *nmc_config, /*****************************************************************************/ typedef enum { + NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_RUNNING = 0, NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_VERSION, NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STATE, @@ -117,6 +118,11 @@ typedef enum { NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_WIMAX_HW, NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_WIMAX, _NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_NUM, + + NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_PERMISSION = 0, + NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE, + _NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_NUM, + } NmcGenericInfoType; struct _NmcMetaGenericInfo { |