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