Skip to content

Fix 353 #354

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 3 commits into from
Apr 25, 2016
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 @@ -1184,6 +1184,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

* `scw inspect` fix panic ([#353](https://github.com/scaleway/scaleway-cli/issues/353)
* Clear cache between the releases ([#329](https://github.com/scaleway/scaleway-cli/issues/329)
* Fix `scw _patch bootscript` nil dereference
* Fix `scw images` bad error message ([#336](https://github.com/scaleway/scaleway-cli/issues/337))
Expand Down
8 changes: 3 additions & 5 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1981,16 +1981,14 @@ func (s *ScalewayAPI) GetServerID(needle string) (string, error) {
}

func showResolverResults(needle string, results ScalewayResolverResults) error {
arch := ""
w := tabwriter.NewWriter(os.Stderr, 20, 1, 3, ' ', 0)
defer w.Flush()
sort.Sort(results)
for _, result := range results {
arch = result.Arch
if arch == "" {
arch = "n/a"
if result.Arch == "" {
result.Arch = "n/a"
}
fmt.Fprintf(w, "- %s\t%s\t%s\t%s\n", result.TruncIdentifier(), result.CodeName(), result.Name, arch)
fmt.Fprintf(w, "- %s\t%s\t%s\t%s\n", result.TruncIdentifier(), result.CodeName(), result.Name, result.Arch)
}
return fmt.Errorf("Too many candidates for %s (%d)", needle, len(results))
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/api/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -93,6 +94,9 @@ type ScalewayResolverResults []ScalewayResolverResult

// NewScalewayResolverResult returns a new ScalewayResolverResult
func NewScalewayResolverResult(Identifier, Name, Arch string, Type int) ScalewayResolverResult {
if err := anonuuid.IsUUID(Identifier); err != nil {
log.Fatal(err)
}
return ScalewayResolverResult{
Identifier: Identifier,
Type: Type,
Expand Down Expand Up @@ -356,12 +360,12 @@ func (c *ScalewayCache) LookUpVolumes(needle string, acceptUUID bool) ScalewayRe
nameRegex := regexp.MustCompile(`(?i)` + regexp.MustCompile(`[_-]`).ReplaceAllString(needle, ".*"))
for identifier, fields := range c.Volumes {
if fields[CacheTitle] == needle {
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
entry.ComputeRankMatch(needle)
exactMatches = append(exactMatches, entry)
}
if strings.HasPrefix(identifier, needle) || nameRegex.MatchString(fields[CacheTitle]) {
entry := NewScalewayResolverResult(needle, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
entry := NewScalewayResolverResult(identifier, fields[CacheTitle], fields[CacheArch], IdentifierVolume)
entry.ComputeRankMatch(needle)
res = append(res, entry)
}
Expand Down
58 changes: 19 additions & 39 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,45 +236,25 @@ func InspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj
ident := idents.Identifiers[0]
wg.Add(1)
go func() {
if ident.Type == IdentifierServer {
server, err := api.GetServer(ident.Identifier)
if err == nil {
cj <- InspectIdentifierResult{
Type: ident.Type,
Object: server,
}
}
} else if ident.Type == IdentifierImage {
image, err := api.GetImage(ident.Identifier)
if err == nil {
cj <- InspectIdentifierResult{
Type: ident.Type,
Object: image,
}
}
} else if ident.Type == IdentifierSnapshot {
snap, err := api.GetSnapshot(ident.Identifier)
if err == nil {
cj <- InspectIdentifierResult{
Type: ident.Type,
Object: snap,
}
}
} else if ident.Type == IdentifierVolume {
volume, err := api.GetVolume(ident.Identifier)
if err == nil {
cj <- InspectIdentifierResult{
Type: ident.Type,
Object: volume,
}
}
} else if ident.Type == IdentifierBootscript {
bootscript, err := api.GetBootscript(ident.Identifier)
if err == nil {
cj <- InspectIdentifierResult{
Type: ident.Type,
Object: bootscript,
}
var obj interface{}
var err error

switch ident.Type {
case IdentifierServer:
obj, err = api.GetServer(ident.Identifier)
case IdentifierImage:
obj, err = api.GetImage(ident.Identifier)
case IdentifierSnapshot:
obj, err = api.GetSnapshot(ident.Identifier)
case IdentifierVolume:
obj, err = api.GetVolume(ident.Identifier)
case IdentifierBootscript:
obj, err = api.GetBootscript(ident.Identifier)
}
if err == nil && obj != nil {
cj <- InspectIdentifierResult{
Type: ident.Type,
Object: obj,
}
}
wg.Done()
Expand Down
4 changes: 3 additions & 1 deletion pkg/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func RunInspect(ctx CommandContext, args InspectArgs) error {
if ctx.Getenv("SCW_SENSITIVE") != "1" {
res = ctx.API.HideAPICredentials(res)
}
fmt.Fprintln(ctx.Stdout, res)
if len(res) > 2 {
fmt.Fprintln(ctx.Stdout, res)
}
}
}

Expand Down