diff --git a/cmd_user.go b/cmd_user.go index 9c0ba55..a930d1f 100644 --- a/cmd_user.go +++ b/cmd_user.go @@ -26,6 +26,7 @@ package main import ( "fmt" + "os" "golang.org/x/crypto/bcrypt" ) @@ -127,5 +128,23 @@ type CmdUserDelete struct { } func (cmd CmdUserDelete) Run(app *app) error { - return app.userStore.RemoveUser(cmd.Username) + if err := app.userStore.RemoveUser(cmd.Username); err != nil { + return fmt.Errorf("cannot remove user: %w", err) + } + + sharesByUser, err := app.shareStore.FindSharesByUser(cmd.Username) + if err != nil { + return fmt.Errorf("cannot get shares of user: %w", err) + } + allSuccessful := true + for _, userShare := range sharesByUser { + if err := app.shareStore.RemoveShare(userShare.UUID); err != nil { + fmt.Fprintf(os.Stderr, "User %q cannot be removed from Share %q: %v\n", cmd.Username, userShare.UUID.String(), err) + allSuccessful = false + } + } + if !allSuccessful { + return fmt.Errorf("could not remove user from all shares") + } + return nil }