6
6
"io"
7
7
"net/http"
8
8
"os"
9
+ "regexp"
9
10
"slices"
10
11
"sort"
11
12
"strings"
@@ -729,12 +730,20 @@ type cmdProfileList struct {
729
730
// Command returns a cobra.Command for use with (*cobra.Command).AddCommand.
730
731
func (c * cmdProfileList ) Command () * cobra.Command {
731
732
cmd := & cobra.Command {}
732
- cmd .Use = usage ("list" , i18n .G ("[<remote>:]" ))
733
+ cmd .Use = usage ("list" , i18n .G ("[<remote>:] [<filter>...] " ))
733
734
cmd .Aliases = []string {"ls" }
734
735
cmd .Short = i18n .G ("List profiles" )
735
736
cmd .Long = cli .FormatSection (i18n .G ("Description" ), i18n .G (
736
737
`List profiles
737
738
739
+ Filters may be of the <key>=<value> form for property based filtering,
740
+ or part of the profile name. Filters must be delimited by a ','.
741
+
742
+ Examples:
743
+ - "foo" lists all profiles that start with the name foo
744
+ - "name=foo" lists all profiles that exactly have the name foo
745
+ - "description=.*bar.*" lists all profiles with a description that contains "bar"
746
+
738
747
The -c option takes a (optionally comma-separated) list of arguments
739
748
that control which image attributes to output when displaying in table
740
749
or csv format.
@@ -826,7 +835,7 @@ func (c *cmdProfileList) usedByColumnData(profile api.Profile) string {
826
835
// Run runs the actual command logic.
827
836
func (c * cmdProfileList ) Run (cmd * cobra.Command , args []string ) error {
828
837
// Quick checks.
829
- exit , err := c .global .checkArgs (cmd , args , 0 , 1 )
838
+ exit , err := c .global .checkArgs (cmd , args , 0 , - 1 )
830
839
if exit {
831
840
return err
832
841
}
@@ -835,10 +844,16 @@ func (c *cmdProfileList) Run(cmd *cobra.Command, args []string) error {
835
844
return errors .New (i18n .G ("Can't specify --project with --all-projects" ))
836
845
}
837
846
838
- // Parse remote
847
+ // Parse remote and filters.
839
848
remote := ""
840
- if len (args ) > 0 {
841
- remote = args [0 ]
849
+ filters := []string {}
850
+
851
+ if len (args ) != 0 {
852
+ filters = args
853
+ if strings .Contains (args [0 ], ":" ) && ! strings .Contains (args [0 ], "=" ) {
854
+ remote = args [0 ]
855
+ filters = args [1 :]
856
+ }
842
857
}
843
858
844
859
resources , err := c .global .parseServers (remote )
@@ -848,18 +863,29 @@ func (c *cmdProfileList) Run(cmd *cobra.Command, args []string) error {
848
863
849
864
resource := resources [0 ]
850
865
866
+ flattenedFilters := []string {}
867
+ for _ , filter := range filters {
868
+ flattenedFilters = append (flattenedFilters , strings .Split (filter , "," )... )
869
+ }
870
+
871
+ filters = flattenedFilters
872
+
873
+ if len (filters ) > 0 && ! strings .Contains (filters [0 ], "=" ) {
874
+ filters [0 ] = fmt .Sprintf ("name=^%s($|.*)" , regexp .QuoteMeta (filters [0 ]))
875
+ }
876
+
877
+ serverFilters , _ := getServerSupportedFilters (filters , []string {}, false )
878
+
851
879
// List profiles
852
880
var profiles []api.Profile
853
881
if c .flagAllProjects {
854
- profiles , err = resource .server .GetProfilesAllProjects ()
855
- if err != nil {
856
- return err
857
- }
882
+ profiles , err = resource .server .GetProfilesAllProjectsWithFilter (serverFilters )
858
883
} else {
859
- profiles , err = resource .server .GetProfiles ()
860
- if err != nil {
861
- return err
862
- }
884
+ profiles , err = resource .server .GetProfilesWithFilter (serverFilters )
885
+ }
886
+
887
+ if err != nil {
888
+ return err
863
889
}
864
890
865
891
columns , err := c .parseColumns ()
0 commit comments