@@ -25,8 +25,15 @@ import (
2525)
2626
2727func 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}
0 commit comments