From 5c1049054eace710abd3418bbad141fbb7dd5d7f Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 23 May 2017 11:06:25 -0400 Subject: PLT-6471 Properly panic when translations can't be loaded (#6414) * PLT-6471 Properly panic when translations can't be loaded * Print usage messages when errors occur during CLI initialization * Reverted behaviour of FindDir and added second return value to it * Fixed merge conflict --- cmd/platform/channel.go | 28 ++++++++++++++++++------ cmd/platform/import.go | 8 +++++-- cmd/platform/init.go | 14 ++++++++---- cmd/platform/license.go | 4 +++- cmd/platform/mattermost.go | 4 +++- cmd/platform/roles.go | 10 +++++++-- cmd/platform/server.go | 10 ++++++--- cmd/platform/team.go | 16 ++++++++++---- cmd/platform/test.go | 10 +++++++-- cmd/platform/user.go | 53 ++++++++++++++++++++++++++++++++++++---------- cmd/platform/version.go | 11 +++++++--- 11 files changed, 128 insertions(+), 40 deletions(-) (limited to 'cmd') diff --git a/cmd/platform/channel.go b/cmd/platform/channel.go index 218cef1d5..53daf0f9a 100644 --- a/cmd/platform/channel.go +++ b/cmd/platform/channel.go @@ -102,7 +102,9 @@ func init() { } func createChannelCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if !utils.IsLicensed { return errors.New(utils.T("cli.license.critical")) @@ -152,7 +154,9 @@ func createChannelCmdF(cmd *cobra.Command, args []string) error { } func removeChannelUsersCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if !utils.IsLicensed { return errors.New(utils.T("cli.license.critical")) @@ -186,7 +190,9 @@ func removeUserFromChannel(channel *model.Channel, user *model.User, userArg str } func addChannelUsersCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if !utils.IsLicensed { return errors.New(utils.T("cli.license.critical")) @@ -220,7 +226,9 @@ func addUserToChannel(channel *model.Channel, user *model.User, userArg string) } func archiveChannelsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 1 { return errors.New("Enter at least one channel to delete.") @@ -241,7 +249,9 @@ func archiveChannelsCmdF(cmd *cobra.Command, args []string) error { } func deleteChannelsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 1 { return errors.New("Enter at least one channel to delete.") @@ -278,7 +288,9 @@ func deleteChannel(channel *model.Channel) *model.AppError { } func listChannelsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if !utils.IsLicensed { return errors.New(utils.T("cli.license.critical")) @@ -313,7 +325,9 @@ func listChannelsCmdF(cmd *cobra.Command, args []string) error { } func restoreChannelsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if !utils.IsLicensed { return errors.New(utils.T("cli.license.critical")) diff --git a/cmd/platform/import.go b/cmd/platform/import.go index ea3e42ad2..bf027340a 100644 --- a/cmd/platform/import.go +++ b/cmd/platform/import.go @@ -44,7 +44,9 @@ func init() { } func slackImportCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) != 2 { return errors.New("Incorrect number of arguments.") @@ -76,7 +78,9 @@ func slackImportCmdF(cmd *cobra.Command, args []string) error { } func bulkImportCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } apply, err := cmd.Flags().GetBool("apply") if err != nil { diff --git a/cmd/platform/init.go b/cmd/platform/init.go index b650cf2fd..5f915b9ab 100644 --- a/cmd/platform/init.go +++ b/cmd/platform/init.go @@ -12,14 +12,18 @@ func initDBCommandContextCobra(cmd *cobra.Command) error { if err != nil { return err } - initDBCommandContext(config) + + if err := initDBCommandContext(config); err != nil { + // Returning an error just prints the usage message, so actually panic + panic(err) + } return nil } -func initDBCommandContext(configFileLocation string) { - if errstr := utils.InitAndLoadConfig(configFileLocation); errstr != "" { - return +func initDBCommandContext(configFileLocation string) error { + if err := utils.InitAndLoadConfig(configFileLocation); err != nil { + return err } utils.ConfigureCmdLineLog() @@ -29,4 +33,6 @@ func initDBCommandContext(configFileLocation string) { if model.BuildEnterpriseReady == "true" { app.LoadLicense() } + + return nil } diff --git a/cmd/platform/license.go b/cmd/platform/license.go index 91dc3bfba..dcb37092f 100644 --- a/cmd/platform/license.go +++ b/cmd/platform/license.go @@ -28,7 +28,9 @@ func init() { } func uploadLicenseCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) != 1 { return errors.New("Enter one license file to upload") diff --git a/cmd/platform/mattermost.go b/cmd/platform/mattermost.go index 1646faf85..64e7974bf 100644 --- a/cmd/platform/mattermost.go +++ b/cmd/platform/mattermost.go @@ -59,7 +59,9 @@ var resetCmd = &cobra.Command{ } func resetCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } confirmFlag, _ := cmd.Flags().GetBool("confirm") if !confirmFlag { diff --git a/cmd/platform/roles.go b/cmd/platform/roles.go index df4b49436..97d6edf17 100644 --- a/cmd/platform/roles.go +++ b/cmd/platform/roles.go @@ -38,7 +38,10 @@ func init() { } func makeSystemAdminCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) < 1 { return errors.New("Enter at least one user.") } @@ -58,7 +61,10 @@ func makeSystemAdminCmdF(cmd *cobra.Command, args []string) error { } func makeMemberCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) < 1 { return errors.New("Enter at least one user.") } diff --git a/cmd/platform/server.go b/cmd/platform/server.go index 9846f8de9..ba7ace062 100644 --- a/cmd/platform/server.go +++ b/cmd/platform/server.go @@ -44,12 +44,16 @@ func runServerCmd(cmd *cobra.Command, args []string) error { } func runServer(configFileLocation string) { - if errstr := utils.InitAndLoadConfig(configFileLocation); errstr != "" { - l4g.Exit("Unable to load mattermost configuration file: ", errstr) + if err := utils.InitAndLoadConfig(configFileLocation); err != nil { + l4g.Exit("Unable to load Mattermost configuration file: ", err) + return + } + + if err := utils.InitTranslations(utils.Cfg.LocalizationSettings); err != nil { + l4g.Exit("Unable to load Mattermost translation files: %v", err) return } - utils.InitTranslations(utils.Cfg.LocalizationSettings) utils.TestConnection(utils.Cfg) pwd, _ := os.Getwd() diff --git a/cmd/platform/team.go b/cmd/platform/team.go index 71bcd543c..4e6a592a7 100644 --- a/cmd/platform/team.go +++ b/cmd/platform/team.go @@ -67,7 +67,9 @@ func init() { } func createTeamCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } name, errn := cmd.Flags().GetString("name") if errn != nil || name == "" { @@ -100,7 +102,9 @@ func createTeamCmdF(cmd *cobra.Command, args []string) error { } func removeUsersCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 2 { return errors.New("Not enough arguments.") @@ -130,7 +134,9 @@ func removeUserFromTeam(team *model.Team, user *model.User, userArg string) { } func addUsersCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 2 { return errors.New("Not enough arguments.") @@ -160,7 +166,9 @@ func addUserToTeam(team *model.Team, user *model.User, userArg string) { } func deleteTeamsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 1 { return errors.New("Not enough arguments.") diff --git a/cmd/platform/test.go b/cmd/platform/test.go index 735261439..efc89a2b2 100644 --- a/cmd/platform/test.go +++ b/cmd/platform/test.go @@ -45,7 +45,10 @@ func init() { } func webClientTestsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + utils.InitTranslations(utils.Cfg.LocalizationSettings) api.InitRouter() wsapi.InitRouter() @@ -61,7 +64,10 @@ func webClientTestsCmdF(cmd *cobra.Command, args []string) error { } func serverForWebClientTestsCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + utils.InitTranslations(utils.Cfg.LocalizationSettings) api.InitRouter() wsapi.InitRouter() diff --git a/cmd/platform/user.go b/cmd/platform/user.go index dc0aa0f71..74e71ebe3 100644 --- a/cmd/platform/user.go +++ b/cmd/platform/user.go @@ -157,7 +157,9 @@ func init() { } func userActivateCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 1 { return errors.New("Enter user(s) to activate.") @@ -193,7 +195,9 @@ func changeUserActiveStatus(user *model.User, userArg string, activate bool) err } func userDeactivateCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } if len(args) < 1 { return errors.New("Enter user(s) to deactivate.") @@ -204,7 +208,10 @@ func userDeactivateCmdF(cmd *cobra.Command, args []string) error { } func userCreateCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + username, erru := cmd.Flags().GetString("username") if erru != nil || username == "" { return errors.New("Username is required") @@ -248,7 +255,10 @@ func userCreateCmdF(cmd *cobra.Command, args []string) error { } func userInviteCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + utils.InitHTML() if len(args) < 2 { @@ -285,7 +295,10 @@ func inviteUser(email string, team *model.Team, teamArg string) error { } func resetUserPasswordCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) != 2 { return errors.New("Incorect number of arguments.") } @@ -304,7 +317,10 @@ func resetUserPasswordCmdF(cmd *cobra.Command, args []string) error { } func resetUserMfaCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) < 1 { return errors.New("Enter at least one user.") } @@ -325,7 +341,10 @@ func resetUserMfaCmdF(cmd *cobra.Command, args []string) error { } func deleteUserCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) < 1 { return errors.New("Enter at least one user.") } @@ -362,7 +381,10 @@ func deleteUserCmdF(cmd *cobra.Command, args []string) error { } func deleteAllUsersCommandF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) > 0 { return errors.New("Don't enter any agruments.") } @@ -393,7 +415,10 @@ func deleteAllUsersCommandF(cmd *cobra.Command, args []string) error { } func migrateAuthCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) != 3 { return errors.New("Enter the correct number of arguments.") } @@ -431,7 +456,10 @@ func migrateAuthCmdF(cmd *cobra.Command, args []string) error { } func verifyUserCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) < 1 { return errors.New("Enter at least one user.") } @@ -452,7 +480,10 @@ func verifyUserCmdF(cmd *cobra.Command, args []string) error { } func searchUserCmdF(cmd *cobra.Command, args []string) error { - initDBCommandContextCobra(cmd) + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + if len(args) < 1 { return errors.New("Enter at least one query.") } diff --git a/cmd/platform/version.go b/cmd/platform/version.go index 13ebdc1cd..9c0c48471 100644 --- a/cmd/platform/version.go +++ b/cmd/platform/version.go @@ -12,12 +12,17 @@ import ( var versionCmd = &cobra.Command{ Use: "version", Short: "Display version information", - Run: versionCmdF, + RunE: versionCmdF, } -func versionCmdF(cmd *cobra.Command, args []string) { - initDBCommandContextCobra(cmd) +func versionCmdF(cmd *cobra.Command, args []string) error { + if err := initDBCommandContextCobra(cmd); err != nil { + return err + } + printVersion() + + return nil } func printVersion() { -- cgit v1.2.3-1-g7c22