Skip to content

Commit da44eac

Browse files
authored
Feat: Add scope options in labels (#560)
1 parent 906c147 commit da44eac

File tree

28 files changed

+439
-183
lines changed

28 files changed

+439
-183
lines changed

cmd/harbor/root/artifact/label/add.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,15 @@ Examples:
6565

6666
if len(args) == 2 {
6767
labelName = args[1]
68-
labelID, err = api.GetLabelIdByName(labelName)
68+
labelID, err = api.GetLabelIdByName(labelName, api.ListFlags{})
6969
if err != nil {
7070
return fmt.Errorf("failed to get label id: %v", utils.ParseHarborErrorMsg(err))
7171
}
7272
} else {
73-
labels, err := api.ListLabel()
73+
labelID, err = prompt.GetLabelIdFromUser(api.ListFlags{})
7474
if err != nil {
75-
return fmt.Errorf("failed to list labels: %v", utils.ParseHarborErrorMsg(err))
75+
return fmt.Errorf("failed to get label id: %v", utils.ParseHarborErrorMsg(err))
7676
}
77-
labelID = prompt.GetLabelIdFromUser(labels.Payload)
7877
}
7978

8079
label := api.GetLabel(labelID)

cmd/harbor/root/artifact/label/delete.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,15 @@ Examples:
6666

6767
if len(args) == 2 {
6868
labelName := args[1]
69-
labelID, err = api.GetLabelIdByName(labelName)
69+
labelID, err = api.GetLabelIdByName(labelName, api.ListFlags{})
7070
if err != nil {
7171
return fmt.Errorf("failed to get label id: %v", utils.ParseHarborErrorMsg(err))
7272
}
7373
} else {
74-
artifact, err := api.ViewArtifact(projectName, repoName, reference, true)
75-
if err != nil || artifact == nil {
76-
return fmt.Errorf("failed to get artifact info: %v", utils.ParseHarborErrorMsg(err))
77-
}
78-
79-
labels := artifact.Payload.Labels
80-
if len(labels) == 0 {
81-
fmt.Printf("No labels found for artifact %s/%s@%s\n", projectName, repoName, reference)
82-
return nil
74+
labelID, err = prompt.GetLabelIdFromUser(api.ListFlags{})
75+
if err != nil {
76+
return fmt.Errorf("failed to get label id: %v", utils.ParseHarborErrorMsg(err))
8377
}
84-
labelID = prompt.GetLabelIdFromUser(labels)
8578
}
8679

8780
if _, err := api.RemoveLabelArtifact(projectName, repoName, reference, labelID); err != nil {

cmd/harbor/root/labels/cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// limitations under the License.
1414
package labels
1515

16-
import "github.com/spf13/cobra"
16+
import (
17+
"github.com/spf13/cobra"
18+
)
1719

1820
func Labels() *cobra.Command {
1921
cmd := &cobra.Command{

cmd/harbor/root/labels/create.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
package labels
1515

1616
import (
17+
"fmt"
18+
1719
"github.com/goharbor/harbor-cli/pkg/api"
20+
"github.com/goharbor/harbor-cli/pkg/prompt"
1821
"github.com/goharbor/harbor-cli/pkg/views/label/create"
1922
log "github.com/sirupsen/logrus"
2023
"github.com/spf13/cobra"
24+
"github.com/spf13/pflag"
2125
)
2226

2327
func CreateLabelCommand() *cobra.Command {
@@ -36,11 +40,13 @@ func CreateLabelCommand() *cobra.Command {
3640
Color: opts.Color,
3741
Scope: opts.Scope,
3842
Description: opts.Description,
43+
ProjectID: opts.ProjectID,
3944
}
4045
if opts.Name != "" && opts.Scope != "" {
4146
err = api.CreateLabel(opts)
4247
} else {
43-
err = createLabelView(createView)
48+
flags := cmd.Flags()
49+
err = createLabelView(createView, flags)
4450
}
4551

4652
if err != nil {
@@ -53,16 +59,26 @@ func CreateLabelCommand() *cobra.Command {
5359
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the label")
5460
flags.StringVarP(&opts.Color, "color", "", "#FFFFFF", "Color of the label.color is in hex value")
5561
flags.StringVarP(&opts.Scope, "scope", "s", "g", "Scope of the label. eg- g(global), p(specific project)")
62+
flags.Int64VarP(&opts.ProjectID, "project", "i", 0, "Id of the project when scope is p")
5663
flags.StringVarP(&opts.Description, "description", "d", "", "Description of the label")
5764

5865
return cmd
5966
}
6067

61-
func createLabelView(createView *create.CreateView) error {
68+
func createLabelView(createView *create.CreateView, flags *pflag.FlagSet) error {
6269
if createView == nil {
6370
createView = &create.CreateView{}
6471
}
6572

6673
create.CreateLabelView(createView)
74+
75+
if createView.Scope == "p" && !flags.Changed("project") {
76+
projectID, err := prompt.GetProjectIDFromUser()
77+
if err != nil {
78+
return fmt.Errorf("failed to get project id: %v", err)
79+
}
80+
81+
createView.ProjectID = projectID
82+
}
6783
return api.CreateLabel(*createView)
6884
}

cmd/harbor/root/labels/delete.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,65 @@ import (
1919
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
2020
"github.com/goharbor/harbor-cli/pkg/api"
2121
"github.com/goharbor/harbor-cli/pkg/prompt"
22-
"github.com/goharbor/harbor-cli/pkg/utils"
2322
"github.com/spf13/cobra"
2423
)
2524

2625
func DeleteLabelCommand() *cobra.Command {
2726
var opts models.Label
27+
var projectName string
28+
var isGlobal bool
29+
2830
cmd := &cobra.Command{
2931
Use: "delete",
3032
Short: "delete label",
3133
Example: "harbor label delete [labelname]",
3234
Args: cobra.MaximumNArgs(1),
3335
RunE: func(cmd *cobra.Command, args []string) error {
34-
var err error
35-
var labelId int64
36+
// Defining ProjectID & Scope based on user inputs
37+
if isGlobal {
38+
opts.Scope = "g"
39+
} else if projectName != "" {
40+
id, err := api.GetProjectIDFromName(projectName)
41+
if err != nil {
42+
return err
43+
}
44+
45+
opts.ProjectID = id
46+
opts.Scope = "p"
47+
} else if opts.ProjectID != 0 {
48+
opts.Scope = "p"
49+
} else {
50+
opts.Scope = "g"
51+
}
52+
3653
deleteView := &api.ListFlags{
37-
Scope: opts.Scope,
54+
Scope: opts.Scope,
55+
ProjectID: opts.ProjectID,
3856
}
3957

58+
var err error
59+
var labelId int64
4060
if len(args) > 0 {
41-
labelId, _ = api.GetLabelIdByName(args[0])
61+
labelId, err = api.GetLabelIdByName(args[0], *deleteView)
4262
} else {
43-
labelList, err := api.ListLabel(*deleteView)
44-
if err != nil {
45-
return fmt.Errorf("failed to get label list: %v", utils.ParseHarborErrorMsg(err))
46-
}
47-
labelId = prompt.GetLabelIdFromUser(labelList.Payload)
63+
labelId, err = prompt.GetLabelIdFromUser(*deleteView)
4864
}
65+
if err != nil {
66+
return fmt.Errorf("failed to get label id: %v", err)
67+
}
68+
4969
err = api.DeleteLabel(labelId)
5070
if err != nil {
51-
return fmt.Errorf("failed to delete label: %v", utils.ParseHarborErrorMsg(err))
71+
return fmt.Errorf("failed to delete label: %v", err)
5272
}
73+
5374
return nil
5475
},
5576
}
5677
flags := cmd.Flags()
57-
flags.StringVarP(&opts.Scope, "scope", "s", "g", "default(global).'p' for project labels.Query scope of the label")
78+
flags.StringVarP(&projectName, "project", "p", "", "project name when query project labels")
79+
flags.BoolVarP(&isGlobal, "global", "", false, "whether to list global or project scope labels. (default scope is global)")
80+
flags.Int64VarP(&opts.ProjectID, "project-id", "i", 0, "project ID when query project labels")
5881

5982
return cmd
6083
}

cmd/harbor/root/labels/list.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ import (
2525
)
2626

2727
func ListLabelCommand() *cobra.Command {
28-
var opts api.ListFlags
29-
28+
var (
29+
opts api.ListFlags
30+
projectName string
31+
isGlobal bool
32+
// For querying, opts.Q
33+
fuzzy []string
34+
match []string
35+
ranges []string
36+
)
3037
cmd := &cobra.Command{
3138
Use: "list",
3239
Short: "list labels",
@@ -36,14 +43,39 @@ func ListLabelCommand() *cobra.Command {
3643
return fmt.Errorf("page size should be less than or equal to 100")
3744
}
3845

46+
// Defining ProjectID & Scope based on user inputs
47+
if isGlobal {
48+
opts.Scope = "g"
49+
} else if projectName != "" {
50+
id, err := api.GetProjectIDFromName(projectName)
51+
if err != nil {
52+
return err
53+
}
54+
55+
opts.ProjectID = id
56+
opts.Scope = "p"
57+
} else if opts.ProjectID != 0 {
58+
opts.Scope = "p"
59+
} else {
60+
opts.Scope = "g"
61+
}
62+
63+
if len(fuzzy) != 0 || len(match) != 0 || len(ranges) != 0 { // Only Building Query if a param exists
64+
q, qErr := utils.BuildQueryParam(fuzzy, match, ranges,
65+
[]string{"name", "id", "label_id", "creation_time", "owner_id", "color", "description"},
66+
)
67+
if qErr != nil {
68+
return qErr
69+
}
70+
71+
opts.Q = q
72+
}
73+
3974
label, err := api.ListLabel(opts)
4075
if err != nil {
4176
log.Fatalf("failed to get label list: %v", err)
4277
}
43-
if len(label.Payload) == 0 {
44-
log.Info("No labels found")
45-
return nil
46-
}
78+
4779
formatFlag := viper.GetString("output-format")
4880
if formatFlag != "" {
4981
err = utils.PrintFormat(label, formatFlag)
@@ -53,6 +85,7 @@ func ListLabelCommand() *cobra.Command {
5385
} else {
5486
list.ListLabels(label.Payload)
5587
}
88+
5689
return nil
5790
},
5891
}
@@ -61,9 +94,13 @@ func ListLabelCommand() *cobra.Command {
6194
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
6295
flags.Int64VarP(&opts.PageSize, "page-size", "", 20, "Size of per page")
6396
flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources")
64-
flags.StringVarP(&opts.Scope, "scope", "s", "g", "default(global).'p' for project labels.Query scope of the label")
65-
flags.Int64VarP(&opts.ProjectID, "projectid", "i", 1, "project ID when query project labels")
97+
flags.StringVarP(&projectName, "project", "p", "", "project name when query project labels")
98+
flags.Int64VarP(&opts.ProjectID, "project-id", "i", 0, "project ID when query project labels")
99+
flags.BoolVarP(&isGlobal, "global", "", false, "whether to list global or project scope labels. (default scope is global)")
66100
flags.StringVarP(&opts.Sort, "sort", "", "", "Sort the label list in ascending or descending order")
101+
flags.StringSliceVar(&fuzzy, "fuzzy", nil, "Fuzzy match filter (key=value)")
102+
flags.StringSliceVar(&match, "match", nil, "exact match filter (key=value)")
103+
flags.StringSliceVar(&ranges, "range", nil, "range filter (key=min~max)")
67104

68105
return cmd
69106
}

cmd/harbor/root/labels/update.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,63 @@
1414
package labels
1515

1616
import (
17+
"fmt"
18+
1719
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
1820
"github.com/goharbor/harbor-cli/pkg/api"
1921
"github.com/goharbor/harbor-cli/pkg/prompt"
2022
"github.com/goharbor/harbor-cli/pkg/views/label/update"
21-
log "github.com/sirupsen/logrus"
2223
"github.com/spf13/cobra"
2324
)
2425

2526
func UpdateLableCommand() *cobra.Command {
2627
opts := &models.Label{}
28+
var projectName string
29+
var isGlobal bool
2730

2831
cmd := &cobra.Command{
2932
Use: "update",
3033
Short: "update label",
3134
Example: "harbor label update [labelname]",
3235
Args: cobra.MaximumNArgs(1),
33-
Run: func(cmd *cobra.Command, args []string) {
36+
RunE: func(cmd *cobra.Command, args []string) error {
3437
var err error
3538
var labelId int64
39+
40+
// Defining ProjectID & Scope based on user inputs
41+
if isGlobal {
42+
opts.Scope = "g"
43+
} else if projectName != "" {
44+
id, err := api.GetProjectIDFromName(projectName)
45+
if err != nil {
46+
return err
47+
}
48+
49+
opts.ProjectID = id
50+
opts.Scope = "p"
51+
} else if opts.ProjectID != 0 {
52+
opts.Scope = "p"
53+
} else {
54+
opts.Scope = "g"
55+
}
56+
3657
updateflags := api.ListFlags{
37-
Scope: opts.Scope,
58+
Scope: opts.Scope,
59+
ProjectID: opts.ProjectID,
3860
}
3961

4062
if len(args) > 0 {
41-
labelId, err = api.GetLabelIdByName(args[0])
63+
labelId, err = api.GetLabelIdByName(args[0], updateflags)
4264
} else {
43-
labelList, err := api.ListLabel(updateflags)
44-
if err != nil {
45-
log.Errorf("failed to get label list: %v", err)
46-
return
47-
}
48-
labelId = prompt.GetLabelIdFromUser(labelList.Payload)
65+
labelId, err = prompt.GetLabelIdFromUser(updateflags)
4966
}
5067
if err != nil {
51-
log.Errorf("failed to parse label id: %v", err)
68+
return fmt.Errorf("failed to parse label id: %v", err)
5269
}
5370

5471
existingLabel := api.GetLabel(labelId)
5572
if existingLabel == nil {
56-
log.Errorf("label is not found")
57-
return
73+
return fmt.Errorf("label is not found")
5874
}
5975
updateView := &models.Label{
6076
Name: existingLabel.Name,
@@ -80,14 +96,17 @@ func UpdateLableCommand() *cobra.Command {
8096
update.UpdateLabelView(updateView)
8197
err = api.UpdateLabel(updateView, labelId)
8298
if err != nil {
83-
log.Errorf("failed to update label: %v", err)
99+
return fmt.Errorf("failed to update label: %v", err)
84100
}
101+
return nil
85102
},
86103
}
87104
flags := cmd.Flags()
88105
flags.StringVarP(&opts.Name, "name", "n", "", "Name of the label")
89106
flags.StringVarP(&opts.Color, "color", "", "", "Color of the label.color is in hex value")
90-
flags.StringVarP(&opts.Scope, "scope", "s", "g", "Scope of the label. eg- g(global), p(specific project)")
107+
flags.StringVarP(&projectName, "project", "p", "", "project name when query project labels")
108+
flags.BoolVarP(&isGlobal, "global", "", false, "whether to list global or project scope labels. (default scope is global)")
109+
flags.Int64VarP(&opts.ProjectID, "project-id", "i", 0, "project ID when query project labels")
91110
flags.StringVarP(&opts.Description, "description", "d", "", "Description of the label")
92111

93112
return cmd

0 commit comments

Comments
 (0)