From 80153ef87379040ff1c9ac6f11a7063b743a3fb2 Mon Sep 17 00:00:00 2001 From: Mukul Rawat Date: Tue, 16 Oct 2018 03:36:46 +0530 Subject: [MM-12362] Add CLI command 'config show' (#9536) (#9564) * Add the subcommand by creating a new Command instance. * Implemented the structure of the subcommand function. * Register our new command * Write some helper functions * finish the pretty print function * write some test for config show * Refactor and extract the tab printing functionality in its own function * Use app.Config() to create our config object & accept incoming changes * Removed reading the file, make helper functions return string and perform printing inside the command * Remove the previous code for checking presence of arguments and use 'cobra.NoArgs()' instead * Remove named return and instead declare the variable and then return it. * Remove printTab function and simplify printing out tabs using strings.Repeat * Add some functions to test the output * Update the usage and remove a comment * Update the print --- cmd/mattermost/commands/config.go | 39 ++++++++++++++++++++++++++++++++-- cmd/mattermost/commands/config_test.go | 15 +++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/cmd/mattermost/commands/config.go b/cmd/mattermost/commands/config.go index 47dd61458..c40debfd3 100644 --- a/cmd/mattermost/commands/config.go +++ b/cmd/mattermost/commands/config.go @@ -50,6 +50,14 @@ var ConfigGetCmd = &cobra.Command{ RunE: configGetCmdF, } +var ConfigShowCmd = &cobra.Command{ + Use: "show", + Short: "Writes the server configuration to STDOUT", + Long: "Pretty-prints the server configuration and writes to STDOUT", + Example: "config show", + RunE: configShowCmdF, +} + func init() { ConfigSubpathCmd.Flags().String("path", "", "Optional subpath; defaults to value in SiteURL") @@ -57,6 +65,7 @@ func init() { ValidateConfigCmd, ConfigSubpathCmd, ConfigGetCmd, + ConfigShowCmd, ) RootCmd.AddCommand(ConfigCmd) } @@ -140,6 +149,27 @@ func configGetCmdF(command *cobra.Command, args []string) error { return nil } +func configShowCmdF(command *cobra.Command, args []string) error { + app, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer app.Shutdown() + + // check that no arguments are given + err = cobra.NoArgs(command, args) + if err != nil { + return err + } + + // set up the config object + config := app.Config() + + // pretty print + fmt.Printf("%s", prettyPrint(configToMap(*config))) + return nil +} + // printConfigValues function prints out the value of the configSettings working recursively or // gives an error if config setting is not in the file. func printConfigValues(configMap map[string]interface{}, configSetting []string, name string) (string, error) { @@ -163,10 +193,15 @@ func printConfigValues(configMap map[string]interface{}, configSetting []string, } } -// printMap takes a reflect.Value and return a string, recursively if its a map with the given tab settings. +// prettyPrint the map +func prettyPrint(configMap map[string]interface{}) string { + value := reflect.ValueOf(configMap) + return printMap(value, 0) +} + +// printMap takes a reflect.Value and print it out, recursively if its a map with the given tab settings. func printMap(value reflect.Value, tabVal int) string { - // our output buffer out := &bytes.Buffer{} for _, key := range value.MapKeys() { diff --git a/cmd/mattermost/commands/config_test.go b/cmd/mattermost/commands/config_test.go index a68818201..02178c770 100644 --- a/cmd/mattermost/commands/config_test.go +++ b/cmd/mattermost/commands/config_test.go @@ -401,5 +401,20 @@ func TestPrintConfigValues(t *testing.T) { } }) } +} + +func TestConfigShow(t *testing.T) { + + // error + assert.Error(t, RunCommand(t, "config", "show", "abc")) + + // no error + assert.NoError(t, RunCommand(t, "config", "show")) + + // check the output + output := CheckCommand(t, "config", "show") + assert.Contains(t, string(output), "SqlSettings") + assert.Contains(t, string(output), "MessageExportSettings") + assert.Contains(t, string(output), "AnnouncementSettings") } -- cgit v1.2.3-1-g7c22