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