diff options
author | Mukul Rawat <mukulsrawat@gmail.com> | 2018-10-16 03:36:46 +0530 |
---|---|---|
committer | Jesse Hallam <jesse.hallam@gmail.com> | 2018-10-15 18:06:46 -0400 |
commit | 80153ef87379040ff1c9ac6f11a7063b743a3fb2 (patch) | |
tree | 104f3af889e1b2f583b9ba01359b59a9f7ecc403 /cmd/mattermost/commands/config.go | |
parent | 1cdf717446d79191701949a97557f801681bc278 (diff) | |
download | chat-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
Diffstat (limited to 'cmd/mattermost/commands/config.go')
-rw-r--r-- | cmd/mattermost/commands/config.go | 39 |
1 files changed, 37 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() { |