Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support labels add/remove on artifact #314

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
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
}
38 changes: 38 additions & 0 deletions doc/cli-docs/harbor-artifact-labels-add.md
Original file line number Diff line number Diff line change
@@ -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 <project>/<repository>/<reference> <labelName|labelID>
```

### 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

38 changes: 38 additions & 0 deletions doc/cli-docs/harbor-artifact-labels-remove.md
Original file line number Diff line number Diff line change
@@ -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 <project>/<repository>/<reference> <labelName|labelID>
```

### 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

30 changes: 30 additions & 0 deletions doc/cli-docs/harbor-artifact-labels.md
Original file line number Diff line number Diff line change
@@ -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

45 changes: 45 additions & 0 deletions doc/man-docs/man1/harbor-artifact-labels-add.1
Original file line number Diff line number Diff line change
@@ -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 <project>/<repository>/<reference> <labelName|labelID>
.EE


.SH SEE ALSO
\fBharbor-artifact-labels(1)\fP


.SH HISTORY
5-Feb-2025 Auto generated by spf13/cobra
45 changes: 45 additions & 0 deletions doc/man-docs/man1/harbor-artifact-labels-remove.1
Original file line number Diff line number Diff line change
@@ -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 <project>/<repository>/<reference> <labelName|labelID>
.EE


.SH SEE ALSO
\fBharbor-artifact-labels(1)\fP


.SH HISTORY
5-Feb-2025 Auto generated by spf13/cobra
39 changes: 39 additions & 0 deletions doc/man-docs/man1/harbor-artifact-labels.1
Original file line number Diff line number Diff line change
@@ -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
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
}
Loading