From 83c36a3c9fc39af9d359a20ead7e26812a8ea315 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Thu, 8 Jun 2023 18:32:19 +0200 Subject: [PATCH] refactor(emqx_conf_cli): do not print hidden roots in 'conf show' cmd --- apps/emqx_conf/src/emqx_conf_cli.erl | 46 ++++++++++++++++++++++------ changes/ce/feat-10985.en.md | 2 ++ 2 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 changes/ce/feat-10985.en.md diff --git a/apps/emqx_conf/src/emqx_conf_cli.erl b/apps/emqx_conf/src/emqx_conf_cli.erl index c0ca18100..530e4bfcb 100644 --- a/apps/emqx_conf/src/emqx_conf_cli.erl +++ b/apps/emqx_conf/src/emqx_conf_cli.erl @@ -22,6 +22,8 @@ unload/0 ]). +-include_lib("hocon/include/hoconsc.hrl"). + %% kept cluster_call for compatibility -define(CLUSTER_CALL, cluster_call). -define(CONF, conf). @@ -34,8 +36,8 @@ unload() -> emqx_ctl:unregister_command(?CLUSTER_CALL), emqx_ctl:unregister_command(?CONF). -conf(["show", "--keys-only"]) -> - print(emqx_config:get_root_names()); +conf(["show_keys" | _]) -> + print_keys(get_config()); conf(["show"]) -> print_hocon(get_config()); conf(["show", Key]) -> @@ -87,9 +89,10 @@ usage_conf() -> [ %% TODO add reload %{"conf reload", "reload etc/emqx.conf on local node"}, - {"conf show --keys-only", "Print all config keys"}, - {"conf show", "Print config in use"}, - {"conf show ", "Print configs under the given key"}, + {"conf show_keys", "Print all config keys"}, + {"conf show []", + "Print in-use configs (including default values) under the given key. " + "Print ALL keys if key is not provided"}, {"conf load ", "Load a HOCON format config file." "The config is overlay on top of the existing configs. " @@ -100,13 +103,13 @@ usage_conf() -> usage_sync() -> [ - {"conf cluster_sync tatus", "Show cluster config sync status summary"}, + {"conf cluster_sync status", "Show cluster config sync status summary"}, {"conf cluster_sync skip [node]", "Increase one commit on specific node"}, {"conf cluster_sync tnxid ", "Display detailed information of the config change transaction at TnxId"}, {"conf cluster_sync fast_forward [node] [tnx_id]", "Fast-forward config change transaction to tnx_id on the given node." - "WARNING: This results in inconsistent configs among the clustered nodes."} + "WARNING: This results in inconsistent configs among the clustered nodes."} ]. status() -> @@ -129,14 +132,39 @@ status() -> ), emqx_ctl:print("-----------------------------------------------\n"). +print_keys(Config) -> + print(lists:sort(maps:keys(Config))). + print(Json) -> emqx_ctl:print("~ts~n", [emqx_logger_jsonfmt:best_effort_json(Json)]). print_hocon(Hocon) -> emqx_ctl:print("~ts~n", [hocon_pp:do(Hocon, #{})]). -get_config() -> emqx_config:fill_defaults(emqx:get_raw_config([])). -get_config(Key) -> emqx_config:fill_defaults(#{Key => emqx:get_raw_config([Key])}). +get_config() -> + drop_hidden_roots(emqx_config:fill_defaults(emqx:get_raw_config([]))). + +drop_hidden_roots(Conf) -> + Hidden = hidden_roots(), + maps:without(Hidden, Conf). + +hidden_roots() -> + SchemaModule = emqx_conf:schema_module(), + Roots = hocon_schema:roots(SchemaModule), + lists:filtermap( + fun({BinName, {_RefName, Schema}}) -> + case hocon_schema:field_schema(Schema, importance) =/= ?IMPORTANCE_HIDDEN of + true -> + false; + false -> + {true, BinName} + end + end, + Roots + ). + +get_config(Key) -> + emqx_config:fill_defaults(#{Key => emqx:get_raw_config([Key])}). -define(OPTIONS, #{rawconf_with_defaults => true, override_to => cluster}). load_config(Path) -> diff --git a/changes/ce/feat-10985.en.md b/changes/ce/feat-10985.en.md new file mode 100644 index 000000000..89c0838a9 --- /dev/null +++ b/changes/ce/feat-10985.en.md @@ -0,0 +1,2 @@ +Renamed emqx ctl command 'cluster_call' to 'conf cluster_sync'. +The old command 'cluster_call' is still a valid command, but not included in usage info.