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