github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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  	"os"
     9  
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  	"github.com/mattermost/mattermost-server/utils"
    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  var ConfigSubpathCmd = &cobra.Command{
    30  	Use:   "subpath",
    31  	Short: "Update client asset loading to use the configured subpath",
    32  	Long:  "Update the hard-coded production client asset paths to take into account Mattermost running on a subpath.",
    33  	Example: `  config subpath
    34    config subpath --path /mattermost
    35    config subpath --path /`,
    36  	RunE: configSubpathCmdF,
    37  }
    38  
    39  func init() {
    40  	ConfigSubpathCmd.Flags().String("path", "", "Optional subpath; defaults to value in SiteURL")
    41  
    42  	ConfigCmd.AddCommand(
    43  		ValidateConfigCmd,
    44  		ConfigSubpathCmd,
    45  	)
    46  	RootCmd.AddCommand(ConfigCmd)
    47  }
    48  
    49  func configValidateCmdF(command *cobra.Command, args []string) error {
    50  	utils.TranslationsPreInit()
    51  	model.AppErrorInit(utils.T)
    52  	filePath, err := command.Flags().GetString("config")
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	filePath = utils.FindConfigFile(filePath)
    58  
    59  	file, err := os.Open(filePath)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	decoder := json.NewDecoder(file)
    65  	config := model.Config{}
    66  	err = decoder.Decode(&config)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if _, err := file.Stat(); err != nil {
    72  		return err
    73  	}
    74  
    75  	if err := config.IsValid(); err != nil {
    76  		return errors.New(utils.T(err.Id))
    77  	}
    78  
    79  	CommandPrettyPrintln("The document is valid")
    80  	return nil
    81  }
    82  
    83  func configSubpathCmdF(command *cobra.Command, args []string) error {
    84  	a, err := InitDBCommandContextCobra(command)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	defer a.Shutdown()
    89  
    90  	path, err := command.Flags().GetString("path")
    91  	if err != nil {
    92  		return errors.Wrap(err, "failed reading path")
    93  	}
    94  
    95  	if path == "" {
    96  		return utils.UpdateAssetsSubpathFromConfig(a.Config())
    97  	}
    98  
    99  	if err := utils.UpdateAssetsSubpath(path); err != nil {
   100  		return errors.Wrap(err, "failed to update assets subpath")
   101  	}
   102  
   103  	return nil
   104  }