|
| 1 | +package connections |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/planetscale/cli/internal/cmdutil" |
| 9 | + live "github.com/planetscale/cli/internal/connections" |
| 10 | + "github.com/planetscale/cli/internal/printer" |
| 11 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 12 | +) |
| 13 | + |
| 14 | +type actionResult struct { |
| 15 | + Success bool `csv:"success" header:"success" json:"success"` |
| 16 | + Keyspace string `csv:"keyspace" header:"keyspace" json:"keyspace,omitempty"` |
| 17 | + Shard string `csv:"shard" header:"shard" json:"shard,omitempty"` |
| 18 | + Tablet string `csv:"tablet" header:"tablet" json:"tablet,omitempty"` |
| 19 | + ID int64 `csv:"id" header:"id,text" json:"id,omitempty"` |
| 20 | + Kind string `csv:"kind" header:"kind" json:"kind,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +func (a *actionResult) MarshalCSVValue() interface{} { |
| 24 | + return []*actionResult{a} |
| 25 | +} |
| 26 | + |
| 27 | +type compactActionResult struct { |
| 28 | + Success bool `csv:"success" header:"success" json:"success"` |
| 29 | + ID int64 `csv:"id" header:"id,text" json:"id,omitempty"` |
| 30 | + Kind string `csv:"kind" header:"kind" json:"kind,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +func (a *compactActionResult) MarshalCSVValue() interface{} { |
| 34 | + return []*compactActionResult{a} |
| 35 | +} |
| 36 | + |
| 37 | +func toActionResult(result live.ActionResult) *actionResult { |
| 38 | + return &actionResult{ |
| 39 | + Success: result.Success, |
| 40 | + Keyspace: result.Keyspace, |
| 41 | + Shard: result.Shard, |
| 42 | + Tablet: result.Tablet, |
| 43 | + ID: result.ID, |
| 44 | + Kind: result.Kind, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func toCompactActionResult(result live.ActionResult) *compactActionResult { |
| 49 | + return &compactActionResult{ |
| 50 | + Success: result.Success, |
| 51 | + ID: result.ID, |
| 52 | + Kind: result.Kind, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// RunCancelQuery cancels the active query identified by a live connection query ID. |
| 57 | +func RunCancelQuery(ctx context.Context, ch *cmdutil.Helper, database, branch, queryID string, target ConnectionTarget) error { |
| 58 | + return RunCancelQueryForEngine(ctx, ch, database, branch, queryID, ps.DatabaseEngineMySQL, target) |
| 59 | +} |
| 60 | + |
| 61 | +// RunCancelQueryForEngine cancels the active query and prints output for the resolved database engine. |
| 62 | +func RunCancelQueryForEngine(ctx context.Context, ch *cmdutil.Helper, database, branch, queryID string, engine ps.DatabaseEngine, target ConnectionTarget) error { |
| 63 | + return runAction(ctx, ch, database, branch, "query-id", queryID, target, func(ctx context.Context, client *live.Client, id string) (live.ActionResult, error) { |
| 64 | + return client.CancelQueryResult(ctx, live.ActionTarget{QueryID: &id}) |
| 65 | + }, engine) |
| 66 | +} |
| 67 | + |
| 68 | +// RunKillTransaction terminates the connection identified by a live connection transaction ID. |
| 69 | +func RunKillTransaction(ctx context.Context, ch *cmdutil.Helper, database, branch, transactionID string, target ConnectionTarget) error { |
| 70 | + return RunKillTransactionForEngine(ctx, ch, database, branch, transactionID, ps.DatabaseEnginePostgres, target) |
| 71 | +} |
| 72 | + |
| 73 | +// RunKillTransactionForEngine terminates a transaction and prints output for the resolved database engine. |
| 74 | +func RunKillTransactionForEngine(ctx context.Context, ch *cmdutil.Helper, database, branch, transactionID string, engine ps.DatabaseEngine, target ConnectionTarget) error { |
| 75 | + return runAction(ctx, ch, database, branch, "transaction-id", transactionID, target, func(ctx context.Context, client *live.Client, id string) (live.ActionResult, error) { |
| 76 | + return client.TerminateTransactionResult(ctx, live.ActionTarget{TransactionID: &id}) |
| 77 | + }, engine) |
| 78 | +} |
| 79 | + |
| 80 | +// RunKillConnection terminates the connection identified by a live connection_id. |
| 81 | +func RunKillConnection(ctx context.Context, ch *cmdutil.Helper, database, branch, connectionID string, target ConnectionTarget) error { |
| 82 | + return RunKillConnectionForEngine(ctx, ch, database, branch, connectionID, ps.DatabaseEngineMySQL, target) |
| 83 | +} |
| 84 | + |
| 85 | +// RunKillConnectionForEngine terminates a connection and prints output for the resolved database engine. |
| 86 | +func RunKillConnectionForEngine(ctx context.Context, ch *cmdutil.Helper, database, branch, connectionID string, engine ps.DatabaseEngine, target ConnectionTarget) error { |
| 87 | + return runAction(ctx, ch, database, branch, "connection-id", connectionID, target, func(ctx context.Context, client *live.Client, id string) (live.ActionResult, error) { |
| 88 | + return client.TerminateConnectionResult(ctx, live.ActionTarget{ConnectionID: &id}) |
| 89 | + }, engine) |
| 90 | +} |
| 91 | + |
| 92 | +func runAction(ctx context.Context, ch *cmdutil.Helper, database, branch, idName, id string, target ConnectionTarget, runAction func(context.Context, *live.Client, string) (live.ActionResult, error), engine ps.DatabaseEngine) error { |
| 93 | + if err := validateActionID(idName, id); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + id = strings.TrimSpace(id) |
| 97 | + |
| 98 | + client, err := newConnectionsClient(ch, database, branch, target) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + result, err := runAction(ctx, client, id) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + return printActionResult(ch, result, engine, idName) |
| 108 | +} |
| 109 | + |
| 110 | +// ValidateConnectionID checks the connection action identifier without making network calls. |
| 111 | +func ValidateConnectionID(id string) error { |
| 112 | + return validateActionID("connection-id", id) |
| 113 | +} |
| 114 | + |
| 115 | +// ValidateQueryID checks the query action identifier without making network calls. |
| 116 | +func ValidateQueryID(id string) error { |
| 117 | + return validateActionID("query-id", id) |
| 118 | +} |
| 119 | + |
| 120 | +// ValidateTransactionID checks the transaction action identifier without making network calls. |
| 121 | +func ValidateTransactionID(id string) error { |
| 122 | + return validateActionID("transaction-id", id) |
| 123 | +} |
| 124 | + |
| 125 | +func validateActionID(idName, id string) error { |
| 126 | + if strings.TrimSpace(id) == "" { |
| 127 | + return errors.New(idName + " is required") |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func printActionResult(ch *cmdutil.Helper, result live.ActionResult, engine ps.DatabaseEngine, idName string) error { |
| 133 | + if ch.Printer.Format() == printer.Human { |
| 134 | + ch.Printer.Printf("%s.\n", actionResultMessage(result, idName)) |
| 135 | + return nil |
| 136 | + } |
| 137 | + if ch.Printer.Format() == printer.JSON { |
| 138 | + return ch.Printer.PrintResource(toActionResult(result)) |
| 139 | + } |
| 140 | + if engine == ps.DatabaseEnginePostgres { |
| 141 | + return ch.Printer.PrintResource(toCompactActionResult(result)) |
| 142 | + } |
| 143 | + return ch.Printer.PrintResource(toActionResult(result)) |
| 144 | +} |
| 145 | + |
| 146 | +func actionResultMessage(result live.ActionResult, idName string) string { |
| 147 | + var message string |
| 148 | + switch idName { |
| 149 | + case "query-id": |
| 150 | + message = "Cancelled query" |
| 151 | + case "transaction-id": |
| 152 | + message = "Killed transaction" |
| 153 | + case "connection-id": |
| 154 | + message = "Killed connection" |
| 155 | + default: |
| 156 | + message = "Action sent" |
| 157 | + } |
| 158 | + if result.Tablet != "" { |
| 159 | + message += " on " + result.Tablet |
| 160 | + } |
| 161 | + return message |
| 162 | +} |
0 commit comments