Skip to content

Commit

Permalink
Support short UUIDs in cancel / task commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jhunt committed Aug 2, 2019
1 parent 974b430 commit 5678f6b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
6 changes: 3 additions & 3 deletions ci/release_notes.md
@@ -1,5 +1,5 @@
# Improvements

- The `restore-archive` and `purge-archive` commands in the
SHIELD CLI now properly support short UUIDs, like all other
commands.
- The `cancel`, `task`, `restore-archive` and `purge-archive`
commands in the SHIELD CLI now properly support short UUIDs,
like all other commands.
24 changes: 24 additions & 0 deletions client/v2/shield/tasks.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

qs "github.com/jhunt/go-querytron"
"github.com/pborman/uuid"
)

type Task struct {
Expand Down Expand Up @@ -57,6 +58,29 @@ func (c *Client) ListTasks(parent *Tenant, filter *TaskFilter) ([]*Task, error)
return out, nil
}

func (c *Client) FindTask(parent *Tenant, q string, fuzzy bool) (*Task, error) {
if uuid.Parse(q) != nil {
return c.GetTask(parent, q)
}

l, err := c.ListTasks(parent, &TaskFilter{
UUID: q,
Fuzzy: fuzzy,
})
if err != nil {
return nil, err
}

if len(l) == 0 {
return nil, fmt.Errorf("no matching task found")
}
if len(l) > 1 {
return nil, fmt.Errorf("multiple matching tasks found")
}

return c.GetTask(parent, l[0].UUID)
}

func (c *Client) GetTask(parent *Tenant, uuid string) (*Task, error) {
url := fmt.Sprintf("/v2/tasks/%s", uuid)
if parent != nil {
Expand Down
13 changes: 2 additions & 11 deletions cmd/shield/main.go
Expand Up @@ -2779,16 +2779,7 @@ tenants:
bail(err)
}

tasks, err := c.ListTasks(tenant, &shield.TaskFilter{UUID: args[0], Fuzzy: true})
bail(err)
if len(tasks) > 1 {
bail(fmt.Errorf("Ambiguous UUID prefix given for task"))
}
if len(tasks) == 0 {
bail(fmt.Errorf("No task found with UUID prefix `%s'", args[0]))
}

task, err := c.GetTask(tenant, tasks[0].UUID)
task, err := c.FindTask(tenant, args[0], !opts.Exact)
bail(err)

if opts.JSON {
Expand Down Expand Up @@ -2838,7 +2829,7 @@ tenants:
tenant, err := c.FindMyTenant(opts.Tenant, true)
bail(err)

task, err := c.GetTask(tenant, args[0])
task, err := c.FindTask(tenant, args[0], !opts.Exact)
bail(err)

r := tui.NewReport()
Expand Down

0 comments on commit 5678f6b

Please sign in to comment.