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
Этот коммит содержится в:
Jesse Hallam
2019-05-28 22:18:17 -05:00
коммит произвёл GitHub
родитель e86adce31e
Коммит ca15690685
2 изменённых файлов: 46 добавлений и 4 удалений

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

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

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

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