Skip to content

Commit

Permalink
Initial version of the search command
Browse files Browse the repository at this point in the history
  • Loading branch information
pepegar committed Jul 29, 2014
1 parent 9ba4bab commit 77e4078
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
46 changes: 46 additions & 0 deletions commands/json_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package commands

import (
"encoding/json"
"io/ioutil"
"net/http"
)

type Response struct {
TotalResults int `json:"total_results"`
ResultsPerPage int `json:"results_per_page"`
TotalPages int `json:"total_pages"`
Plugins []PluginRecord
}

type PluginRecord struct {
Author string
Slug string
ShortDesc string `json:"short_desc"`
GithubUrl string `json:"github_url"`
}

func GetJson(url string) ([]byte, error) {
resp, requestError := http.Get(url)

if requestError != nil {
return nil, requestError
}

defer resp.Body.Close()
body, readError := ioutil.ReadAll(resp.Body)

if readError != nil {
return nil, readError
}

return body, nil
}

func ParsePluginsList(body []byte) (Response, error) {
var r Response

json.Unmarshal(body, &r)

return r, nil
}
26 changes: 25 additions & 1 deletion commands/search.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
package commands

import (
"fmt"
"os"
)

var SearchCommand = Command{
Name: "search",
Description: "Search a plugin",
Action: func() {
println("Search!")
if len(os.Args) < 3 {
fmt.Println("please, supply a keyword for the plugin you want to search")
} else {
url := "http://vimawesome.com/api/plugins?query=" + os.Args[2]
json, jsonError := GetJson(url)

if nil != jsonError {
fmt.Println("error")
}

response, parseError := ParsePluginsList(json)

if nil != parseError {
fmt.Println("error")
}

for _, plugin := range response.Plugins {
fmt.Println("* " + plugin.Slug + " - " + plugin.ShortDesc)
}
}
},
}

0 comments on commit 77e4078

Please sign in to comment.