forked from goharbor/harbor-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
15 changed files
with
598 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.