Skip to content

Commit

Permalink
Add: label commands (goharbor#95)
Browse files Browse the repository at this point in the history
* created new command label and its subcommands

Signed-off-by: Althaf66 <althafasharaf02@gmail.com>

* modified label cmd

Signed-off-by: Althaf66 <althafasharaf02@gmail.com>

* created update label cmd

Signed-off-by: ALTHAF <althafasharaf02@gmail.com>

* modified label update cmd

Signed-off-by: ALTHAF <althafasharaf02@gmail.com>

* modified label update cmd

Signed-off-by: ALTHAF <althafasharaf02@gmail.com>

* added 32 color choice for label

Signed-off-by: ALTHAF <althafasharaf02@gmail.com>

---------

Signed-off-by: Althaf66 <althafasharaf02@gmail.com>
Signed-off-by: ALTHAF <114910365+Althaf66@users.noreply.github.com>
Signed-off-by: ALTHAF <althafasharaf02@gmail.com>
  • Loading branch information
Althaf66 authored Dec 10, 2024
1 parent e0e8a05 commit 2e06912
Show file tree
Hide file tree
Showing 15 changed files with 598 additions and 8 deletions.
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"github.com/goharbor/harbor-cli/cmd/harbor/root/labels"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
Expand Down Expand Up @@ -67,6 +68,7 @@ harbor help
artifact.Artifact(),
HealthCommand(),
schedule.Schedule(),
labels.Labels(),
)

return root
Expand Down
1 change: 0 additions & 1 deletion cmd/harbor/root/labels/add.go

This file was deleted.

6 changes: 6 additions & 0 deletions cmd/harbor/root/labels/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ func Labels() *cobra.Command {
Use: "label",
Short: "Manage labels in Harbor",
}
cmd.AddCommand(
CreateLabelCommand(),
DeleteLabelCommand(),
ListLabelCommand(),
UpdateLableCommand(),
)

return cmd
}
56 changes: 56 additions & 0 deletions cmd/harbor/root/labels/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package labels

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/views/label/create"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func CreateLabelCommand() *cobra.Command {
var opts create.CreateView

cmd := &cobra.Command{
Use: "create",
Short: "create label",
Long: "create label in harbor",
Example: "harbor label create",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var err error
createView := &create.CreateView{
Name: opts.Name,
Color: opts.Color,
Scope: opts.Scope,
Description: opts.Description,
}
if opts.Name != "" && opts.Scope != "" {
err = api.CreateLabel(opts)
} else {
err = createLabelView(createView)
}

if err != nil {
log.Errorf("failed to create label: %v", err)
}

},
}

flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the label")
flags.StringVarP(&opts.Color, "color", "", "#FFFFFF", "Color of the label.color is in hex value")
flags.StringVarP(&opts.Scope, "scope", "s", "g", "Scope of the label. eg- g(global), p(specific project)")
flags.StringVarP(&opts.Description, "description", "d", "", "Description of the label")

return cmd
}

func createLabelView(createView *create.CreateView) error {
if createView == nil {
createView = &create.CreateView{}
}

create.CreateLabelView(createView)
return api.CreateLabel(*createView)
}
39 changes: 39 additions & 0 deletions cmd/harbor/root/labels/delete.go
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
package labels

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"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func DeleteLabelCommand() *cobra.Command {
var opts models.Label
cmd := &cobra.Command{
Use: "delete",
Short: "delete label",
Example: "harbor label delete [labelname]",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
deleteView := &api.ListFlags{
Scope: opts.Scope,
}

if len(args) > 0 {
labelId, _ := api.GetLabelIdByName(args[0])
err = api.DeleteLabel(labelId)
} else {
labelId := prompt.GetLabelIdFromUser(*deleteView)
err = api.DeleteLabel(labelId)
}
if err != nil {
log.Errorf("failed to delete label: %v", err)
}
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.Scope, "scope", "s", "g", "default(global).'p' for project labels.Query scope of the label")

return cmd
}
41 changes: 41 additions & 0 deletions cmd/harbor/root/labels/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package labels

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/label/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

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

cmd := &cobra.Command{
Use: "list",
Short: "list labels",
Run: func(cmd *cobra.Command, args []string) {
label, err := api.ListLabel(opts)
if err != nil {
log.Fatalf("failed to get label list: %v", err)
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(label)
return
}
list.ListLabels(label.Payload)
},
}

flags := cmd.Flags()
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.PageSize, "page-size", "", 20, "Size of per page")
flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources")
flags.StringVarP(&opts.Scope, "scope", "s", "g", "default(global).'p' for project labels.Query scope of the label")
flags.Int64VarP(&opts.ProjectID, "projectid", "i", 1, "project ID when query project labels")
flags.StringVarP(&opts.Sort, "sort", "", "", "Sort the label list in ascending or descending order")

return cmd
}
76 changes: 76 additions & 0 deletions cmd/harbor/root/labels/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package labels

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/views/label/update"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func UpdateLableCommand() *cobra.Command {
opts := &models.Label{}

cmd := &cobra.Command{
Use: "update",
Short: "update label",
Example: "harbor label update [labelname]",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
var labelId int64
updateflags := api.ListFlags{
Scope: opts.Scope,
}

if len(args) > 0 {
labelId, err = api.GetLabelIdByName(args[0])
} else {
labelId = prompt.GetLabelIdFromUser(updateflags)
}
if err != nil {
log.Errorf("failed to parse label id: %v", err)
}

existingLabel := api.GetLabel(labelId)
if existingLabel == nil {
log.Errorf("label is not found")
return
}
updateView := &models.Label{
Name: existingLabel.Name,
Color: existingLabel.Color,
Description: existingLabel.Description,
Scope: existingLabel.Scope,
}

flags := cmd.Flags()
if flags.Changed("name") {
updateView.Name = opts.Name
}
if flags.Changed("color") {
updateView.Color = opts.Color
}
if flags.Changed("description") {
updateView.Description = opts.Description
}
if flags.Changed("scope") {
updateView.Scope = opts.Scope
}

update.UpdateLabelView(updateView)
err = api.UpdateLabel(updateView, labelId)
if err != nil {
log.Errorf("failed to update label: %v", err)
}
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the label")
flags.StringVarP(&opts.Color, "color", "", "", "Color of the label.color is in hex value")
flags.StringVarP(&opts.Scope, "scope", "s", "g", "Scope of the label. eg- g(global), p(specific project)")
flags.StringVarP(&opts.Description, "description", "d", "", "Description of the label")

return cmd
}
2 changes: 1 addition & 1 deletion cmd/harbor/root/registry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func DeleteRegistryCommand() *cobra.Command {

cmd := &cobra.Command{
Use: "delete",
Short: "delete registry by id",
Short: "delete registry",
Example: "harbor registry delete [registryname]",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
124 changes: 124 additions & 0 deletions pkg/api/label_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package api

import (
"fmt"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/label"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/label/create"
)

func CreateLabel(opts create.CreateView) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}
_, err = client.Label.CreateLabel(ctx, &label.CreateLabelParams{Label: &models.Label{Name: opts.Name, Color: opts.Color, Description: opts.Description, Scope: opts.Scope}})

if err != nil {
return err
}

fmt.Printf("Label %s created\n", opts.Name)
return nil
}

func DeleteLabel(Labelid int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}
_, err = client.Label.DeleteLabel(ctx, &label.DeleteLabelParams{LabelID: Labelid})

if err != nil {
return err
}

fmt.Println("label deleted successfully")

return nil
}

func ListLabel(opts ...ListFlags) (*label.ListLabelsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}

var listFlags ListFlags

if len(opts) > 0 {
listFlags = opts[0]
}
scope := "g"
response, err := client.Label.ListLabels(ctx, &label.ListLabelsParams{
Page: &listFlags.Page,
PageSize: &listFlags.PageSize,
Q: &listFlags.Q,
Sort: &listFlags.Sort,
Scope: &scope,
ProjectID: &listFlags.ProjectID,
})

if err != nil {
return nil, err
}

return response, nil
}

func UpdateLabel(updateView *models.Label, Labelid int64) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}
labelUpdate := &models.Label{
Name: updateView.Name,
Color: updateView.Color,
Description: updateView.Description,
Scope: updateView.Scope,
}

_, err = client.Label.UpdateLabel(
ctx,
&label.UpdateLabelParams{LabelID: Labelid, Label: labelUpdate},
)
if err != nil {
return err
}

fmt.Println("label updated successfully")

return nil
}

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

return response.GetPayload()
}

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

l, err := ListLabel(opts)
if err != nil {
return 0, err
}

for _, label := range l.Payload {
if label.Name == labelName {
return label.ID, nil
}
}

return 0, err
}
Loading

0 comments on commit 2e06912

Please sign in to comment.