| 65 | } |
| 66 | |
| 67 | func userRemoveCmd(args []string, stdin io.Reader) { |
| 68 | fs := setupFlagSet("remove", "dnote-server user remove") |
| 69 | |
| 70 | email := fs.String("email", "", "User email address (required)") |
| 71 | dbPath := fs.String("dbPath", "", "Path to SQLite database file (env: DBPath, default: $XDG_DATA_HOME/dnote/server.db)") |
| 72 | |
| 73 | fs.Parse(args) |
| 74 | |
| 75 | requireString(fs, *email, "email") |
| 76 | |
| 77 | a, cleanup := createApp(fs, *dbPath) |
| 78 | defer cleanup() |
| 79 | |
| 80 | // Check if user exists first |
| 81 | _, err := a.GetUserByEmail(*email) |
| 82 | if err != nil { |
| 83 | if errors.Is(err, app.ErrNotFound) { |
| 84 | fmt.Printf("Error: user with email %s not found\n", *email) |
| 85 | } else { |
| 86 | log.ErrorWrap(err, "finding user") |
| 87 | } |
| 88 | os.Exit(1) |
| 89 | } |
| 90 | |
| 91 | // Show confirmation prompt |
| 92 | ok, err := confirm(stdin, fmt.Sprintf("Remove user %s?", *email), false) |
| 93 | if err != nil { |
| 94 | log.ErrorWrap(err, "getting confirmation") |
| 95 | os.Exit(1) |
| 96 | } |
| 97 | if !ok { |
| 98 | fmt.Println("Aborted by user") |
| 99 | os.Exit(0) |
| 100 | } |
| 101 | |
| 102 | // Remove the user |
| 103 | if err := a.RemoveUser(*email); err != nil { |
| 104 | if errors.Is(err, app.ErrNotFound) { |
| 105 | fmt.Printf("Error: user with email %s not found\n", *email) |
| 106 | } else if errors.Is(err, app.ErrUserHasExistingResources) { |
| 107 | fmt.Printf("Error: %s\n", err) |
| 108 | } else { |
| 109 | log.ErrorWrap(err, "removing user") |
| 110 | } |
| 111 | os.Exit(1) |
| 112 | } |
| 113 | |
| 114 | fmt.Printf("User removed successfully\n") |
| 115 | fmt.Printf("Email: %s\n", *email) |
| 116 | } |
| 117 | |
| 118 | func userResetPasswordCmd(args []string) { |
| 119 | fs := setupFlagSet("reset-password", "dnote-server user reset-password") |