diff options
author | George Goldberg <george@gberg.me> | 2017-01-27 15:14:54 +0000 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2017-01-27 10:14:54 -0500 |
commit | e07e9937e0e0ae4fcb2a51553238a7566d1e4c67 (patch) | |
tree | 9ca0511deff58df13d2b00a6d19c7bf77359e889 /cmd/platform/import.go | |
parent | 7b9586a740194a5add773483bd309cff84256b57 (diff) | |
download | chat-e07e9937e0e0ae4fcb2a51553238a7566d1e4c67.tar.gz chat-e07e9937e0e0ae4fcb2a51553238a7566d1e4c67.tar.bz2 chat-e07e9937e0e0ae4fcb2a51553238a7566d1e4c67.zip |
PLT-5366, PLT-5364, PLT-5363: Bulk Import Part 1. (#5204)
This commit provides the first part of the bulk import system.
The CLI command is provided, complete with validation & apply modes. All
the basic properties of Teams and Channels can be imported. Users &
Posts will follow separately in a future commit.
Diffstat (limited to 'cmd/platform/import.go')
-rw-r--r-- | cmd/platform/import.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/cmd/platform/import.go b/cmd/platform/import.go index 09b135354..76ed1fb81 100644 --- a/cmd/platform/import.go +++ b/cmd/platform/import.go @@ -6,6 +6,7 @@ import ( "errors" "os" + "fmt" "github.com/mattermost/platform/app" "github.com/spf13/cobra" ) @@ -23,8 +24,19 @@ var slackImportCmd = &cobra.Command{ RunE: slackImportCmdF, } +var bulkImportCmd = &cobra.Command{ + Use: "bulk [file]", + Short: "Import bulk data.", + Long: "Import data from a Mattermost Bulk Import File.", + Example: " import bulk bulk_data.json", + RunE: bulkImportCmdF, +} + func init() { + bulkImportCmd.Flags().Bool("apply", false, "Save the import data to the database. Use with caution - this cannot be reverted.") + importCmd.AddCommand( + bulkImportCmd, slackImportCmd, ) } @@ -60,3 +72,47 @@ func slackImportCmdF(cmd *cobra.Command, args []string) error { return nil } + +func bulkImportCmdF(cmd *cobra.Command, args []string) error { + initDBCommandContextCobra(cmd) + + apply, err := cmd.Flags().GetBool("apply") + if err != nil { + return errors.New("Apply flag error") + } + + if len(args) != 1 { + return errors.New("Incorrect number of arguments.") + } + + fileReader, err := os.Open(args[0]) + if err != nil { + return err + } + defer fileReader.Close() + + if apply { + CommandPrettyPrintln("Running Bulk Import. This may take a long time.") + } else { + CommandPrettyPrintln("Running Bulk Import Data Validation.") + CommandPrettyPrintln("** This checks the validity of the entities in the data file, but does not persist any changes **") + CommandPrettyPrintln("Use the --apply flag to perform the actual data import.") + } + + CommandPrettyPrintln("") + + if err, lineNumber := app.BulkImport(fileReader, !apply); err != nil { + CommandPrettyPrintln(err.Error()) + if lineNumber != 0 { + CommandPrettyPrintln(fmt.Sprintf("Error occurred on data file line %v", lineNumber)) + } + } + + if apply { + CommandPrettyPrintln("Finished Bulk Import.") + } else { + CommandPrettyPrintln("Validation complete. You can now perform the import by rerunning this command with the --apply flag.") + } + + return nil +} |