github.com/spline-fu/mattermost-server@v4.10.10+incompatible/cmd/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/cmd"
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/utils"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var ConfigCmd = &cobra.Command{
    18  	Use:   "config",
    19  	Short: "Configuration",
    20  }
    21  
    22  var ValidateConfigCmd = &cobra.Command{
    23  	Use:   "validate",
    24  	Short: "Validate config file",
    25  	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.",
    26  	RunE:  configValidateCmdF,
    27  }
    28  
    29  func init() {
    30  	ConfigCmd.AddCommand(
    31  		ValidateConfigCmd,
    32  	)
    33  	cmd.RootCmd.AddCommand(ConfigCmd)
    34  }
    35  
    36  func configValidateCmdF(command *cobra.Command, args []string) error {
    37  	utils.TranslationsPreInit()
    38  	model.AppErrorInit(utils.T)
    39  	filePath, err := command.Flags().GetString("config")
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	filePath = utils.FindConfigFile(filePath)
    45  
    46  	file, err := os.Open(filePath)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	decoder := json.NewDecoder(file)
    52  	config := model.Config{}
    53  	err = decoder.Decode(&config)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	if _, err := file.Stat(); err != nil {
    59  		return err
    60  	}
    61  
    62  	if err := config.IsValid(); err != nil {
    63  		return errors.New(utils.T(err.Id))
    64  	}
    65  
    66  	cmd.CommandPrettyPrintln("The document is valid")
    67  	return nil
    68  }