@@ -7,10 +7,12 @@ package commands
7
7
import (
8
8
"fmt"
9
9
"sort"
10
+ "strings"
10
11
"sync"
11
12
"text/tabwriter"
12
13
"time"
13
14
15
+ "github.com/renstrom/fuzzysearch/fuzzy"
14
16
"github.com/scaleway/scaleway-cli/pkg/api"
15
17
"github.com/scaleway/scaleway-cli/pkg/utils"
16
18
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
@@ -22,6 +24,7 @@ type ImagesArgs struct {
22
24
All bool
23
25
NoTrunc bool
24
26
Quiet bool
27
+ Filters map [string ]string
25
28
}
26
29
27
30
// RunImages is the handler for 'scw images'
@@ -30,98 +33,111 @@ func RunImages(ctx CommandContext, args ImagesArgs) error {
30
33
chEntries := make (chan api.ScalewayImageInterface )
31
34
var entries = []api.ScalewayImageInterface {}
32
35
33
- // FIXME: remove log.Fatalf in routines
36
+ filterType := args . Filters [ "type" ]
34
37
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
58
39
59
- if args . All {
40
+ if filterType == "" || filterType == "image" {
60
41
wg .Add (1 )
61
42
go func () {
62
43
defer wg .Done ()
63
- snapshots , err := ctx .API .GetSnapshots ()
44
+ images , err := ctx .API .GetImages ()
64
45
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 )
66
47
}
67
- for _ , val := range * snapshots {
48
+ for _ , val := range * images {
68
49
creationDate , err := time .Parse ("2006-01-02T15:04:05.000000+00:00" , val .CreationDate )
69
50
if err != nil {
70
51
logrus .Fatalf ("unable to parse creation date from the Scaleway API: %v" , err )
71
52
}
72
53
chEntries <- api.ScalewayImageInterface {
73
- Type : "snapshot " ,
54
+ Type : "image " ,
74
55
CreationDate : creationDate ,
75
56
Identifier : val .Identifier ,
76
57
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 ,
80
62
}
81
63
}
82
64
}()
65
+ }
83
66
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 )
98
75
}
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
+ }
101
94
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 ()
111
100
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 )
113
102
}
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
+ }
122
111
}
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
+ }
125
141
}
126
142
127
143
go func () {
@@ -144,13 +160,52 @@ func RunImages(ctx CommandContext, args ImagesArgs) error {
144
160
}
145
161
}
146
162
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
+
147
172
w := tabwriter .NewWriter (ctx .Stdout , 20 , 1 , 3 , ' ' , 0 )
148
173
defer w .Flush ()
149
174
if ! args .Quiet {
150
175
fmt .Fprintf (w , "REPOSITORY\t TAG\t IMAGE ID\t CREATED\t VIRTUAL SIZE\n " )
151
176
}
152
177
sort .Sort (api .ByCreationDate (entries ))
153
178
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
+
154
209
if args .Quiet {
155
210
fmt .Fprintf (ctx .Stdout , "%s\n " , image .Identifier )
156
211
} else {
@@ -174,6 +229,9 @@ func RunImages(ctx CommandContext, args ImagesArgs) error {
174
229
}
175
230
fmt .Fprintf (w , "%s\t %s\t %s\t %s\t %s\n " , shortName , tag , shortID , creationDate , virtualSize )
176
231
}
232
+
233
+ skipimage:
234
+ continue
177
235
}
178
236
return nil
179
237
}
0 commit comments