From 76bf72902d90208cf6f49daf4a52deef6bc00fdd Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 8 Jan 2019 12:02:49 +0000 Subject: [PATCH] feat: add replace opt --- cmd/users_import.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cmd/users_import.go b/cmd/users_import.go index a9af4d0f..ad0dc9e7 100644 --- a/cmd/users_import.go +++ b/cmd/users_import.go @@ -12,6 +12,7 @@ import ( func init() { usersCmd.AddCommand(usersImportCmd) usersImportCmd.Flags().Bool("overwrite", false, "overwrite users with the same id/username combo") + usersImportCmd.Flags().Bool("replace", false, "replace the entire user base") } var usersImportCmd = &cobra.Command{ @@ -32,10 +33,23 @@ var usersImportCmd = &cobra.Command{ checkErr(err) } + if mustGetBool(cmd, "replace") { + oldUsers, err := d.store.Users.Gets("") + checkErr(err) + + err = marshal("users.backup.json", list) + checkErr(err) + + for _, user := range oldUsers { + err = d.store.Users.Delete(user.ID) + checkErr(err) + } + } + overwrite := mustGetBool(cmd, "overwrite") for _, user := range list { - old, err := d.store.Users.Get("", user.ID) + onDB, err := d.store.Users.Get("", user.ID) // User exists in DB. if err == nil { @@ -46,7 +60,7 @@ var usersImportCmd = &cobra.Command{ // If the usernames mismatch, check if there is another one in the DB // with the new username. If there is, print an error and cancel the // operation - if user.Username != old.Username { + if user.Username != onDB.Username { conflictuous, err := d.store.Users.Get("", user.Username) if err == nil { checkErr(usernameConflictError(user.Username, conflictuous.ID, user.ID))