github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/cmd/commands/message_export.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package commands 5 6 import ( 7 "errors" 8 9 "context" 10 11 "time" 12 13 "github.com/mattermost/mattermost-server/cmd" 14 "github.com/mattermost/mattermost-server/model" 15 "github.com/spf13/cobra" 16 ) 17 18 var MessageExportCmd = &cobra.Command{ 19 Use: "export", 20 Short: "Export data from Mattermost", 21 Long: "Export data from Mattermost in a format suitable for import into a third-party application", 22 Example: "export --format=actiance --exportFrom=12345", 23 RunE: messageExportCmdF, 24 } 25 26 func init() { 27 MessageExportCmd.Flags().String("format", "actiance", "The format to export data in") 28 MessageExportCmd.Flags().Int64("exportFrom", -1, "The timestamp of the earliest post to export, expressed in seconds since the unix epoch.") 29 MessageExportCmd.Flags().Int("timeoutSeconds", -1, "The maximum number of seconds to wait for the job to complete before timing out.") 30 cmd.RootCmd.AddCommand(MessageExportCmd) 31 } 32 33 func messageExportCmdF(command *cobra.Command, args []string) error { 34 a, err := cmd.InitDBCommandContextCobra(command) 35 if err != nil { 36 return err 37 } 38 defer a.Shutdown() 39 40 if !*a.Config().MessageExportSettings.EnableExport { 41 return errors.New("ERROR: The message export feature is not enabled") 42 } 43 44 // for now, format is hard-coded to actiance. In time, we'll have to support other formats and inject them into job data 45 if format, err := command.Flags().GetString("format"); err != nil { 46 return errors.New("format flag error") 47 } else if format != "actiance" { 48 return errors.New("unsupported export format") 49 } 50 51 startTime, err := command.Flags().GetInt64("exportFrom") 52 if err != nil { 53 return errors.New("exportFrom flag error") 54 } else if startTime < 0 { 55 return errors.New("exportFrom must be a positive integer") 56 } 57 58 timeoutSeconds, err := command.Flags().GetInt("timeoutSeconds") 59 if err != nil { 60 return errors.New("timeoutSeconds error") 61 } else if timeoutSeconds < 0 { 62 return errors.New("timeoutSeconds must be a positive integer") 63 } 64 65 if messageExportI := a.MessageExport; messageExportI != nil { 66 ctx := context.Background() 67 if timeoutSeconds > 0 { 68 var cancel context.CancelFunc 69 ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeoutSeconds)) 70 defer cancel() 71 } 72 73 job, err := messageExportI.StartSynchronizeJob(ctx, startTime) 74 if err != nil || job.Status == model.JOB_STATUS_ERROR || job.Status == model.JOB_STATUS_CANCELED { 75 cmd.CommandPrintErrorln("ERROR: Message export job failed. Please check the server logs") 76 } else { 77 cmd.CommandPrettyPrintln("SUCCESS: Message export job complete") 78 } 79 } 80 81 return nil 82 }