Skip to content

Commit

Permalink
Implement search plugin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
syossan27 committed Feb 2, 2018
1 parent 8a63937 commit 66f7b84
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 15 deletions.
79 changes: 71 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@

[[constraint]]
name = "github.com/urfave/cli"

[[constraint]]
name = "gopkg.in/resty.v1"
version = "1.2.0"

[[constraint]]
name = "github.com/koron/go-dproxy"
version = "1.1.0"

[[constraint]]
name = "github.com/kyokomi/emoji"
version = "1.5.0"
20 changes: 20 additions & 0 deletions cmd_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"errors"

"github.com/urfave/cli"
)

func cmdSearch(c *cli.Context) error {
pluginName := c.Args().First()
if pluginName == "" {
return errors.New("plugin name required")
}

if err := searchPlugin(pluginName); err != nil {
fatal("Error: Fail search plugin.")
}

return nil
}
57 changes: 55 additions & 2 deletions kirimori.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ import (
"regexp"
"strings"

"encoding/json"

"text/tabwriter"

"github.com/BurntSushi/toml"
"github.com/fatih/color"
"github.com/haya14busa/go-vimlparser"
"github.com/koron/go-dproxy"
"github.com/kyokomi/emoji"
"github.com/mattn/go-colorable"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
"gopkg.in/resty.v1"
)

const (
Expand Down Expand Up @@ -104,8 +112,8 @@ func makeApp() *cli.App {
app := cli.NewApp()

app.Name = "kirimori"
app.Usage = "Add Vim Plugin Tool"
app.Version = "0.0.3"
app.Usage = "Manage Vim Plugin Tool"
app.Version = "0.0.4"

app.Commands = []cli.Command{
{
Expand All @@ -132,6 +140,12 @@ func makeApp() *cli.App {
Usage: "list plugin",
Action: cmdList,
},
{
Name: "search",
Aliases: []string{"s"},
Usage: "search plugin in vimawesome.com",
Action: cmdSearch,
},
{
Name: "config",
Aliases: []string{"c"},
Expand Down Expand Up @@ -204,6 +218,45 @@ func updateVimrc(filename string, b []byte) error {
return err
}

func searchPlugin(pluginName string) error {
// Get search results
result, err := resty.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20)).
R().
SetQueryParams(map[string]string{
"query": pluginName,
"page": "1",
}).
Get("http://vimawesome.com/api/plugins")

// Parse search results for print
var resultInterface interface{}
err = json.Unmarshal(result.Body(), &resultInterface)
proxy := dproxy.New(resultInterface)
pluginsInterface, err := proxy.M("plugins").Array()
totalResults, err := proxy.M("total_results").Int64()

// Set PrintColor
green := color.New(color.FgGreen).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()

// Print search results
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 0, 8, ' ', 0)
fmt.Printf("Total: %d\n\n", totalResults)
fmt.Fprintf(w, "%s\t%s\n", red("Plugin Name"), red("Short Description"))
for _, pluginInterface := range pluginsInterface {
proxy = dproxy.New(pluginInterface)
githubURL, _ := proxy.M("github_url").String()
pluginName := strings.Replace(githubURL, "https://github.com/", "", 1)
shortDesc, _ := proxy.M("short_desc").String()
fmt.Fprintf(w, "%s\t%s\n", green(pluginName), yellow(emoji.Sprint(shortDesc)))
}
w.Flush()

return err
}

func config() *Config {
var conf Config
if _, err := toml.DecodeFile(settingFilePath, &conf); err != nil {
Expand Down
18 changes: 13 additions & 5 deletions kirimori_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"testing"
)

func TestPlugins(t *testing.T) {
for _, pm := range pluginManagers {
testAddPlugin(t, pm.Name, pm.Key)
testListPlugin(t, pm.Name, pm.Key)
testRemovePlugin(t, pm.Name, pm.Key)
testSearchPlugin(t)
}
}

func testAddPlugin(t *testing.T, name, key string) {
settingFilePath = filepath.Join("testdir", key+".toml")
conf := config()
Expand Down Expand Up @@ -103,10 +112,9 @@ func testRemovePlugin(t *testing.T, name, key string) {
}
}

func TestPlugins(t *testing.T) {
for _, pm := range pluginManagers {
testAddPlugin(t, pm.Name, pm.Key)
testListPlugin(t, pm.Name, pm.Key)
testRemovePlugin(t, pm.Name, pm.Key)
func testSearchPlugin(t *testing.T) {
err := searchPlugin("emmet-vim")
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 66f7b84

Please sign in to comment.