github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/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/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  	RootCmd.AddCommand(MessageExportCmd)
    30  }
    31  
    32  func messageExportCmdF(command *cobra.Command, args []string) error {
    33  	a, err := InitDBCommandContextCobra(command)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	defer a.Shutdown()
    38  
    39  	if !*a.Config().MessageExportSettings.EnableExport {
    40  		return errors.New("ERROR: The message export feature is not enabled")
    41  	}
    42  
    43  	// for now, format is hard-coded to actiance. In time, we'll have to support other formats and inject them into job data
    44  	if format, err := command.Flags().GetString("format"); err != nil {
    45  		return errors.New("format flag error")
    46  	} else if format != "actiance" {
    47  		return errors.New("unsupported export format")
    48  	}
    49  
    50  	startTime, err := command.Flags().GetInt64("exportFrom")
    51  	if err != nil {
    52  		return errors.New("exportFrom flag error")
    53  	} else if startTime < 0 {
    54  		return errors.New("exportFrom must be a positive integer")
    55  	}
    56  
    57  	timeoutSeconds, err := command.Flags().GetInt("timeoutSeconds")
    58  	if err != nil {
    59  		return errors.New("timeoutSeconds error")
    60  	} else if timeoutSeconds < 0 {
    61  		return errors.New("timeoutSeconds must be a positive integer")
    62  	}
    63  
    64  	if messageExportI := a.MessageExport; messageExportI != nil {
    65  		ctx := context.Background()
    66  		if timeoutSeconds > 0 {
    67  			var cancel context.CancelFunc
    68  			ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeoutSeconds))
    69  			defer cancel()
    70  		}
    71  
    72  		job, err := messageExportI.StartSynchronizeJob(ctx, startTime)
    73  		if err != nil || job.Status == model.JOB_STATUS_ERROR || job.Status == model.JOB_STATUS_CANCELED {
    74  			CommandPrintErrorln("ERROR: Message export job failed. Please check the server logs")
    75  		} else {
    76  			CommandPrettyPrintln("SUCCESS: Message export job complete")
    77  		}
    78  	}
    79  
    80  	return nil
    81  }