feat: add replace opt
Former-commit-id: 7cc788c0560fb54cd098f4b6039b42d46fd78561 [formerly 7fd126f9152827162e9cf17dcd3ea293793ce504] [formerly bef00ad6a024835145bea635511be47b7e1f8a16 [formerly 76bf72902d90208cf6f49daf4a52deef6bc00fdd]] Former-commit-id: afc342bb45e4b8d3f059c33f2c60043f9612210b [formerly 149f17999b12be474cc91e2b821a528ce98adfcf] Former-commit-id: b855a6691f1661b5d8af13a58f80773eda2d8a96
This commit is contained in:
parent
684366e050
commit
66d485f639
|
@ -12,6 +12,7 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
usersCmd.AddCommand(usersImportCmd)
|
usersCmd.AddCommand(usersImportCmd)
|
||||||
usersImportCmd.Flags().Bool("overwrite", false, "overwrite users with the same id/username combo")
|
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{
|
var usersImportCmd = &cobra.Command{
|
||||||
|
@ -32,10 +33,23 @@ var usersImportCmd = &cobra.Command{
|
||||||
checkErr(err)
|
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")
|
overwrite := mustGetBool(cmd, "overwrite")
|
||||||
|
|
||||||
for _, user := range list {
|
for _, user := range list {
|
||||||
old, err := d.store.Users.Get("", user.ID)
|
onDB, err := d.store.Users.Get("", user.ID)
|
||||||
|
|
||||||
// User exists in DB.
|
// User exists in DB.
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -46,7 +60,7 @@ var usersImportCmd = &cobra.Command{
|
||||||
// If the usernames mismatch, check if there is another one in the DB
|
// 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
|
// with the new username. If there is, print an error and cancel the
|
||||||
// operation
|
// operation
|
||||||
if user.Username != old.Username {
|
if user.Username != onDB.Username {
|
||||||
conflictuous, err := d.store.Users.Get("", user.Username)
|
conflictuous, err := d.store.Users.Get("", user.Username)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
checkErr(usernameConflictError(user.Username, conflictuous.ID, user.ID))
|
checkErr(usernameConflictError(user.Username, conflictuous.ID, user.ID))
|
||||||
|
|
Loading…
Reference in New Issue