github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/cmd/mattermost/commands/import.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/audit"
    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  	BulkImportCmd.Flags().String("import-path", "", "A path to the data directory to import files from.")
    42  
    43  	ImportCmd.AddCommand(
    44  		BulkImportCmd,
    45  		SlackImportCmd,
    46  	)
    47  	RootCmd.AddCommand(ImportCmd)
    48  }
    49  
    50  func slackImportCmdF(command *cobra.Command, args []string) error {
    51  	a, err := InitDBCommandContextCobra(command)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	defer a.Srv().Shutdown()
    56  
    57  	if len(args) != 2 {
    58  		return errors.New("Incorrect number of arguments.")
    59  	}
    60  
    61  	team := getTeamFromTeamArg(a, args[0])
    62  	if team == nil {
    63  		return errors.New("Unable to find team '" + args[0] + "'")
    64  	}
    65  
    66  	fileReader, err := os.Open(args[1])
    67  	if err != nil {
    68  		return err
    69  	}
    70  	defer fileReader.Close()
    71  
    72  	fileInfo, err := fileReader.Stat()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	CommandPrettyPrintln("Running Slack Import. This may take a long time for large teams or teams with many messages.")
    78  
    79  	importErr, log := a.SlackImport(fileReader, fileInfo.Size(), team.Id)
    80  
    81  	if importErr != nil {
    82  		return err
    83  	}
    84  
    85  	CommandPrettyPrintln("")
    86  	CommandPrintln(log.String())
    87  	CommandPrettyPrintln("")
    88  
    89  	CommandPrettyPrintln("Finished Slack Import.")
    90  	CommandPrettyPrintln("")
    91  
    92  	auditRec := a.MakeAuditRecord("slackImport", audit.Success)
    93  	auditRec.AddMeta("team", team)
    94  	auditRec.AddMeta("file", args[1])
    95  	a.LogAuditRec(auditRec, nil)
    96  
    97  	return nil
    98  }
    99  
   100  func bulkImportCmdF(command *cobra.Command, args []string) error {
   101  	a, err := InitDBCommandContextCobra(command)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	defer a.Srv().Shutdown()
   106  
   107  	apply, err := command.Flags().GetBool("apply")
   108  	if err != nil {
   109  		return errors.New("Apply flag error")
   110  	}
   111  
   112  	validate, err := command.Flags().GetBool("validate")
   113  	if err != nil {
   114  		return errors.New("Validate flag error")
   115  	}
   116  
   117  	workers, err := command.Flags().GetInt("workers")
   118  	if err != nil {
   119  		return errors.New("Workers flag error")
   120  	}
   121  
   122  	importPath, err := command.Flags().GetString("import-path")
   123  	if err != nil {
   124  		return errors.New("import-path flag error")
   125  	}
   126  
   127  	if len(args) != 1 {
   128  		return errors.New("Incorrect number of arguments.")
   129  	}
   130  
   131  	fileReader, err := os.Open(args[0])
   132  	if err != nil {
   133  		return err
   134  	}
   135  	defer fileReader.Close()
   136  
   137  	if apply && validate {
   138  		CommandPrettyPrintln("Use only one of --apply or --validate.")
   139  		return nil
   140  	}
   141  
   142  	if apply && !validate {
   143  		CommandPrettyPrintln("Running Bulk Import. This may take a long time.")
   144  	} else {
   145  		CommandPrettyPrintln("Running Bulk Import Data Validation.")
   146  		CommandPrettyPrintln("** This checks the validity of the entities in the data file, but does not persist any changes **")
   147  		CommandPrettyPrintln("Use the --apply flag to perform the actual data import.")
   148  	}
   149  
   150  	CommandPrettyPrintln("")
   151  
   152  	if err, lineNumber := a.BulkImportWithPath(fileReader, !apply, workers, importPath); err != nil {
   153  		CommandPrintErrorln(err.Error())
   154  		if lineNumber != 0 {
   155  			CommandPrintErrorln(fmt.Sprintf("Error occurred on data file line %v", lineNumber))
   156  		}
   157  		return err
   158  	}
   159  
   160  	if apply {
   161  		CommandPrettyPrintln("Finished Bulk Import.")
   162  		auditRec := a.MakeAuditRecord("bulkImport", audit.Success)
   163  		auditRec.AddMeta("file", args[0])
   164  		a.LogAuditRec(auditRec, nil)
   165  	} else {
   166  		CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.")
   167  	}
   168  
   169  	return nil
   170  }