This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
205 lines (186 loc) · 4.88 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Copyright (c) 2017 Bevan Hunt. All rights reserved.
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package cmd
import (
"fmt"
"log"
"net/url"
"strconv"
"strings"
"time"
"github.com/bevanhunt/gowrex"
"github.com/dixonwille/wmenu"
"github.com/gosuri/uitable"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
"github.com/ttacon/chalk"
)
// flag value for limit
var limit int
// searchCmd represents the search command
var searchCmd = &cobra.Command{
Use: "search",
Short: "Keyword search apis.io",
Long: `Keyword search apis.io. Example: search trade`,
Run: func(cmd *cobra.Command, args []string) {
type JSONReceive struct {
Status string `json:"status"`
Data []struct {
CreatedAt *time.Time `json:"createdAt"`
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
BaseURL string `json:"baseURL"`
HumanURL string `json:"humanURL"`
Properties []struct {
Type string `json:"type"`
URL string `json:"url"`
} `json:"properties"`
Contact []struct {
FN string `json:"FN"`
Email string `json:"email"`
OrganizationName string `json:"organizationName"`
XTwitter string `json:"X-twitter"`
} `json:"contact"`
Authoritative bool `json:"authoritative"`
APIFileURL string `json:"apiFileUrl"`
Slug string `json:"slug"`
UpdatedAt *time.Time `json:"updatedAt"`
Tags []string `json:"tags,omitempty"`
} `json:"data"`
Limit int `json:" limit"`
Skip int `json:" skip"`
Paging struct {
Next string `json:"next"`
Previous string `json:"previous"`
} `json:" paging"`
}
// join all args into one keyword separated by spaces and url encoded
keywords := strings.Join(args, " ")
keywordsEscaped := url.QueryEscape(keywords)
// make API request to apis.io
req, err := gowrex.Request{
URI: "http://apis.io/api/search?q=" + keywordsEscaped + "&limit=" + strconv.Itoa(limit),
}.GetJSON()
if err != nil {
log.Println(err)
}
res, err := req.Do()
if err != nil {
log.Println(err)
}
resp := &JSONReceive{}
_, err = res.JSON(resp)
if err != nil {
log.Println(err)
}
// create results top bar
fmt.Println(chalk.Red.Color("Search: " + keywords))
fmt.Println(chalk.Yellow.Color("Results: "))
fmt.Println("")
// handle no results
if len(resp.Data) == 1 && resp.Data[0].Name == "" {
fmt.Println(chalk.Red.Color("No Results"))
return
}
// create results table
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
table.AddRow(
"#",
"Name",
"Description",
"Swagger",
)
for i, el := range resp.Data {
swagger := false
for _, elem := range el.Properties {
if elem.Type == "Swagger" {
swagger = true
}
}
table.AddRow("")
table.AddRow(
i,
el.Name,
el.Description,
swagger,
)
}
fmt.Println(table)
// site viewing menu
actFunc := func(opt wmenu.Opt) error {
selection, ok := opt.Value.(int)
if ok {
open.Run(resp.Data[selection].HumanURL)
}
return nil
}
menu := wmenu.NewMenu("Select to show site:")
menu.Action(actFunc)
for x, elem := range resp.Data {
menu.Option(elem.Name, x, false, nil)
}
menuErr := menu.Run()
if menuErr != nil {
log.Fatal(menuErr)
}
// count swagger items
swaggerCount := 0
for _, el := range resp.Data {
swagger := false
for _, elem := range el.Properties {
if elem.Type == "Swagger" {
swagger = true
}
}
if swagger == true {
swaggerCount++
}
}
// if more than one swagger item then show menu
if swaggerCount > 0 {
// swagger viewing menu
swFunc := func(opt wmenu.Opt) error {
url, ok := opt.Value.(string)
if ok {
open.Run("http://petstore.swagger.io?url=" + url)
}
return nil
}
swMenu := wmenu.NewMenu("Select to display Swagger:")
swMenu.Action(swFunc)
for _, elem := range resp.Data {
var url string
swaggered := false
for _, el := range elem.Properties {
if el.Type == "Swagger" {
url = el.URL
swaggered = true
}
}
if swaggered == true {
swMenu.Option(elem.Name, url, false, nil)
}
}
swMenuErr := swMenu.Run()
if swMenuErr != nil {
log.Fatal(swMenuErr)
}
}
},
}
func init() {
RootCmd.AddCommand(searchCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// searchCmd.PersistentFlags().String("limit", "l", "# of max results (limit)")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
searchCmd.Flags().IntVarP(&limit, "limit", "l", 10, "limit number of results - default 10")
}