-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
45 lines (37 loc) · 901 Bytes
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package command
import (
"fmt"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
)
// NewSearchCommand initializes a CLI command to search Submissions.
func NewSearchCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "search",
Short: "Search for submissions by title",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
submissions, err := submissionService.Search(args[0])
if err != nil {
cobra.CheckErr(err)
}
writer := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
for _, submission := range submissions {
fmt.Fprintf(
writer,
"%s\t%s\t%d x %d\t%s\n",
submission.Subreddit.Name,
submission.PostID,
submission.ImageWidthPx,
submission.ImageHeightPx,
submission.Title,
)
}
writer.Flush()
fmt.Println()
fmt.Println(len(submissions), "submission(s) found")
},
}
return cmd
}