summaryrefslogtreecommitdiffstats
path: root/cmd/platform/config.go
blob: f9b9daa85b3c16951c4c0c3211cf06c1f634776e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main

import (
	"encoding/json"
	"errors"
	"github.com/mattermost/platform/model"
	"github.com/mattermost/platform/utils"
	"github.com/spf13/cobra"
	"os"
)

var configCmd = &cobra.Command{
	Use:   "config",
	Short: "Configuration",
}

var validateConfigCmd = &cobra.Command{
	Use:   "validate",
	Short: "Validate config file",
	Run:   configValidateCmdF,
}

func init() {
	configCmd.AddCommand(
		validateConfigCmd,
	)
}

func configValidateCmdF(cmd *cobra.Command, args []string) {
	utils.TranslationsPreInit()
	filePath, err := cmd.Flags().GetString("config")
	if err != nil {
		CommandPrintErrorln(err)
		return
	}

	filePath = utils.FindConfigFile(filePath)

	file, err := os.Open(filePath)
	if err != nil {
		CommandPrintErrorln(err)
		return
	}

	decoder := json.NewDecoder(file)
	config := model.Config{}
	err = decoder.Decode(&config)
	if err != nil {
		CommandPrintErrorln(err)
		return
	}

	if _, err := file.Stat(); err != nil {
		CommandPrintErrorln(err)
		return
	}

	if err := config.IsValid(); err != nil {
		CommandPrintErrorln(errors.New(utils.T(err.Id)))
		return
	}

	CommandPrettyPrintln("The document is valid")
}