diff --git a/cmd/mattermost/commands/config.go b/cmd/mattermost/commands/config.go index 009bf00a4e..c04543fcda 100644 --- a/cmd/mattermost/commands/config.go +++ b/cmd/mattermost/commands/config.go @@ -17,6 +17,7 @@ import ( "github.com/mattermost/mattermost-server/config" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/utils" + "github.com/mattermost/viper" ) var ConfigCmd = &cobra.Command{ @@ -121,10 +122,7 @@ func getConfigStore(command *cobra.Command) (config.Store, error) { return nil, errors.Wrap(err, "failed to initialize i18n") } - configDSN, err := command.Flags().GetString("config") - if err != nil { - return nil, errors.Wrap(err, "failed to parse --config flag") - } + configDSN := viper.GetString("config") configStore, err := config.NewStore(configDSN, false) if err != nil { diff --git a/cmd/mattermost/commands/init.go b/cmd/mattermost/commands/init.go index 45c1fdcefb..7acea66796 100644 --- a/cmd/mattermost/commands/init.go +++ b/cmd/mattermost/commands/init.go @@ -7,14 +7,12 @@ import ( "github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/utils" + "github.com/mattermost/viper" "github.com/spf13/cobra" ) func InitDBCommandContextCobra(command *cobra.Command) (*app.App, error) { - config, err := command.Flags().GetString("config") - if err != nil { - return nil, err - } + config := viper.GetString("config") a, err := InitDBCommandContext(config) diff --git a/cmd/mattermost/commands/jobserver.go b/cmd/mattermost/commands/jobserver.go index 238fcaca35..7ff22b5d81 100644 --- a/cmd/mattermost/commands/jobserver.go +++ b/cmd/mattermost/commands/jobserver.go @@ -9,13 +9,14 @@ import ( "syscall" "github.com/mattermost/mattermost-server/mlog" + "github.com/mattermost/viper" "github.com/spf13/cobra" ) var JobserverCmd = &cobra.Command{ Use: "jobserver", Short: "Start the Mattermost job server", - Run: jobserverCmdF, + RunE: jobserverCmdF, } func init() { @@ -25,15 +26,17 @@ func init() { RootCmd.AddCommand(JobserverCmd) } -func jobserverCmdF(command *cobra.Command, args []string) { +func jobserverCmdF(command *cobra.Command, args []string) error { // Options noJobs, _ := command.Flags().GetBool("nojobs") noSchedule, _ := command.Flags().GetBool("noschedule") + config := viper.GetString("config") + // Initialize - a, err := InitDBCommandContext("config.json") + a, err := InitDBCommandContext(config) if err != nil { - panic(err.Error()) + return err } defer a.Shutdown() @@ -58,4 +61,6 @@ func jobserverCmdF(command *cobra.Command, args []string) { // Cleanup anything that isn't handled by a defer statement mlog.Info("Stopping Mattermost job server") + + return nil } diff --git a/cmd/mattermost/commands/root.go b/cmd/mattermost/commands/root.go index 725f394f65..1b5ef883be 100644 --- a/cmd/mattermost/commands/root.go +++ b/cmd/mattermost/commands/root.go @@ -4,6 +4,7 @@ package commands import ( + "github.com/mattermost/viper" "github.com/spf13/cobra" ) @@ -25,4 +26,8 @@ func init() { RootCmd.PersistentFlags().Bool("disableconfigwatch", false, "When set config.json will not be loaded from disk when the file is changed.") RootCmd.PersistentFlags().Bool("platform", false, "This flag signifies that the user tried to start the command from the platform binary, so we can log a mssage") RootCmd.PersistentFlags().MarkHidden("platform") + + viper.SetEnvPrefix("mm") + viper.BindEnv("config") + viper.BindPFlag("config", RootCmd.PersistentFlags().Lookup("config")) } diff --git a/cmd/mattermost/commands/server.go b/cmd/mattermost/commands/server.go index 2a3832e861..e80313dcb0 100644 --- a/cmd/mattermost/commands/server.go +++ b/cmd/mattermost/commands/server.go @@ -17,6 +17,7 @@ import ( "github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/web" "github.com/mattermost/mattermost-server/wsapi" + "github.com/mattermost/viper" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -34,17 +35,14 @@ func init() { } func serverCmdF(command *cobra.Command, args []string) error { - configDSN, err := command.Flags().GetString("config") - if err != nil { - return err - } + configDSN := viper.GetString("config") disableConfigWatch, _ := command.Flags().GetBool("disableconfigwatch") usedPlatform, _ := command.Flags().GetBool("platform") interruptChan := make(chan os.Signal, 1) - if err = utils.TranslationsPreInit(); err != nil { + if err := utils.TranslationsPreInit(); err != nil { return errors.Wrapf(err, "unable to load Mattermost translation files") } configStore, err := config.NewStore(configDSN, !disableConfigWatch)