github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/config.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"os"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/utils"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var ConfigCmd = &cobra.Command{
    17  	Use:   "config",
    18  	Short: "Configuration",
    19  }
    20  
    21  var ValidateConfigCmd = &cobra.Command{
    22  	Use:   "validate",
    23  	Short: "Validate config file",
    24  	Long:  "If the config file is valid, this command will output a success message and have a zero exit code. If it is invalid, this command will output an error and have a non-zero exit code.",
    25  	RunE:  configValidateCmdF,
    26  }
    27  
    28  func init() {
    29  	ConfigCmd.AddCommand(
    30  		ValidateConfigCmd,
    31  	)
    32  	RootCmd.AddCommand(ConfigCmd)
    33  }
    34  
    35  func configValidateCmdF(command *cobra.Command, args []string) error {
    36  	utils.TranslationsPreInit()
    37  	model.AppErrorInit(utils.T)
    38  	filePath, err := command.Flags().GetString("config")
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	filePath = utils.FindConfigFile(filePath)
    44  
    45  	file, err := os.Open(filePath)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	decoder := json.NewDecoder(file)
    51  	config := model.Config{}
    52  	err = decoder.Decode(&config)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	if _, err := file.Stat(); err != nil {
    58  		return err
    59  	}
    60  
    61  	if err := config.IsValid(); err != nil {
    62  		return errors.New(utils.T(err.Id))
    63  	}
    64  
    65  	CommandPrettyPrintln("The document is valid")
    66  	return nil
    67  }