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/doc/cli-docs/harbor-artifact-labels-add.md b/doc/cli-docs/harbor-artifact-labels-add.md new file mode 100644 index 00000000..06f0c861 --- /dev/null +++ b/doc/cli-docs/harbor-artifact-labels-add.md @@ -0,0 +1,38 @@ +--- +title: harbor artifact labels add +weight: 95 +--- +## harbor artifact labels add + +### Description + +##### Add a label of an artifact + +```sh +harbor artifact labels add [flags] +``` + +### Examples + +```sh +harbor artifact labels add // +``` + +### Options + +```sh + -h, --help help for add +``` + +### Options inherited from parent commands + +```sh + -c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml) + -o, --output-format string Output format. One of: json|yaml + -v, --verbose verbose output +``` + +### SEE ALSO + +* [harbor artifact labels](harbor-artifact-labels.md) - Manage labels of an artifact + diff --git a/doc/cli-docs/harbor-artifact-labels-remove.md b/doc/cli-docs/harbor-artifact-labels-remove.md new file mode 100644 index 00000000..ba6ea3c2 --- /dev/null +++ b/doc/cli-docs/harbor-artifact-labels-remove.md @@ -0,0 +1,38 @@ +--- +title: harbor artifact labels remove +weight: 15 +--- +## harbor artifact labels remove + +### Description + +##### Remove a label of an artifact + +```sh +harbor artifact labels remove [flags] +``` + +### Examples + +```sh +harbor artifact labels remove // +``` + +### Options + +```sh + -h, --help help for remove +``` + +### Options inherited from parent commands + +```sh + -c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml) + -o, --output-format string Output format. One of: json|yaml + -v, --verbose verbose output +``` + +### SEE ALSO + +* [harbor artifact labels](harbor-artifact-labels.md) - Manage labels of an artifact + diff --git a/doc/cli-docs/harbor-artifact-labels.md b/doc/cli-docs/harbor-artifact-labels.md new file mode 100644 index 00000000..21b8795d --- /dev/null +++ b/doc/cli-docs/harbor-artifact-labels.md @@ -0,0 +1,30 @@ +--- +title: harbor artifact labels +weight: 80 +--- +## harbor artifact labels + +### Description + +##### Manage labels of an artifact + +### Options + +```sh + -h, --help help for labels +``` + +### Options inherited from parent commands + +```sh + -c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml) + -o, --output-format string Output format. One of: json|yaml + -v, --verbose verbose output +``` + +### SEE ALSO + +* [harbor artifact](harbor-artifact.md) - Manage artifacts +* [harbor artifact labels add](harbor-artifact-labels-add.md) - Add a label of an artifact +* [harbor artifact labels remove](harbor-artifact-labels-remove.md) - Remove a label of an artifact + diff --git a/doc/man-docs/man1/harbor-artifact-labels-add.1 b/doc/man-docs/man1/harbor-artifact-labels-add.1 new file mode 100644 index 00000000..c32763ac --- /dev/null +++ b/doc/man-docs/man1/harbor-artifact-labels-add.1 @@ -0,0 +1,45 @@ +.nh +.TH "HARBOR" "1" "Feb 2025" "Habor Community" "Harbor User Mannuals" + +.SH NAME +harbor-artifact-labels-add - Add a label of an artifact + + +.SH SYNOPSIS +\fBharbor artifact labels add [flags]\fP + + +.SH DESCRIPTION +Add a label of an artifact + + +.SH OPTIONS +\fB-h\fP, \fB--help\fP[=false] + help for add + + +.SH OPTIONS INHERITED FROM PARENT COMMANDS +\fB-c\fP, \fB--config\fP="" + config file (default is $HOME/.config/harbor-cli/config.yaml) + +.PP +\fB-o\fP, \fB--output-format\fP="" + Output format. One of: json|yaml + +.PP +\fB-v\fP, \fB--verbose\fP[=false] + verbose output + + +.SH EXAMPLE +.EX +harbor artifact labels add // +.EE + + +.SH SEE ALSO +\fBharbor-artifact-labels(1)\fP + + +.SH HISTORY +5-Feb-2025 Auto generated by spf13/cobra diff --git a/doc/man-docs/man1/harbor-artifact-labels-remove.1 b/doc/man-docs/man1/harbor-artifact-labels-remove.1 new file mode 100644 index 00000000..5c033324 --- /dev/null +++ b/doc/man-docs/man1/harbor-artifact-labels-remove.1 @@ -0,0 +1,45 @@ +.nh +.TH "HARBOR" "1" "Feb 2025" "Habor Community" "Harbor User Mannuals" + +.SH NAME +harbor-artifact-labels-remove - Remove a label of an artifact + + +.SH SYNOPSIS +\fBharbor artifact labels remove [flags]\fP + + +.SH DESCRIPTION +Remove a label of an artifact + + +.SH OPTIONS +\fB-h\fP, \fB--help\fP[=false] + help for remove + + +.SH OPTIONS INHERITED FROM PARENT COMMANDS +\fB-c\fP, \fB--config\fP="" + config file (default is $HOME/.config/harbor-cli/config.yaml) + +.PP +\fB-o\fP, \fB--output-format\fP="" + Output format. One of: json|yaml + +.PP +\fB-v\fP, \fB--verbose\fP[=false] + verbose output + + +.SH EXAMPLE +.EX +harbor artifact labels remove // +.EE + + +.SH SEE ALSO +\fBharbor-artifact-labels(1)\fP + + +.SH HISTORY +5-Feb-2025 Auto generated by spf13/cobra diff --git a/doc/man-docs/man1/harbor-artifact-labels.1 b/doc/man-docs/man1/harbor-artifact-labels.1 new file mode 100644 index 00000000..6e3f65dc --- /dev/null +++ b/doc/man-docs/man1/harbor-artifact-labels.1 @@ -0,0 +1,39 @@ +.nh +.TH "HARBOR" "1" "Feb 2025" "Habor Community" "Harbor User Mannuals" + +.SH NAME +harbor-artifact-labels - Manage labels of an artifact + + +.SH SYNOPSIS +\fBharbor artifact labels [flags]\fP + + +.SH DESCRIPTION +Manage labels of an artifact + + +.SH OPTIONS +\fB-h\fP, \fB--help\fP[=false] + help for labels + + +.SH OPTIONS INHERITED FROM PARENT COMMANDS +\fB-c\fP, \fB--config\fP="" + config file (default is $HOME/.config/harbor-cli/config.yaml) + +.PP +\fB-o\fP, \fB--output-format\fP="" + Output format. One of: json|yaml + +.PP +\fB-v\fP, \fB--verbose\fP[=false] + verbose output + + +.SH SEE ALSO +\fBharbor-artifact(1)\fP, \fBharbor-artifact-labels-add(1)\fP, \fBharbor-artifact-labels-remove(1)\fP + + +.SH HISTORY +5-Feb-2025 Auto generated by spf13/cobra 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 +}