[bug]: artifact tags create and delete panic with index out of range when exactly one argument is provided

Open Beginner friendly
#1,069 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
go
Domain
cli

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.

Description

bug status/has-approved-pr

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 <reference> and <tag> 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:

Error: requires both <project>/<repository>/<reference> and <tag>, 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:

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:

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.

Dominant language
Go
Stars
163
Forks
212
Avg merge
1m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from goharbor/harbor-cli

All issues in goharbor/harbor-cli

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.