summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMukul Rawat <mukulsrawat@gmail.com>2018-10-16 03:36:46 +0530
committerJesse Hallam <jesse.hallam@gmail.com>2018-10-15 18:06:46 -0400
commit80153ef87379040ff1c9ac6f11a7063b743a3fb2 (patch)
tree104f3af889e1b2f583b9ba01359b59a9f7ecc403
parent1cdf717446d79191701949a97557f801681bc278 (diff)
downloadchat-80153ef87379040ff1c9ac6f11a7063b743a3fb2.tar.gz
chat-80153ef87379040ff1c9ac6f11a7063b743a3fb2.tar.bz2
chat-80153ef87379040ff1c9ac6f11a7063b743a3fb2.zip
[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
-rw-r--r--cmd/mattermost/commands/config.go39
-rw-r--r--cmd/mattermost/commands/config_test.go15
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")
}