Skip to content

Commit

Permalink
Allow to auto confirm a run during stack deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik committed Aug 4, 2023
1 parent 5d6cbd1 commit f0edad7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
7 changes: 7 additions & 0 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ var flagTail = &cli.BoolFlag{
Value: false,
}

var flagAutoConfirm = &cli.BoolFlag{
Name: "auto-confirm",
Usage: "Indicate whether to automatically confirm the run. It also forces the run log tailing.",
Value: false,
Required: false,
}

var flagNoTail = &cli.BoolFlag{
Name: "no-tail",
Usage: "Indicate whether not to tail the run",
Expand Down
27 changes: 25 additions & 2 deletions internal/cmd/stack/run_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,28 @@ import (
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
)

type actionOnRunState func(state structs.RunState, stackID, runID string) error

func runLogsWithAction(ctx context.Context, stack, run string, acFn actionOnRunState) (terminal *structs.RunStateTransition, err error) {
lines := make(chan string)

go func() {
terminal, err = runStates(ctx, stack, run, lines, acFn)
close(lines)
}()

for line := range lines {
fmt.Print(line)
}

return
}

func runLogs(ctx context.Context, stack, run string) (terminal *structs.RunStateTransition, err error) {
lines := make(chan string)

go func() {
terminal, err = runStates(ctx, stack, run, lines)
terminal, err = runStates(ctx, stack, run, lines, nil)
close(lines)
}()

Expand All @@ -26,7 +43,7 @@ func runLogs(ctx context.Context, stack, run string) (terminal *structs.RunState
return
}

func runStates(ctx context.Context, stack, run string, sink chan<- string) (*structs.RunStateTransition, error) {
func runStates(ctx context.Context, stack, run string, sink chan<- string, acFn actionOnRunState) (*structs.RunStateTransition, error) {
var query struct {
Stack *struct {
Run *struct {
Expand Down Expand Up @@ -81,6 +98,12 @@ func runStates(ctx context.Context, stack, run string, sink chan<- string) (*str
}
}

if acFn != nil {
if err := acFn(transition.State, stack, run); err != nil {
return nil, fmt.Errorf("failed to execute action on run state: %w", err)
}
}

if transition.Terminal {
return &transition, nil
}
Expand Down
32 changes: 30 additions & 2 deletions internal/cmd/stack/run_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,39 @@ func runTrigger(spaceliftType, humanType string) cli.ActionFunc {
mutation.RunTrigger.ID,
))

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

terminal, err := runLogs(ctx, stackID, mutation.RunTrigger.ID)
actionFn := func(state structs.RunState, stackID, runID string) error {
if state != "UNCONFIRMED" {
return nil
}

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

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

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

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

fmt.Println("Deployment was automatically confirmed because of --auto-confirm flag")
return nil
}

terminal, err := runLogsWithAction(ctx, stackID, mutation.RunTrigger.ID, actionFn)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Command() *cli.Command {
flagCommitSHA,
flagRunMetadata,
flagTail,
flagAutoConfirm,
},
Action: runTrigger("TRACKED", "deployment"),
Before: authenticated.Ensure,
Expand Down

0 comments on commit f0edad7

Please sign in to comment.