From bfb2640451c95b242ae7b8bb8528538b706412ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 24 Jul 2018 16:11:47 +0200 Subject: Add idiomatic error handling in mattermost commands (#9147) --- cmd/mattermost/commands/config.go | 8 ++++++-- cmd/mattermost/commands/import.go | 14 ++++++++------ cmd/mattermost/commands/message_export.go | 15 ++++++++++----- 3 files changed, 24 insertions(+), 13 deletions(-) (limited to 'cmd') diff --git a/cmd/mattermost/commands/config.go b/cmd/mattermost/commands/config.go index d9881b050..b8630b82f 100644 --- a/cmd/mattermost/commands/config.go +++ b/cmd/mattermost/commands/config.go @@ -90,9 +90,13 @@ func configSubpathCmdF(command *cobra.Command, args []string) error { path, err := command.Flags().GetString("path") if err != nil { return errors.Wrap(err, "failed reading path") - } else if path == "" { + } + + if path == "" { return utils.UpdateAssetsSubpathFromConfig(a.Config()) - } else if err := utils.UpdateAssetsSubpath(path); err != nil { + } + + if err := utils.UpdateAssetsSubpath(path); err != nil { return errors.Wrap(err, "failed to update assets subpath") } diff --git a/cmd/mattermost/commands/import.go b/cmd/mattermost/commands/import.go index 8526ba6f8..d376fd61e 100644 --- a/cmd/mattermost/commands/import.go +++ b/cmd/mattermost/commands/import.go @@ -125,7 +125,9 @@ func bulkImportCmdF(command *cobra.Command, args []string) error { if apply && validate { CommandPrettyPrintln("Use only one of --apply or --validate.") return nil - } else if apply && !validate { + } + + if apply && !validate { CommandPrettyPrintln("Running Bulk Import. This may take a long time.") } else { CommandPrettyPrintln("Running Bulk Import Data Validation.") @@ -141,12 +143,12 @@ func bulkImportCmdF(command *cobra.Command, args []string) error { CommandPrettyPrintln(fmt.Sprintf("Error occurred on data file line %v", lineNumber)) } return err + } + + if apply { + CommandPrettyPrintln("Finished Bulk Import.") } else { - if apply { - CommandPrettyPrintln("Finished Bulk Import.") - } else { - CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.") - } + CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.") } return nil diff --git a/cmd/mattermost/commands/message_export.go b/cmd/mattermost/commands/message_export.go index ae97eacfb..e7d82a8c1 100644 --- a/cmd/mattermost/commands/message_export.go +++ b/cmd/mattermost/commands/message_export.go @@ -78,23 +78,27 @@ func scheduleExportCmdF(command *cobra.Command, args []string) error { } // for now, format is hard-coded to actiance. In time, we'll have to support other formats and inject them into job data - if format, err := command.Flags().GetString("format"); err != nil { + format, err := command.Flags().GetString("format") + if err != nil { return errors.New("format flag error") - } else if format != "actiance" { + } + if format != "actiance" { return errors.New("unsupported export format") } startTime, err := command.Flags().GetInt64("exportFrom") if err != nil { return errors.New("exportFrom flag error") - } else if startTime < 0 { + } + if startTime < 0 { return errors.New("exportFrom must be a positive integer") } timeoutSeconds, err := command.Flags().GetInt("timeoutSeconds") if err != nil { return errors.New("timeoutSeconds error") - } else if timeoutSeconds < 0 { + } + if timeoutSeconds < 0 { return errors.New("timeoutSeconds must be a positive integer") } @@ -128,7 +132,8 @@ func buildExportCmdF(format string) func(command *cobra.Command, args []string) startTime, err := command.Flags().GetInt64("exportFrom") if err != nil { return errors.New("exportFrom flag error") - } else if startTime < 0 { + } + if startTime < 0 { return errors.New("exportFrom must be a positive integer") } -- cgit v1.2.3-1-g7c22