github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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 "os" 9 10 "fmt" 11 12 "github.com/mattermost/mattermost-server/v5/audit" 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 RootCmd.AddCommand(ImportCmd) 47 } 48 49 func slackImportCmdF(command *cobra.Command, args []string) error { 50 a, err := InitDBCommandContextCobra(command) 51 if err != nil { 52 return err 53 } 54 defer a.Srv().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 CommandPrettyPrintln("Running Slack Import. This may take a long time for large teams or teams with many messages.") 77 78 importErr, log := a.SlackImport(fileReader, fileInfo.Size(), team.Id) 79 80 if importErr != nil { 81 return err 82 } 83 84 CommandPrettyPrintln("") 85 CommandPrintln(log.String()) 86 CommandPrettyPrintln("") 87 88 CommandPrettyPrintln("Finished Slack Import.") 89 CommandPrettyPrintln("") 90 91 auditRec := a.MakeAuditRecord("slackImport", audit.Success) 92 auditRec.AddMeta("team", team) 93 auditRec.AddMeta("file", args[1]) 94 a.LogAuditRec(auditRec, nil) 95 96 return nil 97 } 98 99 func bulkImportCmdF(command *cobra.Command, args []string) error { 100 a, err := InitDBCommandContextCobra(command) 101 if err != nil { 102 return err 103 } 104 defer a.Srv().Shutdown() 105 106 apply, err := command.Flags().GetBool("apply") 107 if err != nil { 108 return errors.New("Apply flag error") 109 } 110 111 validate, err := command.Flags().GetBool("validate") 112 if err != nil { 113 return errors.New("Validate flag error") 114 } 115 116 workers, err := command.Flags().GetInt("workers") 117 if err != nil { 118 return errors.New("Workers flag error") 119 } 120 121 if len(args) != 1 { 122 return errors.New("Incorrect number of arguments.") 123 } 124 125 fileReader, err := os.Open(args[0]) 126 if err != nil { 127 return err 128 } 129 defer fileReader.Close() 130 131 if apply && validate { 132 CommandPrettyPrintln("Use only one of --apply or --validate.") 133 return nil 134 } 135 136 if apply && !validate { 137 CommandPrettyPrintln("Running Bulk Import. This may take a long time.") 138 } else { 139 CommandPrettyPrintln("Running Bulk Import Data Validation.") 140 CommandPrettyPrintln("** This checks the validity of the entities in the data file, but does not persist any changes **") 141 CommandPrettyPrintln("Use the --apply flag to perform the actual data import.") 142 } 143 144 CommandPrettyPrintln("") 145 146 if err, lineNumber := a.BulkImport(fileReader, !apply, workers); err != nil { 147 CommandPrintErrorln(err.Error()) 148 if lineNumber != 0 { 149 CommandPrintErrorln(fmt.Sprintf("Error occurred on data file line %v", lineNumber)) 150 } 151 return err 152 } 153 154 if apply { 155 CommandPrettyPrintln("Finished Bulk Import.") 156 auditRec := a.MakeAuditRecord("bulkImport", audit.Success) 157 auditRec.AddMeta("file", args[0]) 158 a.LogAuditRec(auditRec, nil) 159 } else { 160 CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.") 161 } 162 163 return nil 164 }