[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
Этот коммит содержится в:
Mukul Rawat
2018-10-16 03:36:46 +05:30
коммит произвёл Jesse Hallam
родитель 1cdf717446
Коммит 80153ef873
2 изменённых файлов: 52 добавлений и 2 удалений

Просмотреть файл

@@ -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() {

Просмотреть файл

@@ -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")
}