Skip to content

Commit fd6ad5b

Browse files
committed
Refactored 'search' command (scaleway#80)
1 parent cac92ab commit fd6ad5b

File tree

2 files changed

+102
-65
lines changed

2 files changed

+102
-65
lines changed

pkg/cli/search.go

+10-65
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
package cli
66

77
import (
8-
"fmt"
9-
"os"
10-
"text/tabwriter"
8+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
119

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"
1611
)
1712

1813
var cmdSearch = &Command{
@@ -31,71 +26,21 @@ func init() {
3126
var searchNoTrunc bool // --no-trunc flag
3227
var searchHelp bool // -h, --help flag
3328

34-
func runSearch(cmd *Command, args []string) {
29+
func runSearch(cmd *Command, rawArgs []string) {
3530
if searchHelp {
3631
cmd.PrintUsage()
3732
}
38-
if len(args) != 1 {
33+
if len(rawArgs) != 1 {
3934
cmd.PrintShortUsage()
4035
}
4136

42-
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
43-
defer w.Flush()
44-
fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\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,
5140
}
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)
6143
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)
10045
}
10146
}

pkg/commands/search.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package commands
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
"text/tabwriter"
11+
12+
"github.com/scaleway/scaleway-cli/pkg/api"
13+
"github.com/scaleway/scaleway-cli/pkg/utils"
14+
"github.com/scaleway/scaleway-cli/vendor/github.com/renstrom/fuzzysearch/fuzzy"
15+
)
16+
17+
// SearchArgs are flags for the `RunSearch` function
18+
type SearchArgs struct {
19+
Term string
20+
NoTrunc bool
21+
}
22+
23+
// RunSearch is the handler for 'scw search'
24+
func RunSearch(ctx CommandContext, args SearchArgs) error {
25+
// FIXME: parallelize API calls
26+
27+
term := strings.ToLower(args.Term)
28+
w := tabwriter.NewWriter(ctx.Stdout, 10, 1, 3, ' ', 0)
29+
defer w.Flush()
30+
fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
31+
32+
var entries = []api.ScalewayImageInterface{}
33+
34+
images, err := ctx.API.GetImages()
35+
if err != nil {
36+
return fmt.Errorf("unable to fetch images from the Scaleway API: %v", err)
37+
}
38+
for _, val := range *images {
39+
if fuzzy.Match(term, strings.ToLower(val.Name)) {
40+
entries = append(entries, api.ScalewayImageInterface{
41+
Type: "image",
42+
Name: val.Name,
43+
Public: val.Public,
44+
})
45+
}
46+
}
47+
48+
snapshots, err := ctx.API.GetSnapshots()
49+
if err != nil {
50+
return fmt.Errorf("unable to fetch snapshots from the Scaleway API: %v", err)
51+
}
52+
for _, val := range *snapshots {
53+
if fuzzy.Match(term, strings.ToLower(val.Name)) {
54+
entries = append(entries, api.ScalewayImageInterface{
55+
Type: "snapshot",
56+
Name: val.Name,
57+
Public: false,
58+
})
59+
}
60+
}
61+
62+
for _, image := range entries {
63+
// name field
64+
name := utils.TruncIf(utils.Wordify(image.Name), 45, !args.NoTrunc)
65+
66+
// description field
67+
var description string
68+
switch image.Type {
69+
case "image":
70+
if image.Public {
71+
description = "public image"
72+
} else {
73+
description = "user image"
74+
}
75+
76+
case "snapshot":
77+
description = "user snapshot"
78+
}
79+
description = utils.TruncIf(utils.Wordify(description), 45, !args.NoTrunc)
80+
81+
// official field
82+
var official string
83+
if image.Public {
84+
official = "[OK]"
85+
} else {
86+
official = ""
87+
}
88+
89+
fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\n", name, description, 0, official, "")
90+
}
91+
return nil
92+
}

0 commit comments

Comments
 (0)