Skip to content

Commit

Permalink
Allow to cancel runs
Browse files Browse the repository at this point in the history
```
➜  spacectl git:(main) ✗ spc stack cancel --id testing-component --run 01HCHMRJ9JF64E41B4NR5FQ87Q
You have successfully canceled a run
The run can be visited at http://tomasmik.app.spacelift.tf/stack/testing-component/run/01HCHMRJ9JF64E41B4NR5FQ87Q
➜  spacectl git:(main) ✗ spc stack cancel --id testing-component --run 01HCHMZWE9CAVKGWHN2E2CZ7K3 --tail
You have successfully canceled a run
The run can be visited at http://tomasmik.app.spacelift.tf/stack/testing-component/run/01HCHMZWE9CAVKGWHN2E2CZ7K3

-----------------
QUEUED	Thu Oct 12 12:55:23 EEST 2023
-----------------

-----------------
CANCELED	Fri Oct 13 12:49:40 EEST 2023	api::01HBR32BK0ZD84TB1DKF83D760
-----------------

2023/10/13 12:49:40 finished with CANCELED state
```
  • Loading branch information
tomasmik committed Oct 16, 2023
1 parent 33e63aa commit f4cc174
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
56 changes: 56 additions & 0 deletions internal/cmd/stack/run_cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package stack

import (
"context"
"fmt"

"github.com/shurcooL/graphql"
"github.com/urfave/cli/v2"

"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
)

func runCancel() cli.ActionFunc {
return func(cliCtx *cli.Context) error {
stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

var mutation struct {
RunDiscard struct {
ID string `graphql:"id"`
} `graphql:"runCancel(stack: $stack, run: $run)"`
}

variables := map[string]interface{}{
"stack": graphql.ID(stackID),
"run": graphql.ID(cliCtx.String(flagRequiredRun.Name)),
}

ctx := context.Background()

if err := authenticated.Client.Mutate(ctx, &mutation, variables); err != nil {
return err
}

fmt.Println("You have successfully canceled the run")

fmt.Println("The run can be visited at", authenticated.Client.URL(
"/stack/%s/run/%s",
stackID,
mutation.RunDiscard.ID,
))

if !cliCtx.Bool(flagTail.Name) {
return nil
}

terminal, err := runLogsWithAction(ctx, stackID, mutation.RunDiscard.ID, nil)
if err != nil {
return err
}

return terminal.Error()
}
}
13 changes: 13 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func Command() *cli.Command {
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
{
Category: "Run management",
Name: "cancel",
Usage: "Cancel a run that hasn't started yet",
Flags: []cli.Flag{
flagStackID,
flagRequiredRun,
flagTail,
},
Action: runCancel(),
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
{
Category: "Run management",
Name: "approve",
Expand Down

0 comments on commit f4cc174

Please sign in to comment.