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