From 3f8f556a44a6adbeb7481976f0d5743c4e80563f Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 29 Sep 2021 15:15:31 +0200 Subject: [PATCH] cmd/pool: add sidecar list and cancel commands --- cmd/pool/sidecar.go | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/cmd/pool/sidecar.go b/cmd/pool/sidecar.go index 477fd6a5b..19a27b5d1 100644 --- a/cmd/pool/sidecar.go +++ b/cmd/pool/sidecar.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/hex" "fmt" "strconv" @@ -23,6 +24,8 @@ var sidecarCommands = []cli.Command{ sidecarPrintTicketCommand, sidecarRegisterCommand, sidecarExpectChannelCommand, + sidecarListCommand, + sidecarCancelCommand, }, }, } @@ -291,3 +294,80 @@ func sidecarExpectChannel(ctx *cli.Context) error { return nil } + +var sidecarListCommand = cli.Command{ + Name: "list", + Aliases: []string{"l"}, + Usage: "list all sidecar tickets", + Action: sidecarList, +} + +func sidecarList(ctx *cli.Context) error { + // Show help if no arguments or flags are provided. + if ctx.NArg() != 0 || ctx.NumFlags() != 0 { + _ = cli.ShowCommandHelp(ctx, "list") + return nil + } + + client, cleanup, err := getClient(ctx) + if err != nil { + return err + } + defer cleanup() + + resp, err := client.ListSidecars( + context.Background(), &poolrpc.ListSidecarsRequest{}, + ) + if err != nil { + return err + } + + printRespJSON(resp) + + return nil +} + +var sidecarCancelCommand = cli.Command{ + Name: "cancel", + Aliases: []string{"c"}, + Usage: "cancel the execution of a sidecar ticket identified by " + + "its ID", + ArgsUsage: "ticket_id", + Description: ` + Tries to cancel the execution of a sidecar ticket identified by its ID. + If a bid order was created for the ticket already, that order is + cancelled on the server side.`, + Action: sidecarCancel, +} + +func sidecarCancel(ctx *cli.Context) error { + // Show help if no arguments or flags are provided. + if ctx.NArg() != 1 || ctx.NumFlags() != 0 { + _ = cli.ShowCommandHelp(ctx, "cancel") + return nil + } + + client, cleanup, err := getClient(ctx) + if err != nil { + return err + } + defer cleanup() + + idBytes, err := hex.DecodeString(ctx.Args().First()) + if err != nil { + return fmt.Errorf("error decoding ticket ID: %v", err) + } + + resp, err := client.CancelSidecar( + context.Background(), &poolrpc.CancelSidecarRequest{ + SidecarId: idBytes, + }, + ) + if err != nil { + return err + } + + printRespJSON(resp) + + return nil +}