Skip to content

Show similar library names in lib search #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cli/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (res result) Data() interface{} {

names := []LibName{}
results := res.results.GetLibraries()
for _, lsr := range results {
names = append(names, LibName{lsr.Name})
for _, lib := range results {
names = append(names, LibName{lib.Name})
}

return NamesOnly{
Expand All @@ -113,9 +113,18 @@ func (res result) String() string {

var out strings.Builder

if res.results.GetStatus() == rpc.LibrarySearchStatus_failed {
out.WriteString("No libraries matching your search.\nDid you mean...\n")
}

for _, lib := range results {
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lib.Name))
if res.namesOnly {
if res.results.GetStatus() == rpc.LibrarySearchStatus_success {
out.WriteString(fmt.Sprintf("Name: \"%s\"\n", lib.Name))
if res.namesOnly {
continue
}
} else {
out.WriteString(fmt.Sprintf("%s\n", lib.Name))
continue
}

Expand Down
26 changes: 23 additions & 3 deletions commands/lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,28 @@ import (
"strings"

"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/imjasonmiller/godice"
semver "go.bug.st/relaxed-semver"
)

var similarityThreshold = 0.7

// LibrarySearch FIXMEDOC
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.LibrarySearchResp, error) {
lm := commands.GetLibraryManager(req.GetInstance().GetId())
if lm == nil {
return nil, errors.New("invalid instance")
}

return searchLibrary(req, lm)
}

func searchLibrary(req *rpc.LibrarySearchReq, lm *librariesmanager.LibrariesManager) (*rpc.LibrarySearchResp, error) {
res := []*rpc.SearchedLibrary{}
status := rpc.LibrarySearchStatus_success

for _, lib := range lm.Index.Libraries {
qry := strings.ToLower(req.GetQuery())
Expand All @@ -46,16 +55,27 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchReq) (*rpc.Library
}
latest := GetLibraryParameters(lib.Latest)

searchedlib := &rpc.SearchedLibrary{
searchedLib := &rpc.SearchedLibrary{
Name: lib.Name,
Releases: releases,
Latest: latest,
}
res = append(res, searchedlib)
res = append(res, searchedLib)
}
}

if len(res) == 0 {
status = rpc.LibrarySearchStatus_failed
for _, lib := range lm.Index.Libraries {
if godice.CompareString(req.GetQuery(), lib.Name) > similarityThreshold {
res = append(res, &rpc.SearchedLibrary{
Name: lib.Name,
})
}
}
}

return &rpc.LibrarySearchResp{Libraries: res}, nil
return &rpc.LibrarySearchResp{Libraries: res, Status: status}, nil
}

// GetLibraryParameters FIXMEDOC
Expand Down
54 changes: 54 additions & 0 deletions commands/lib/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package lib

import (
"strings"
"testing"

"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
rpc "github.com/arduino/arduino-cli/rpc/commands"
paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
)

var customIndexPath = paths.New("testdata")

func TestSearchLibrary(t *testing.T) {
lm := librariesmanager.NewLibraryManager(customIndexPath, nil)
lm.LoadIndex()

req := &rpc.LibrarySearchReq{
Instance: &rpc.Instance{Id: 1},
Query: "test",
}

resp, err := searchLibrary(req, lm)
if err != nil {
t.Fatal(err)
}

assert := assert.New(t)
assert.Equal(resp.GetStatus(), rpc.LibrarySearchStatus_success)
assert.Equal(len(resp.GetLibraries()), 2)
assert.True(strings.Contains(resp.GetLibraries()[0].Name, "Test"))
assert.True(strings.Contains(resp.GetLibraries()[1].Name, "Test"))
}

func TestSearchLibrarySimilar(t *testing.T) {
lm := librariesmanager.NewLibraryManager(customIndexPath, nil)
lm.LoadIndex()

req := &rpc.LibrarySearchReq{
Instance: &rpc.Instance{Id: 1},
Query: "ardino",
}

resp, err := searchLibrary(req, lm)
if err != nil {
t.Fatal(err)
}

assert := assert.New(t)
assert.Equal(resp.GetStatus(), rpc.LibrarySearchStatus_failed)
assert.Equal(len(resp.GetLibraries()), 1)
assert.Equal(resp.GetLibraries()[0].Name, "Arduino")
}
13 changes: 13 additions & 0 deletions commands/lib/testdata/library_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"libraries": [
{
"name": "ArduinoTestPackage"
},
{
"name": "Arduino"
},
{
"name": "Test"
}
]
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/gofrs/uuid v3.2.0+incompatible
github.com/golang/protobuf v1.3.3
github.com/h2non/filetype v1.0.8 // indirect
github.com/imjasonmiller/godice v0.1.2
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
github.com/juju/testing v0.0.0-20190429233213-dfc56b8c09fc // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/h2non/filetype v1.0.8 h1:le8gpf+FQA0/DlDABbtisA1KiTS0Xi+YSC/E8yY3Y14=
github.com/h2non/filetype v1.0.8/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/imjasonmiller/godice v0.1.2 h1:T1/sW/HoDzFeuwzOOuQjmeMELz9CzZ53I2CnD+08zD4=
github.com/imjasonmiller/godice v0.1.2/go.mod h1:8cTkdnVI+NglU2d6sv+ilYcNaJ5VSTBwvMbFULJd/QQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down
46 changes: 40 additions & 6 deletions rpc/commands/lib.pb.go

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

6 changes: 6 additions & 0 deletions rpc/commands/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ message LibrarySearchReq {
string query = 2;
}

enum LibrarySearchStatus {
failed = 0;
success = 1;
}

message LibrarySearchResp {
repeated SearchedLibrary libraries = 1;
LibrarySearchStatus status = 2;
}

message SearchedLibrary {
Expand Down