github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/cmd/platform/config.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 package main 4 5 import ( 6 "encoding/json" 7 "errors" 8 "os" 9 10 "github.com/mattermost/mattermost-server/model" 11 "github.com/mattermost/mattermost-server/utils" 12 "github.com/spf13/cobra" 13 ) 14 15 var configCmd = &cobra.Command{ 16 Use: "config", 17 Short: "Configuration", 18 } 19 20 var validateConfigCmd = &cobra.Command{ 21 Use: "validate", 22 Short: "Validate config file", 23 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.", 24 RunE: configValidateCmdF, 25 } 26 27 func init() { 28 configCmd.AddCommand( 29 validateConfigCmd, 30 ) 31 } 32 33 func configValidateCmdF(cmd *cobra.Command, args []string) error { 34 utils.TranslationsPreInit() 35 model.AppErrorInit(utils.T) 36 filePath, err := cmd.Flags().GetString("config") 37 if err != nil { 38 return err 39 } 40 41 filePath = utils.FindConfigFile(filePath) 42 43 file, err := os.Open(filePath) 44 if err != nil { 45 return err 46 } 47 48 decoder := json.NewDecoder(file) 49 config := model.Config{} 50 err = decoder.Decode(&config) 51 if err != nil { 52 return err 53 } 54 55 if _, err := file.Stat(); err != nil { 56 return err 57 } 58 59 if err := config.IsValid(); err != nil { 60 return errors.New(utils.T(err.Id)) 61 } 62 63 CommandPrettyPrintln("The document is valid") 64 return nil 65 }