5
5
package cli
6
6
7
7
import (
8
- "fmt"
9
- "os"
10
- "text/tabwriter"
8
+ "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
11
9
12
- log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
13
-
14
- "github.com/scaleway/scaleway-cli/pkg/api"
15
- "github.com/scaleway/scaleway-cli/pkg/utils"
10
+ "github.com/scaleway/scaleway-cli/pkg/commands"
16
11
)
17
12
18
13
var cmdSearch = & Command {
@@ -31,71 +26,21 @@ func init() {
31
26
var searchNoTrunc bool // --no-trunc flag
32
27
var searchHelp bool // -h, --help flag
33
28
34
- func runSearch (cmd * Command , args []string ) {
29
+ func runSearch (cmd * Command , rawArgs []string ) {
35
30
if searchHelp {
36
31
cmd .PrintUsage ()
37
32
}
38
- if len (args ) != 1 {
33
+ if len (rawArgs ) != 1 {
39
34
cmd .PrintShortUsage ()
40
35
}
41
36
42
- w := tabwriter .NewWriter (os .Stdout , 10 , 1 , 3 , ' ' , 0 )
43
- defer w .Flush ()
44
- fmt .Fprintf (w , "NAME\t DESCRIPTION\t STARS\t OFFICIAL\t AUTOMATED\n " )
45
-
46
- var entries = []api.ScalewayImageInterface {}
47
-
48
- images , err := cmd .API .GetImages ()
49
- if err != nil {
50
- log .Fatalf ("unable to fetch images from the Scaleway API: %v" , err )
37
+ args := commands.SearchArgs {
38
+ Term : rawArgs [0 ],
39
+ NoTrunc : searchNoTrunc ,
51
40
}
52
- for _ , val := range * images {
53
- entries = append (entries , api.ScalewayImageInterface {
54
- Type : "image" ,
55
- Name : val .Name ,
56
- Public : val .Public ,
57
- })
58
- }
59
-
60
- snapshots , err := cmd .API .GetSnapshots ()
41
+ ctx := cmd .GetContext (rawArgs )
42
+ err := commands .RunSearch (ctx , args )
61
43
if err != nil {
62
- log .Fatalf ("unable to fetch snapshots from the Scaleway API: %v" , err )
63
- }
64
- for _ , val := range * snapshots {
65
- entries = append (entries , api.ScalewayImageInterface {
66
- Type : "snapshot" ,
67
- Name : val .Name ,
68
- Public : false ,
69
- })
70
- }
71
-
72
- for _ , image := range entries {
73
- // name field
74
- name := utils .TruncIf (utils .Wordify (image .Name ), 45 , ! searchNoTrunc )
75
-
76
- // description field
77
- var description string
78
- switch image .Type {
79
- case "image" :
80
- if image .Public {
81
- description = "public image"
82
- } else {
83
- description = "user image"
84
- }
85
-
86
- case "snapshot" :
87
- description = "user snapshot"
88
- }
89
- description = utils .TruncIf (utils .Wordify (description ), 45 , ! searchNoTrunc )
90
-
91
- // official field
92
- var official string
93
- if image .Public {
94
- official = "[OK]"
95
- } else {
96
- official = ""
97
- }
98
-
99
- fmt .Fprintf (w , "%s\t %s\t %d\t %s\t %s\n " , name , description , 0 , official , "" )
44
+ logrus .Fatalf ("Cannot execute 'search': %v" , err )
100
45
}
101
46
}
0 commit comments