github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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 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 cmd.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 cmd.CommandPrettyPrintln("Finished Slack Import.") 80 81 return nil 82 } 83 84 func bulkImportCmdF(command *cobra.Command, args []string) error { 85 a, err := cmd.InitDBCommandContextCobra(command) 86 if err != nil { 87 return err 88 } 89 90 apply, err := command.Flags().GetBool("apply") 91 if err != nil { 92 return errors.New("Apply flag error") 93 } 94 95 validate, err := command.Flags().GetBool("validate") 96 if err != nil { 97 return errors.New("Validate flag error") 98 } 99 100 workers, err := command.Flags().GetInt("workers") 101 if err != nil { 102 return errors.New("Workers flag error") 103 } 104 105 if len(args) != 1 { 106 return errors.New("Incorrect number of arguments.") 107 } 108 109 fileReader, err := os.Open(args[0]) 110 if err != nil { 111 return err 112 } 113 defer fileReader.Close() 114 115 if apply && validate { 116 cmd.CommandPrettyPrintln("Use only one of --apply or --validate.") 117 return nil 118 } else if apply && !validate { 119 cmd.CommandPrettyPrintln("Running Bulk Import. This may take a long time.") 120 } else { 121 cmd.CommandPrettyPrintln("Running Bulk Import Data Validation.") 122 cmd.CommandPrettyPrintln("** This checks the validity of the entities in the data file, but does not persist any changes **") 123 cmd.CommandPrettyPrintln("Use the --apply flag to perform the actual data import.") 124 } 125 126 cmd.CommandPrettyPrintln("") 127 128 if err, lineNumber := a.BulkImport(fileReader, !apply, workers); err != nil { 129 cmd.CommandPrettyPrintln(err.Error()) 130 if lineNumber != 0 { 131 cmd.CommandPrettyPrintln(fmt.Sprintf("Error occurred on data file line %v", lineNumber)) 132 } 133 } else { 134 if apply { 135 cmd.CommandPrettyPrintln("Finished Bulk Import.") 136 } else { 137 cmd.CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.") 138 } 139 } 140 141 return nil 142 }