Skip to content

Commit ec70597

Browse files
committed
Improved resolver behavior when matching multiple results (#47)
1 parent a941b3b commit ec70597

File tree

4 files changed

+146
-88
lines changed

4 files changed

+146
-88
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: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ func (s *ScalewayAPI) ResolveServer(needle string) ([]ScalewayResolverResult, er
927927
}
928928

929929
// ResolveSnapshot attempts the find a matching Identifier for the input string
930-
func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]string, error) {
930+
func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]ScalewayResolverResult, error) {
931931
snapshots := s.Cache.LookUpSnapshots(needle, true)
932932
if len(snapshots) == 0 {
933933
_, err := s.GetSnapshots()
@@ -940,7 +940,7 @@ func (s *ScalewayAPI) ResolveSnapshot(needle string) ([]string, error) {
940940
}
941941

942942
// ResolveImage attempts the find a matching Identifier for the input string
943-
func (s *ScalewayAPI) ResolveImage(needle string) ([]string, error) {
943+
func (s *ScalewayAPI) ResolveImage(needle string) ([]ScalewayResolverResult, error) {
944944
images := s.Cache.LookUpImages(needle, true)
945945
if len(images) == 0 {
946946
_, err := s.GetImages()
@@ -953,7 +953,7 @@ func (s *ScalewayAPI) ResolveImage(needle string) ([]string, error) {
953953
}
954954

955955
// ResolveBootscript attempts the find a matching Identifier for the input string
956-
func (s *ScalewayAPI) ResolveBootscript(needle string) ([]string, error) {
956+
func (s *ScalewayAPI) ResolveBootscript(needle string) ([]ScalewayResolverResult, error) {
957957
bootscripts := s.Cache.LookUpBootscripts(needle, true)
958958
if len(bootscripts) == 0 {
959959
_, err := s.GetBootscripts()
@@ -1217,16 +1217,13 @@ func (s *ScalewayAPI) GetSnapshotID(needle string) string {
12171217
log.Fatalf("Unable to resolve snapshot %s: %s", needle, err)
12181218
}
12191219
if len(snapshots) == 1 {
1220-
return snapshots[0]
1220+
return snapshots[0].Identifier
12211221
}
12221222
if len(snapshots) == 0 {
12231223
log.Fatalf("No such snapshot: %s", needle)
12241224
}
1225-
log.Errorf("Too many candidates for %s (%d)", needle, len(snapshots))
1226-
for _, identifier := range snapshots {
1227-
// FIXME: also print the name
1228-
log.Infof("- %s", identifier)
1229-
}
1225+
1226+
showResolverResults(needle, snapshots)
12301227
os.Exit(1)
12311228
return ""
12321229
}
@@ -1241,7 +1238,7 @@ func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
12411238
log.Fatalf("Unable to resolve image %s: %s", needle, err)
12421239
}
12431240
if len(images) == 1 {
1244-
return images[0]
1241+
return images[0].Identifier
12451242
}
12461243
if len(images) == 0 {
12471244
if exitIfMissing {
@@ -1250,11 +1247,8 @@ func (s *ScalewayAPI) GetImageID(needle string, exitIfMissing bool) string {
12501247
return ""
12511248
}
12521249
}
1253-
log.Errorf("Too many candidates for %s (%d)", needle, len(images))
1254-
for _, identifier := range images {
1255-
// FIXME: also print the name
1256-
log.Infof("- %s", identifier)
1257-
}
1250+
1251+
showResolverResults(needle, images)
12581252
os.Exit(1)
12591253
return ""
12601254
}
@@ -1269,16 +1263,13 @@ func (s *ScalewayAPI) GetBootscriptID(needle string) string {
12691263
log.Fatalf("Unable to resolve bootscript %s: %s", needle, err)
12701264
}
12711265
if len(bootscripts) == 1 {
1272-
return bootscripts[0]
1266+
return bootscripts[0].Identifier
12731267
}
12741268
if len(bootscripts) == 0 {
12751269
log.Fatalf("No such bootscript: %s", needle)
12761270
}
1277-
log.Errorf("Too many candidates for %s (%d)", needle, len(bootscripts))
1278-
for _, identifier := range bootscripts {
1279-
// FIXME: also print the name
1280-
log.Infof("- %s", identifier)
1281-
}
1271+
1272+
showResolverResults(needle, bootscripts)
12821273
os.Exit(1)
12831274
return ""
12841275
}

0 commit comments

Comments
 (0)