Skip to content

Commit 858c518

Browse files
committed
Identifiers can be prefixed with the type of the resource
1 parent f040713 commit 858c518

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,16 @@ Options:
307307

308308
Examples:
309309

310+
$ scw inspect my-server
311+
$ scw inspect server:my-server
310312
$ scw inspect a-public-image
313+
$ scw inspect image:a-public-image
311314
$ scw inspect my-snapshot
312-
$ scw inspect my-image
313-
$ scw inspect my-server
315+
$ scw inspect snapshot:my-snapshot
314316
$ scw inspect my-volume
317+
$ scw inspect volume:my-volume
318+
$ scw inspect my-image
319+
$ scw inspect image:my-image
315320
$ scw inspect my-server | jq '.[0].public_ip.address'
316321
$ scw inspect $(scw inspect my-image | jq '.[0].root_volume.id')
317322
$ scw inspect -f "{{ .PublicAddress.IP }}" my-server
@@ -955,6 +960,8 @@ Same as Hack, without step 5
955960
* `scw {create,run}`, prefixing root-volume with the server hostname ([#63](https://github.com/scaleway/scaleway-cli/issues/63))
956961
* `scw {create,run} IMAGE`, *IMAGE* can be a snapshot ([#19](https://github.com/scaleway/scaleway-cli/issues/19))
957962
* Support of `scw stop -w, --wait` option
963+
* Identifiers can be prefixed with the type of the resource, i.e: `scw inspect my-server` == `scw inspect server:my-server`
964+
It may be useful if you have the same name in a server and a volume
958965

959966
#### Fixes
960967

api/cache.go

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"strings"
1414
"sync"
1515

16-
"github.com/scaleway/scaleway-cli/utils"
17-
1816
"code.google.com/p/go-uuid/uuid"
17+
18+
"github.com/scaleway/scaleway-cli/utils"
1919
)
2020

2121
// ScalewayCache is used not to query the API to resolve full identifiers
@@ -46,8 +46,10 @@ type ScalewayCache struct {
4646
}
4747

4848
const (
49+
// IdentifierUnknown is used when we don't know explicitely the type key of the object (used for nil comparison)
50+
IdentifierUnknown = 1 << iota
4951
// IdentifierServer is the type key of cached server objects
50-
IdentifierServer = iota
52+
IdentifierServer
5153
// IdentifierImage is the type key of cached image objects
5254
IdentifierImage
5355
// IdentifierSnapshot is the type key of cached snapshot objects
@@ -286,43 +288,79 @@ func (c *ScalewayCache) LookUpServers(needle string, acceptUUID bool) []string {
286288
return utils.RemoveDuplicates(res)
287289
}
288290

291+
// parseNeedle parses a user needle and try to extract a forced object type
292+
// i.e:
293+
// - server:blah-blah -> kind=server, needle=blah-blah
294+
// - blah-blah -> kind="", needle=blah-blah
295+
// - not-existing-type:blah-blah
296+
func parseNeedle(input string) (identifierType int, needle string) {
297+
parts := strings.Split(input, ":")
298+
if len(parts) == 2 {
299+
switch parts[0] {
300+
case "server":
301+
return IdentifierServer, parts[1]
302+
case "image":
303+
return IdentifierImage, parts[1]
304+
case "snapshot":
305+
return IdentifierSnapshot, parts[1]
306+
case "bootscript":
307+
return IdentifierBootscript, parts[1]
308+
case "volume":
309+
return IdentifierVolume, parts[1]
310+
}
311+
}
312+
return IdentifierUnknown, input
313+
}
314+
289315
// LookUpIdentifiers attempts to return identifiers matching a pattern
290316
func (c *ScalewayCache) LookUpIdentifiers(needle string) []ScalewayIdentifier {
291317
results := []ScalewayIdentifier{}
292318

293-
for _, identifier := range c.LookUpServers(needle, false) {
294-
results = append(results, ScalewayIdentifier{
295-
Identifier: identifier,
296-
Type: IdentifierServer,
297-
})
319+
identifierType, needle := parseNeedle(needle)
320+
321+
if identifierType&(IdentifierUnknown|IdentifierServer) > 0 {
322+
for _, identifier := range c.LookUpServers(needle, false) {
323+
results = append(results, ScalewayIdentifier{
324+
Identifier: identifier,
325+
Type: IdentifierServer,
326+
})
327+
}
298328
}
299329

300-
for _, identifier := range c.LookUpImages(needle, false) {
301-
results = append(results, ScalewayIdentifier{
302-
Identifier: identifier,
303-
Type: IdentifierImage,
304-
})
330+
if identifierType&(IdentifierUnknown|IdentifierImage) > 0 {
331+
for _, identifier := range c.LookUpImages(needle, false) {
332+
results = append(results, ScalewayIdentifier{
333+
Identifier: identifier,
334+
Type: IdentifierImage,
335+
})
336+
}
305337
}
306338

307-
for _, identifier := range c.LookUpSnapshots(needle, false) {
308-
results = append(results, ScalewayIdentifier{
309-
Identifier: identifier,
310-
Type: IdentifierSnapshot,
311-
})
339+
if identifierType&(IdentifierUnknown|IdentifierSnapshot) > 0 {
340+
for _, identifier := range c.LookUpSnapshots(needle, false) {
341+
results = append(results, ScalewayIdentifier{
342+
Identifier: identifier,
343+
Type: IdentifierSnapshot,
344+
})
345+
}
312346
}
313347

314-
for _, identifier := range c.LookUpVolumes(needle, false) {
315-
results = append(results, ScalewayIdentifier{
316-
Identifier: identifier,
317-
Type: IdentifierVolume,
318-
})
348+
if identifierType&(IdentifierUnknown|IdentifierVolume) > 0 {
349+
for _, identifier := range c.LookUpVolumes(needle, false) {
350+
results = append(results, ScalewayIdentifier{
351+
Identifier: identifier,
352+
Type: IdentifierVolume,
353+
})
354+
}
319355
}
320356

321-
for _, identifier := range c.LookUpBootscripts(needle, false) {
322-
results = append(results, ScalewayIdentifier{
323-
Identifier: identifier,
324-
Type: IdentifierBootscript,
325-
})
357+
if identifierType&(IdentifierUnknown|IdentifierBootscript) > 0 {
358+
for _, identifier := range c.LookUpBootscripts(needle, false) {
359+
results = append(results, ScalewayIdentifier{
360+
Identifier: identifier,
361+
Type: IdentifierBootscript,
362+
})
363+
}
326364
}
327365

328366
return results

commands/inspect.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ var cmdInspect = &types.Command{
2222
Description: "Return low-level information on a server, image, snapshot, volume or bootscript",
2323
Help: "Return low-level information on a server, image, snapshot, volume or bootscript.",
2424
Examples: `
25+
$ scw inspect my-server
26+
$ scw inspect server:my-server
2527
$ scw inspect a-public-image
28+
$ scw inspect image:a-public-image
2629
$ scw inspect my-snapshot
30+
$ scw inspect snapshot:my-snapshot
2731
$ scw inspect my-volume
32+
$ scw inspect volume:my-volume
2833
$ scw inspect my-image
29-
$ scw inspect my-server
30-
$ scw inspect my-volume
34+
$ scw inspect image:my-image
3135
$ scw inspect my-server | jq '.[0].public_ip.address'
3236
$ scw inspect $(scw inspect my-image | jq '.[0].root_volume.id')
3337
$ scw inspect -f "{{ .PublicAddress.IP }}" my-server

commands/x_patch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var cmdPatch = &types.Command{
2222
Help: "PATCH an object on the API",
2323
Examples: `
2424
$ scw _patch myserver state_detail=booted
25+
$ scw _patch server:myserver state_detail=booted
2526
`,
2627
}
2728

0 commit comments

Comments
 (0)