From ca156906859df4b89359b6f924a2a3ad44b9d9fe Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Tue, 28 May 2019 22:18:17 -0500 Subject: [PATCH] MM-15371: allow config show --json (#10918) * MM-15371: allow config show --json Mattermost Cloud will (temporarily?) rely on `mattermost config show` to dump the configuration for an active cluster installation. This adds support for a `--json` flag to dump the config in a format more suitable for programmatic consumption. * indent config show --json * ignore coverage lines too --- cmd/mattermost/commands/config.go | 25 ++++++++++++++++++++++--- cmd/mattermost/commands/config_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cmd/mattermost/commands/config.go b/cmd/mattermost/commands/config.go index be8fb56a0d..b9d7c0d1f6 100644 --- a/cmd/mattermost/commands/config.go +++ b/cmd/mattermost/commands/config.go @@ -82,6 +82,8 @@ func init() { MigrateConfigCmd.Flags().String("to", "", "Config to which to migrate") MigrateConfigCmd.MarkFlagRequired("to") + ConfigShowCmd.Flags().Bool("json", false, "Output the configuration as JSON.") + ConfigCmd.AddCommand( ValidateConfigCmd, ConfigSubpathCmd, @@ -161,9 +163,9 @@ func configGetCmdF(command *cobra.Command, args []string) error { } func configShowCmdF(command *cobra.Command, args []string) error { - configStore, err := getConfigStore(command) + useJSON, err := command.Flags().GetBool("json") if err != nil { - return err + return errors.Wrap(err, "failed reading json parameter") } err = cobra.NoArgs(command, args) @@ -171,7 +173,24 @@ func configShowCmdF(command *cobra.Command, args []string) error { return err } - fmt.Printf("%s", prettyPrintStruct(*configStore.Get())) + configStore, err := getConfigStore(command) + if err != nil { + return err + } + + config := *configStore.Get() + + if useJSON { + configJSON, err := json.MarshalIndent(config, "", " ") + if err != nil { + return errors.Wrap(err, "failed to marshal config as json") + } + + fmt.Printf("%s\n", configJSON) + } else { + fmt.Printf("%s", prettyPrintStruct(config)) + } + return nil } diff --git a/cmd/mattermost/commands/config_test.go b/cmd/mattermost/commands/config_test.go index ce04b85042..b76a4ebd0f 100644 --- a/cmd/mattermost/commands/config_test.go +++ b/cmd/mattermost/commands/config_test.go @@ -4,8 +4,8 @@ package commands import ( + "encoding/json" "fmt" - "github.com/mattermost/mattermost-server/config" "io/ioutil" "os" "reflect" @@ -13,6 +13,8 @@ import ( "strings" "testing" + "github.com/mattermost/mattermost-server/config" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -335,6 +337,27 @@ func TestConfigShow(t *testing.T) { assert.Contains(t, string(output), "MessageExportSettings") assert.Contains(t, string(output), "AnnouncementSettings") }) + + t.Run("successfully dumping config as json", func(t *testing.T) { + output, err := th.RunCommandWithOutput(t, "config", "show", "--json") + require.Nil(t, err) + + // Filter out the test headers + var filteredOutput []string + for _, line := range strings.Split(output, "\n") { + if strings.HasPrefix(line, "---") || strings.HasPrefix(line, "===") || strings.HasPrefix(line, "PASS") || strings.HasPrefix(line, "coverage:") { + continue + } + + filteredOutput = append(filteredOutput, line) + } + + output = strings.Join(filteredOutput, "") + + var config model.Config + err = json.Unmarshal([]byte(output), &config) + require.Nil(t, err) + }) } func TestSetConfig(t *testing.T) {