From 505e36ad96cc1e4aa32ca1a9daba39f432e12db5 Mon Sep 17 00:00:00 2001 From: Alan Tang Date: Sun, 19 Jan 2025 16:38:15 +0800 Subject: [PATCH] feat: support labels add/remove on artifact Signed-off-by: Alan Tang --- cmd/harbor/root/artifact/cmd.go | 1 + cmd/harbor/root/artifact/labels.go | 98 ++++++++++++++++++++++++++++++ pkg/api/artifact_handler.go | 40 ++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 cmd/harbor/root/artifact/labels.go diff --git a/cmd/harbor/root/artifact/cmd.go b/cmd/harbor/root/artifact/cmd.go index c3638de3..f30da31a 100644 --- a/cmd/harbor/root/artifact/cmd.go +++ b/cmd/harbor/root/artifact/cmd.go @@ -31,6 +31,7 @@ func Artifact() *cobra.Command { DeleteArtifactCommand(), ScanArtifactCommand(), ArtifactTagsCmd(), + ArtifactLabelsCmd(), ) return cmd diff --git a/cmd/harbor/root/artifact/labels.go b/cmd/harbor/root/artifact/labels.go new file mode 100644 index 00000000..31e94331 --- /dev/null +++ b/cmd/harbor/root/artifact/labels.go @@ -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 // `, + 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 // `, + 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 +} diff --git a/pkg/api/artifact_handler.go b/pkg/api/artifact_handler.go index c790d965..e8344069 100644 --- a/pkg/api/artifact_handler.go +++ b/pkg/api/artifact_handler.go @@ -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 +}