Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/harbor/root/artifact/label/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ Examples:
}
}

label := api.GetLabel(labelID)
label, err := api.GetLabel(labelID)
if err != nil {
return fmt.Errorf("failed to get label: %v", utils.ParseHarborErrorMsg(err))
}

if _, err := api.AddLabelArtifact(projectName, repoName, reference, label); err != nil {
return fmt.Errorf("failed to add label to artifact: %v", utils.ParseHarborErrorMsg(err))
Expand Down
7 changes: 4 additions & 3 deletions cmd/harbor/root/labels/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/label/update"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -68,9 +69,9 @@ func UpdateLableCommand() *cobra.Command {
return fmt.Errorf("failed to parse label id: %v", err)
}

existingLabel := api.GetLabel(labelId)
if existingLabel == nil {
return fmt.Errorf("label is not found")
existingLabel, err := api.GetLabel(labelId)
if err != nil {
return fmt.Errorf("failed to get label: %v", utils.ParseHarborErrorMsg(err))
}
Comment on lines +72 to 75
updateView := &models.Label{
Name: existingLabel.Name,
Expand Down
13 changes: 9 additions & 4 deletions pkg/api/label_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,22 @@ func UpdateLabel(updateView *models.Label, Labelid int64) error {
return nil
}

func GetLabel(labelid int64) *models.Label {
func GetLabel(labelid int64) (*models.Label, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil
return nil, err
}
response, err := client.Label.GetLabelByID(ctx, &label.GetLabelByIDParams{LabelID: labelid})
if err != nil {
return nil
return nil, err
}

payload := response.GetPayload()
if payload == nil || payload.ID == 0 {
return nil, fmt.Errorf("label with id %d not found", labelid)
}

return response.GetPayload()
return payload, nil
}
Comment on lines 116 to 126

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a distinction for label being returned nil / 0
As label not found.


func GetLabelIdByName(labelName string, opts ListFlags) (int64, error) {
Expand Down