Skip to content

Sorting cache search by Levenshtein distance (Fix #87) #88

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 2 commits into from
Aug 3, 2015
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
* Support of `scw _flush-cache` internal command
* `scw run --gateway ...` or `SCW_GATEWAY="..." scw run ...` now creates a server without public ip address ([#74](https://github.com/scaleway/scaleway-cli/issues/74))
* `scw inspect TYPE:xxx TYPE:yyy` will only refresh cache for `TYPE`
* Sorting cache search by Levenshtein distance ([#87](https://github.com/scaleway/scaleway-cli/issues/87))

#### Fixes

Expand Down
12 changes: 7 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"net/url"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"
Expand Down Expand Up @@ -943,7 +944,7 @@ func (s *ScalewayAPI) PutVolume(volumeID string, definition ScalewayVolumePutDef
}

// ResolveServer attempts the find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveServer(needle string) ([]ScalewayResolverResult, error) {
func (s *ScalewayAPI) ResolveServer(needle string) (ScalewayResolverResults, error) {
servers := s.Cache.LookUpServers(needle, true)
if len(servers) == 0 {
_, err := s.GetServers(true, 0)
Expand All @@ -956,7 +957,7 @@ func (s *ScalewayAPI) ResolveServer(needle string) ([]ScalewayResolverResult, er
}

// ResolveSnapshot attempts the find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]ScalewayResolverResult, error) {
func (s *ScalewayAPI) ResolveSnapshot(needle string) (ScalewayResolverResults, error) {
snapshots := s.Cache.LookUpSnapshots(needle, true)
if len(snapshots) == 0 {
_, err := s.GetSnapshots()
Expand All @@ -969,7 +970,7 @@ func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]ScalewayResolverResult,
}

// ResolveImage attempts the find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveImage(needle string) ([]ScalewayResolverResult, error) {
func (s *ScalewayAPI) ResolveImage(needle string) (ScalewayResolverResults, error) {
images := s.Cache.LookUpImages(needle, true)
if len(images) == 0 {
_, err := s.GetImages()
Expand All @@ -982,7 +983,7 @@ func (s *ScalewayAPI) ResolveImage(needle string) ([]ScalewayResolverResult, err
}

// ResolveBootscript attempts the find a matching Identifier for the input string
func (s *ScalewayAPI) ResolveBootscript(needle string) ([]ScalewayResolverResult, error) {
func (s *ScalewayAPI) ResolveBootscript(needle string) (ScalewayResolverResults, error) {
bootscripts := s.Cache.LookUpBootscripts(needle, true)
if len(bootscripts) == 0 {
_, err := s.GetBootscripts()
Expand Down Expand Up @@ -1225,11 +1226,12 @@ func (s *ScalewayAPI) GetServerID(needle string) string {
return ""
}

func showResolverResults(needle string, results []ScalewayResolverResult) error {
func showResolverResults(needle string, results ScalewayResolverResults) error {
log.Errorf("Too many candidates for %s (%d)", needle, len(results))

w := tabwriter.NewWriter(os.Stderr, 20, 1, 3, ' ', 0)
defer w.Flush()
sort.Sort(results)
for _, result := range results {
fmt.Fprintf(w, "- %s\t%s\t%s\n", result.TruncIdentifier(), result.CodeName(), result.Name)
}
Expand Down
Loading