Skip to content

Commit

Permalink
Allow to auto confirm a run during stack deploy command (#170)
Browse files Browse the repository at this point in the history
* Allow to auto confirm a run during `stack deploy` command
* Remove the `runLogs` function
  • Loading branch information
tomasmik authored Aug 4, 2023
1 parent 5d6cbd1 commit d4f681a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 11 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
2 changes: 1 addition & 1 deletion internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func localPreview() cli.ActionFunc {
return nil
}

terminal, err := runLogs(ctx, stackID, triggerMutation.RunProposeLocalWorkspace.ID)
terminal, err := runLogsWithAction(ctx, stackID, triggerMutation.RunProposeLocalWorkspace.ID, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/stack/run_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func runConfirm() cli.ActionFunc {
return nil
}

terminal, err := runLogs(ctx, stackID, mutation.RunConfirm.ID)
terminal, err := runLogsWithAction(ctx, stackID, mutation.RunConfirm.ID, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/stack/run_discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func runDiscard() cli.ActionFunc {
return nil
}

terminal, err := runLogs(ctx, stackID, mutation.RunDiscard.ID)
terminal, err := runLogsWithAction(ctx, stackID, mutation.RunDiscard.ID, nil)
if err != nil {
return err
}
Expand Down
18 changes: 15 additions & 3 deletions internal/cmd/stack/run_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import (
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
)

func runLogs(ctx context.Context, stack, run string) (terminal *structs.RunStateTransition, err error) {
// actionOnRunState is a function that can be executed on a run state.
//
// It can be used to interact with the run during the log reading,
// for example to confirm a run.
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)
terminal, err = runStates(ctx, stack, run, lines, acFn)
close(lines)
}()

Expand All @@ -26,7 +32,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 +87,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
2 changes: 1 addition & 1 deletion internal/cmd/stack/run_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func runRetry(cliCtx *cli.Context) error {
return nil
}

terminal, err := runLogs(cliCtx.Context, stackID, mutation.RunRetry.ID)
terminal, err := runLogsWithAction(cliCtx.Context, stackID, mutation.RunRetry.ID, nil)
if err != nil {
return err
}
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
3 changes: 2 additions & 1 deletion 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 Expand Up @@ -139,7 +140,7 @@ func Command() *cli.Command {
if err != nil {
return err
}
_, err = runLogs(context.Background(), stackID, cliCtx.String(flagRequiredRun.Name))
_, err = runLogsWithAction(context.Background(), stackID, cliCtx.String(flagRequiredRun.Name), nil)
return err
},
Before: authenticated.Ensure,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/stack/task_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func taskCommand(cliCtx *cli.Context) error {
return nil
}

terminal, err := runLogs(ctx, stackID, mutation.TaskCreate.ID)
terminal, err := runLogsWithAction(ctx, stackID, mutation.TaskCreate.ID, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit d4f681a

Please sign in to comment.