Skip to content

Commit

Permalink
feat: support labels add/remove on artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
Standing-Man committed Jan 19, 2025
1 parent afd5683 commit 2b48067
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/harbor/root/artifact/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Artifact() *cobra.Command {
DeleteArtifactCommand(),
ScanArtifactCommand(),
ArtifactTagsCmd(),
ArtifactLabelsCmd(),
)

return cmd
Expand Down
98 changes: 98 additions & 0 deletions cmd/harbor/root/artifact/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package artifact

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func ArtifactLabelsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "labels",
Short: "Manage labels of an artifact",
}

cmd.AddCommand(
AddLabelsCmd(),
RemoveLabelsCmd(),
)

return cmd
}

func AddLabelsCmd() *cobra.Command {
var opts api.ListFlags
cmd := &cobra.Command{
Use: "add",
Short: "Add a label of an artifact",
Example: `harbor artifact labels add <project>/<repository>/<reference> <labelName|labelID>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
var projectName, repoName, reference string
var labelID int64

if len(args) > 0 {
projectName, repoName, reference = utils.ParseProjectRepoReference(args[0])
labelID, err = api.GetLabelIdByName(args[1])
if err != nil {
logrus.Errorf("Failed to get this lable: %s", args[1])
return
}
} else {
projectName = prompt.GetProjectNameFromUser()
repoName = prompt.GetRepoNameFromUser(projectName)
reference = prompt.GetReferenceFromUser(repoName, projectName)
labelID = prompt.GetLabelIdFromUser(opts)
}
err = api.AddLabel(projectName, repoName, reference, labelID)

if err != nil {
logrus.Errorf("Failed to add label %s/%s@%s", projectName, repoName, reference)
return
}

},
}

return cmd
}

func RemoveLabelsCmd() *cobra.Command {
var opts api.ListFlags

cmd := &cobra.Command{
Use: "remove",
Short: "Remove a label of an artifact",
Example: `harbor artifact labels remove <project>/<repository>/<reference> <labelName|labelID>`,
Run: func(cmd *cobra.Command, args []string) {
var err error
var projectName, repoName, reference string
var labelID int64

if len(args) > 0 {
projectName, repoName, reference = utils.ParseProjectRepoReference(args[0])
labelID, err = api.GetLabelIdByName(args[1])
if err != nil {
logrus.Errorf("Failed to get this lable: %s", args[1])
return
}
} else {
projectName = prompt.GetProjectNameFromUser()
repoName = prompt.GetRepoNameFromUser(projectName)
reference = prompt.GetReferenceFromUser(repoName, projectName)
labelID = prompt.GetLabelIdFromUser(opts)
}
err = api.RemoveLabel(projectName, repoName, reference, labelID)

if err != nil {
logrus.Errorf("Failed to remove label %s/%s@%s", projectName, repoName, reference)
return
}

},
}

return cmd
}
40 changes: 40 additions & 0 deletions pkg/api/artifact_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,43 @@ func CreateTag(projectName, repoName, reference, tagName string) error {
log.Infof("Tag created successfully: %s/%s@%s:%s", projectName, repoName, reference, tagName)
return nil
}

func AddLabel(projectName, repoName, reference string, labelID int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

_, err = client.Artifact.AddLabel(ctx, &artifact.AddLabelParams{
ProjectName: projectName,
RepositoryName: repoName,
Reference: reference,
Label: &models.Label{
ID: labelID,
},
})
if err != nil {
return err
}
log.Infof("Label added successfully: %s/%s@%s", projectName, repoName, reference)
return nil
}

func RemoveLabel(projectName, repoName, reference string, labelID int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

_, err = client.Artifact.RemoveLabel(ctx, &artifact.RemoveLabelParams{
ProjectName: projectName,
RepositoryName: repoName,
Reference: reference,
LabelID: labelID,
})
if err != nil {
return err
}
log.Infof("Label removed successfully: %s/%s@%s", projectName, repoName, reference)
return nil
}

0 comments on commit 2b48067

Please sign in to comment.