goharbor / goharbor/harbor-cli
[bug]: artifact tags create and delete panic with index out of range when exactly one argument is provided
- Dominant language
- Go
- Stars
- 163
- Forks
- 211
- Avg merge
- 1m
- Merged PRs (30d)
- 1
Description
## Description
`harbor artifact tags create` and `harbor artifact tags delete` both panic with an index out of range error when called with exactly one argument.
Both commands support two modes:
- **0 arguments** — launches interactive prompts to select project, repo, reference, and tag
- **2 arguments** — takes `` and `` directly, skips prompts
Neither command defines an `Args` validator on the `cobra.Command`. When a user passes exactly one argument, the code enters the non-interactive branch (because `len(args) > 0` is true) and immediately accesses `args[1]`, which does not exist. The CLI crashes instead of showing a usage error.
## Steps to Reproduce
1. Run `harbor artifact tags create myproject/myrepo/sha256:abc123` (provides a reference but omits the tag name).
2. Alternatively, run `harbor artifact tags delete myproject/myrepo/sha256:abc123`.
3. The CLI panics with a runtime index out of range error instead of exiting cleanly.
## Expected Behavior
A clear error message explaining what is missing, printed by Cobra before any code in `RunE` executes:
```text
Error: requires both // and , got only one argument
```
The CLI should exit with a non-zero status code and make no API calls.
## Actual Behavior
The CLI crashes with:
```text
panic: runtime error: index out of range 1 with length 1
goroutine 1 running:
[github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/tags.(*CreateTagsCmd...).RunE](https://github.com/goharbor/harbor-cli/cmd/harbor/root/artifact/tags.(*CreateTagsCmd...).RunE)(...)
cmd/harbor/root/artifact/tags/create.go:42
```
The crash originates at these lines in both `cmd/harbor/root/artifact/tags/create.go:36-42` and `delete.go:34-40`:
```go
if len(args) > 0 {
projectName, repoName, reference, err = utils.ParseProjectRepoReference(args[0])
if err != nil {
return fmt.Errorf("failed to parse project/repo/reference: %v", err)
}
tagName = args[1] // ← PANICS when len(args) == 1
} else {
// interactive prompt path
}
```
## Environment
- **OS:** macOS / Linux / Windows (any)
- **Harbor CLI version:** `main` branch
- **Harbor server version:** N/A — the panic occurs locally before any API call is made
## Additional Context
**Why the simple fix won't work** — The obvious fix, adding `Args: cobra.ExactArgs(2)`, is incorrect here because both commands also support a 0-argument interactive mode. Using `ExactArgs(2)` would break the interactive prompt flow that already works correctly.
**Proposed approach** — Add a custom `Args` validator function that explicitly handles the argument count cases:
- `0 args`: Allowed (launches interactive mode)
- `2 args`: Allowed (non-interactive execution)
- `1` or `3+ args`: Reject with a clear Cobra error message
This preserves the existing interactive functionality while preventing the panic and giving the user a clear, helpful error message.
**Scope** — Both `create.go` and `delete.go` in `cmd/harbor/root/artifact/tags/` have the identical bug. A single pull request will fix both files, as they are in the same package (`artifacttags`) and share the same code pattern.
I believe a custom Args validator could work here , but I'm open to
other approaches if you have a different preference.
Contributor guide
Research direction
Start with the command definitions and RunE paths in cmd/harbor/root/artifact/tags/create.go and delete.go, especially the argument access identified in the issue. Verify the validator behavior for zero, one, two, and three or more arguments. Done means zero and two arguments retain their existing modes, invalid counts produce a clear Cobra error, and no panic or API call occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 75/100