goharbor / goharbor/harbor-cli

bug: Redundant API calls for user deletion

Open
#697 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
163
Forks
211
Avg merge
1m
Merged PRs (30d)
1

Description

## Refactor: Optimize User Deletion and Improve Error Handling

### Description
The current implementation of the `user delete` command is inefficient when deleting multiple users. It performs redundant API calls and lacks standardized error reporting via `RunE`.

### Current Issues

1. **Redundant API Calls:** For every username passed in the arguments, the command calls `GetUsersIdByName`. This function fetches the **entire list of users** every single time it is called.

```
for _, arg := range args {
// Retrieve user ID by name.
userID, err := api.GetUsersIdByName(arg)
if err != nil {
log.Errorf("failed to get user id for '%s': %v", arg, err)
continue
}
wg.Add(1)
go func(userID int64) {
defer wg.Done()
if err := api.DeleteUser(userID); err != nil {
errChan <- err
}
}(userID)
}
```

```
// Inside GetUsersIdByName
u, err := ListUsers(opts) // Fetches ALL users
for _, user := range u.Payload {
if user.Username == userName {
return user.UserID, nil
}
}
```

If a user deletes n accounts, the CLI fetches the full user list n times, making the subsequent concurrent deletion logic pointless due to the slow user id search phase.

2. **Inconsistent Output:**
The command does not use `RunE` or a standardized view to report success and failure messages, which is inconsistent with newer delete commands in the CLI.

### Proposed Changes

* **Batch Lookup:** Fetch the full list of users **only once** at the start of the command.
* **Standardize UI:** Use `RunE` and clear success/error messaging (similar to the logic used in newer delete command implementations).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.