github.com/dschalla/mattermost-server@v4.8.1-rc1+incompatible/cmd/platform/import.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  package main
     4  
     5  import (
     6  	"errors"
     7  	"os"
     8  
     9  	"fmt"
    10  
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var importCmd = &cobra.Command{
    15  	Use:   "import",
    16  	Short: "Import data.",
    17  }
    18  
    19  var slackImportCmd = &cobra.Command{
    20  	Use:     "slack [team] [file]",
    21  	Short:   "Import a team from Slack.",
    22  	Long:    "Import a team from a Slack export zip file.",
    23  	Example: "  import slack myteam slack_export.zip",
    24  	RunE:    slackImportCmdF,
    25  }
    26  
    27  var bulkImportCmd = &cobra.Command{
    28  	Use:     "bulk [file]",
    29  	Short:   "Import bulk data.",
    30  	Long:    "Import data from a Mattermost Bulk Import File.",
    31  	Example: "  import bulk bulk_data.json",
    32  	RunE:    bulkImportCmdF,
    33  }
    34  
    35  func init() {
    36  	bulkImportCmd.Flags().Bool("apply", false, "Save the import data to the database. Use with caution - this cannot be reverted.")
    37  	bulkImportCmd.Flags().Bool("validate", false, "Validate the import data without making any changes to the system.")
    38  	bulkImportCmd.Flags().Int("workers", 2, "How many workers to run whilst doing the import.")
    39  
    40  	importCmd.AddCommand(
    41  		bulkImportCmd,
    42  		slackImportCmd,
    43  	)
    44  }
    45  
    46  func slackImportCmdF(cmd *cobra.Command, args []string) error {
    47  	a, err := initDBCommandContextCobra(cmd)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	if len(args) != 2 {
    53  		return errors.New("Incorrect number of arguments.")
    54  	}
    55  
    56  	team := getTeamFromTeamArg(a, args[0])
    57  	if team == nil {
    58  		return errors.New("Unable to find team '" + args[0] + "'")
    59  	}
    60  
    61  	fileReader, err := os.Open(args[1])
    62  	if err != nil {
    63  		return err
    64  	}
    65  	defer fileReader.Close()
    66  
    67  	fileInfo, err := fileReader.Stat()
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	CommandPrettyPrintln("Running Slack Import. This may take a long time for large teams or teams with many messages.")
    73  
    74  	a.SlackImport(fileReader, fileInfo.Size(), team.Id)
    75  
    76  	CommandPrettyPrintln("Finished Slack Import.")
    77  
    78  	return nil
    79  }
    80  
    81  func bulkImportCmdF(cmd *cobra.Command, args []string) error {
    82  	a, err := initDBCommandContextCobra(cmd)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	apply, err := cmd.Flags().GetBool("apply")
    88  	if err != nil {
    89  		return errors.New("Apply flag error")
    90  	}
    91  
    92  	validate, err := cmd.Flags().GetBool("validate")
    93  	if err != nil {
    94  		return errors.New("Validate flag error")
    95  	}
    96  
    97  	workers, err := cmd.Flags().GetInt("workers")
    98  	if err != nil {
    99  		return errors.New("Workers flag error")
   100  	}
   101  
   102  	if len(args) != 1 {
   103  		return errors.New("Incorrect number of arguments.")
   104  	}
   105  
   106  	fileReader, err := os.Open(args[0])
   107  	if err != nil {
   108  		return err
   109  	}
   110  	defer fileReader.Close()
   111  
   112  	if apply && validate {
   113  		CommandPrettyPrintln("Use only one of --apply or --validate.")
   114  		return nil
   115  	} else if apply && !validate {
   116  		CommandPrettyPrintln("Running Bulk Import. This may take a long time.")
   117  	} else {
   118  		CommandPrettyPrintln("Running Bulk Import Data Validation.")
   119  		CommandPrettyPrintln("** This checks the validity of the entities in the data file, but does not persist any changes **")
   120  		CommandPrettyPrintln("Use the --apply flag to perform the actual data import.")
   121  	}
   122  
   123  	CommandPrettyPrintln("")
   124  
   125  	if err, lineNumber := a.BulkImport(fileReader, !apply, workers); err != nil {
   126  		CommandPrettyPrintln(err.Error())
   127  		if lineNumber != 0 {
   128  			CommandPrettyPrintln(fmt.Sprintf("Error occurred on data file line %v", lineNumber))
   129  		}
   130  	} else {
   131  		if apply {
   132  			CommandPrettyPrintln("Finished Bulk Import.")
   133  		} else {
   134  			CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.")
   135  		}
   136  	}
   137  
   138  	return nil
   139  }