@@ -7,10 +7,12 @@ package commands
77import (
88 "fmt"
99 "sort"
10+ "strings"
1011 "sync"
1112 "text/tabwriter"
1213 "time"
1314
15+ "github.com/renstrom/fuzzysearch/fuzzy"
1416 "github.com/scaleway/scaleway-cli/pkg/api"
1517 "github.com/scaleway/scaleway-cli/pkg/utils"
1618 "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
@@ -22,6 +24,7 @@ type ImagesArgs struct {
2224 All bool
2325 NoTrunc bool
2426 Quiet bool
27+ Filters map [string ]string
2528}
2629
2730// RunImages is the handler for 'scw images'
@@ -30,98 +33,111 @@ func RunImages(ctx CommandContext, args ImagesArgs) error {
3033 chEntries := make (chan api.ScalewayImageInterface )
3134 var entries = []api.ScalewayImageInterface {}
3235
33- // FIXME: remove log.Fatalf in routines
36+ filterType := args . Filters [ "type" ]
3437
35- wg .Add (1 )
36- go func () {
37- defer wg .Done ()
38- images , err := ctx .API .GetImages ()
39- if err != nil {
40- logrus .Fatalf ("unable to fetch images from the Scaleway API: %v" , err )
41- }
42- for _ , val := range * images {
43- creationDate , err := time .Parse ("2006-01-02T15:04:05.000000+00:00" , val .CreationDate )
44- if err != nil {
45- logrus .Fatalf ("unable to parse creation date from the Scaleway API: %v" , err )
46- }
47- chEntries <- api.ScalewayImageInterface {
48- Type : "image" ,
49- CreationDate : creationDate ,
50- Identifier : val .Identifier ,
51- Name : val .Name ,
52- Public : val .Public ,
53- Tag : "latest" ,
54- VirtualSize : float64 (val .RootVolume .Size ),
55- }
56- }
57- }()
38+ // FIXME: remove log.Fatalf in routines
5839
59- if args . All {
40+ if filterType == "" || filterType == "image" {
6041 wg .Add (1 )
6142 go func () {
6243 defer wg .Done ()
63- snapshots , err := ctx .API .GetSnapshots ()
44+ images , err := ctx .API .GetImages ()
6445 if err != nil {
65- logrus .Fatalf ("unable to fetch snapshots from the Scaleway API: %v" , err )
46+ logrus .Fatalf ("unable to fetch images from the Scaleway API: %v" , err )
6647 }
67- for _ , val := range * snapshots {
48+ for _ , val := range * images {
6849 creationDate , err := time .Parse ("2006-01-02T15:04:05.000000+00:00" , val .CreationDate )
6950 if err != nil {
7051 logrus .Fatalf ("unable to parse creation date from the Scaleway API: %v" , err )
7152 }
7253 chEntries <- api.ScalewayImageInterface {
73- Type : "snapshot " ,
54+ Type : "image " ,
7455 CreationDate : creationDate ,
7556 Identifier : val .Identifier ,
7657 Name : val .Name ,
77- Tag : "<snapshot>" ,
78- VirtualSize : float64 (val .Size ),
79- Public : false ,
58+ Public : val .Public ,
59+ Tag : "latest" ,
60+ VirtualSize : float64 (val .RootVolume .Size ),
61+ Organization : val .Organization ,
8062 }
8163 }
8264 }()
65+ }
8366
84- wg .Add (1 )
85- go func () {
86- defer wg .Done ()
87- bootscripts , err := ctx .API .GetBootscripts ()
88- if err != nil {
89- logrus .Fatalf ("unable to fetch bootscripts from the Scaleway API: %v" , err )
90- }
91- for _ , val := range * bootscripts {
92- chEntries <- api.ScalewayImageInterface {
93- Type : "bootscript" ,
94- Identifier : val .Identifier ,
95- Name : val .Title ,
96- Tag : "<bootscript>" ,
97- Public : false ,
67+ if args .All || filterType != "" {
68+ if filterType == "" || filterType == "snapshot" {
69+ wg .Add (1 )
70+ go func () {
71+ defer wg .Done ()
72+ snapshots , err := ctx .API .GetSnapshots ()
73+ if err != nil {
74+ logrus .Fatalf ("unable to fetch snapshots from the Scaleway API: %v" , err )
9875 }
99- }
100- }()
76+ for _ , val := range * snapshots {
77+ creationDate , err := time .Parse ("2006-01-02T15:04:05.000000+00:00" , val .CreationDate )
78+ if err != nil {
79+ logrus .Fatalf ("unable to parse creation date from the Scaleway API: %v" , err )
80+ }
81+ chEntries <- api.ScalewayImageInterface {
82+ Type : "snapshot" ,
83+ CreationDate : creationDate ,
84+ Identifier : val .Identifier ,
85+ Name : val .Name ,
86+ Tag : "<snapshot>" ,
87+ VirtualSize : float64 (val .Size ),
88+ Public : false ,
89+ Organization : val .Organization ,
90+ }
91+ }
92+ }()
93+ }
10194
102- wg .Add (1 )
103- go func () {
104- defer wg .Done ()
105- volumes , err := ctx .API .GetVolumes ()
106- if err != nil {
107- logrus .Fatalf ("unable to fetch volumes from the Scaleway API: %v" , err )
108- }
109- for _ , val := range * volumes {
110- creationDate , err := time .Parse ("2006-01-02T15:04:05.000000+00:00" , val .CreationDate )
95+ if filterType == "" || filterType == "bootscript" {
96+ wg .Add (1 )
97+ go func () {
98+ defer wg .Done ()
99+ bootscripts , err := ctx .API .GetBootscripts ()
111100 if err != nil {
112- logrus .Fatalf ("unable to parse creation date from the Scaleway API: %v" , err )
101+ logrus .Fatalf ("unable to fetch bootscripts from the Scaleway API: %v" , err )
113102 }
114- chEntries <- api. ScalewayImageInterface {
115- Type : "volume" ,
116- CreationDate : creationDate ,
117- Identifier : val .Identifier ,
118- Name : val .Name ,
119- Tag : "<volume >" ,
120- VirtualSize : float64 ( val . Size ) ,
121- Public : false ,
103+ for _ , val := range * bootscripts {
104+ chEntries <- api. ScalewayImageInterface {
105+ Type : "bootscript" ,
106+ Identifier : val .Identifier ,
107+ Name : val .Title ,
108+ Tag : "<bootscript >" ,
109+ Public : false ,
110+ }
122111 }
123- }
124- }()
112+ }()
113+ }
114+
115+ if filterType == "" || filterType == "volume" {
116+ wg .Add (1 )
117+ go func () {
118+ defer wg .Done ()
119+ volumes , err := ctx .API .GetVolumes ()
120+ if err != nil {
121+ logrus .Fatalf ("unable to fetch volumes from the Scaleway API: %v" , err )
122+ }
123+ for _ , val := range * volumes {
124+ creationDate , err := time .Parse ("2006-01-02T15:04:05.000000+00:00" , val .CreationDate )
125+ if err != nil {
126+ logrus .Fatalf ("unable to parse creation date from the Scaleway API: %v" , err )
127+ }
128+ chEntries <- api.ScalewayImageInterface {
129+ Type : "volume" ,
130+ CreationDate : creationDate ,
131+ Identifier : val .Identifier ,
132+ Name : val .Name ,
133+ Tag : "<volume>" ,
134+ VirtualSize : float64 (val .Size ),
135+ Public : false ,
136+ Organization : val .Organization ,
137+ }
138+ }
139+ }()
140+ }
125141 }
126142
127143 go func () {
@@ -144,13 +160,52 @@ func RunImages(ctx CommandContext, args ImagesArgs) error {
144160 }
145161 }
146162
163+ for key , value := range args .Filters {
164+ switch key {
165+ case "organization" , "type" , "name" , "public" :
166+ continue
167+ default :
168+ logrus .Warnf ("Unknown filter: '%s=%s'" , key , value )
169+ }
170+ }
171+
147172 w := tabwriter .NewWriter (ctx .Stdout , 20 , 1 , 3 , ' ' , 0 )
148173 defer w .Flush ()
149174 if ! args .Quiet {
150175 fmt .Fprintf (w , "REPOSITORY\t TAG\t IMAGE ID\t CREATED\t VIRTUAL SIZE\n " )
151176 }
152177 sort .Sort (api .ByCreationDate (entries ))
153178 for _ , image := range entries {
179+
180+ for key , value := range args .Filters {
181+ switch key {
182+ case "type" :
183+ if value != image .Type {
184+ goto skipimage
185+ }
186+ case "organization" :
187+ switch value {
188+ case "me" :
189+ value = ctx .API .Organization
190+ case "official-distribs" :
191+ value = "a283af0b-d13e-42e1-a43f-855ffbf281ab"
192+ case "official-apps" :
193+ value = "c3884e19-7a3e-4b69-9db8-50e7f902aafc"
194+ }
195+ if image .Organization != value {
196+ goto skipimage
197+ }
198+ case "name" :
199+ if fuzzy .RankMatch (strings .ToLower (value ), strings .ToLower (image .Name )) == - 1 {
200+ goto skipimage
201+ }
202+ case "public" :
203+ if (value == "true" && ! image .Public ) || (value == "false" && image .Public ) {
204+ goto skipimage
205+ }
206+ }
207+ }
208+
154209 if args .Quiet {
155210 fmt .Fprintf (ctx .Stdout , "%s\n " , image .Identifier )
156211 } else {
@@ -174,6 +229,9 @@ func RunImages(ctx CommandContext, args ImagesArgs) error {
174229 }
175230 fmt .Fprintf (w , "%s\t %s\t %s\t %s\t %s\n " , shortName , tag , shortID , creationDate , virtualSize )
176231 }
232+
233+ skipimage:
234+ continue
177235 }
178236 return nil
179237}
0 commit comments