github.com/demisto/mattermost-server@v4.9.0-rc3+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  
    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  			cmd.CommandPrintErrorln("ERROR: Message export job failed. Please check the server logs")
    75  		} else {
    76  			cmd.CommandPrettyPrintln("SUCCESS: Message export job complete")
    77  		}
    78  	}
    79  
    80  	return nil
    81  }