Skip to content

Commit 18f0000

Browse files
committed
Merge pull request #68 from scaleway/feature-cache-resolver-results
Feature cache resolver results
2 parents 6764e5c + ec70597 commit 18f0000

File tree

4 files changed

+216
-95
lines changed

4 files changed

+216
-95
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
10201020

10211021
#### Features
10221022

1023+
* Improved resolver behavior when matching multiple results, now displaying more info too help choosing candidates ([#47](https://github.com/scaleway/scaleway-cli/issues/47))
10231024
* `scw exec SERVER [COMMAND] [ARGS...]`, *COMMAND* is now optional
10241025
* Showing the server MOTD when calling `scw run <image> [COMMAND]` without *COMMAND*
10251026
* Support of `scw attach --no-stdin` option

api/api.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/url"
1414
"os"
1515
"strings"
16+
"text/tabwriter"
1617
"text/template"
1718

1819
log "github.com/Sirupsen/logrus"
@@ -913,7 +914,7 @@ func (s *ScalewayAPI) PutVolume(volumeID string, definition ScalewayVolumePutDef
913914
}
914915

915916
// ResolveServer attempts the find a matching Identifier for the input string
916-
func (s *ScalewayAPI) ResolveServer(needle string) ([]string, error) {
917+
func (s *ScalewayAPI) ResolveServer(needle string) ([]ScalewayResolverResult, error) {
917918
servers := s.Cache.LookUpServers(needle, true)
918919
if len(servers) == 0 {
919920
_, err := s.GetServers(true, 0)
@@ -926,7 +927,7 @@ func (s *ScalewayAPI) ResolveServer(needle string) ([]string, error) {
926927
}
927928

928929
// ResolveSnapshot attempts the find a matching Identifier for the input string
929-
func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]string, error) {
930+
func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]ScalewayResolverResult, error) {
930931
snapshots := s.Cache.LookUpSnapshots(needle, true)
931932
if len(snapshots) == 0 {
932933
_, err := s.GetSnapshots()
@@ -939,7 +940,7 @@ func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]string, error) {
939940
}
940941

941942
// ResolveImage attempts the find a matching Identifier for the input string
942-
func (s *ScalewayAPI) ResolveImage(needle string) ([]string, error) {
943+
func (s *ScalewayAPI) ResolveImage(needle string) ([]ScalewayResolverResult, error) {
943944
images := s.Cache.LookUpImages(needle, true)
944945
if len(images) == 0 {
945946
_, err := s.GetImages()
@@ -952,7 +953,7 @@ func (s *ScalewayAPI) ResolveImage(needle string) ([]string, error) {
952953
}
953954

954955
// ResolveBootscript attempts the find a matching Identifier for the input string
955-
func (s *ScalewayAPI) ResolveBootscript(needle string) ([]string, error) {
956+
func (s *ScalewayAPI) ResolveBootscript(needle string) ([]ScalewayResolverResult, error) {
956957
bootscripts := s.Cache.LookUpBootscripts(needle, true)
957958
if len(bootscripts) == 0 {
958959
_, err := s.GetBootscripts()
@@ -1184,20 +1185,28 @@ func (s *ScalewayAPI) GetServerID(needle string) string {
11841185
log.Fatalf("Unable to resolve server %s: %s", needle, err)
11851186
}
11861187
if len(servers) == 1 {
1187-
return servers[0]
1188+
return servers[0].Identifier
11881189
}
11891190
if len(servers) == 0 {
11901191
log.Fatalf("No such server: %s", needle)
11911192
}
1192-
log.Errorf("Too many candidates for %s (%d)", needle, len(servers))
1193-
for _, identifier := range servers {
1194-
// FIXME: also print the name
1195-
log.Infof("- %s", identifier)
1196-
}
1193+
1194+
showResolverResults(needle, servers)
11971195
os.Exit(1)
11981196
return ""
11991197
}
12001198

1199+
func showResolverResults(needle string, results []ScalewayResolverResult) error {
1200+
log.Errorf("Too many candidates for %s (%d)", needle, len(results))
1201+
1202+
w := tabwriter.NewWriter(os.Stderr, 20, 1, 3, ' ', 0)
1203+
defer w.Flush()
1204+
for _, result := range results {
1205+
fmt.Fprintf(w, "- %s\t%s\t%s\n", result.TruncIdentifier(), result.CodeName(), result.Name)
1206+
}
1207+
return nil
1208+
}
1209+
12011210
// GetSnapshotID returns exactly one snapshot matching or dies
12021211
func (s *ScalewayAPI) GetSnapshotID(needle string) string {
12031212
// Parses optional type prefix, i.e: "snapshot:name" -> "name"
@@ -1208,16 +1217,13 @@ func (s *ScalewayAPI) GetSnapshotID(needle string) string {
12081217
log.Fatalf("Unable to resolve snapshot %s: %s", needle, err)
12091218
}
12101219
if len(snapshots) == 1 {
1211-
return snapshots[0]
1220+
return snapshots[0].Identifier
12121221
}
12131222
if len(snapshots) == 0 {
12141223
log.Fatalf("No such snapshot: %s", needle)
12151224
}
1216-
log.Errorf("Too many candidates for %s (%d)", needle, len(snapshots))
1217-
for _, identifier := range snapshots {
1218-
// FIXME: also print the name
1219-
log.Infof("- %s", identifier)
1220-
}
1225+
1226+
showResolverResults(needle, snapshots)
12211227
os.Exit(1)
12221228
return ""
12231229
}
@@ -1232,7 +1238,7 @@ func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
12321238
log.Fatalf("Unable to resolve image %s: %s", needle, err)
12331239
}
12341240
if len(images) == 1 {
1235-
return images[0]
1241+
return images[0].Identifier
12361242
}
12371243
if len(images) == 0 {
12381244
if exitIfMissing {
@@ -1241,11 +1247,8 @@ func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
12411247
return ""
12421248
}
12431249
}
1244-
log.Errorf("Too many candidates for %s (%d)", needle, len(images))
1245-
for _, identifier := range images {
1246-
// FIXME: also print the name
1247-
log.Infof("- %s", identifier)
1248-
}
1250+
1251+
showResolverResults(needle, images)
12491252
os.Exit(1)
12501253
return ""
12511254
}
@@ -1260,16 +1263,13 @@ func (s *ScalewayAPI) GetBootscriptID(needle string) string {
12601263
log.Fatalf("Unable to resolve bootscript %s: %s", needle, err)
12611264
}
12621265
if len(bootscripts) == 1 {
1263-
return bootscripts[0]
1266+
return bootscripts[0].Identifier
12641267
}
12651268
if len(bootscripts) == 0 {
12661269
log.Fatalf("No such bootscript: %s", needle)
12671270
}
1268-
log.Errorf("Too many candidates for %s (%d)", needle, len(bootscripts))
1269-
for _, identifier := range bootscripts {
1270-
// FIXME: also print the name
1271-
log.Infof("- %s", identifier)
1272-
}
1271+
1272+
showResolverResults(needle, bootscripts)
12731273
os.Exit(1)
12741274
return ""
12751275
}

0 commit comments

Comments
 (0)