From 66d485f639ad3ee8483fece5c987cd9e1ab05838 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 Former-commit-id: 7cc788c0560fb54cd098f4b6039b42d46fd78561 [formerly 7fd126f9152827162e9cf17dcd3ea293793ce504] [formerly bef00ad6a024835145bea635511be47b7e1f8a16 [formerly 76bf72902d90208cf6f49daf4a52deef6bc00fdd]] Former-commit-id: afc342bb45e4b8d3f059c33f2c60043f9612210b [formerly 149f17999b12be474cc91e2b821a528ce98adfcf] Former-commit-id: b855a6691f1661b5d8af13a58f80773eda2d8a96 --- 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))