diff --git a/internal/cmd/stack/local_preview.go b/internal/cmd/stack/local_preview.go index 4915488..d1207d0 100644 --- a/internal/cmd/stack/local_preview.go +++ b/internal/cmd/stack/local_preview.go @@ -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 } diff --git a/internal/cmd/stack/run_confirm.go b/internal/cmd/stack/run_confirm.go index 024a17c..d8c4694 100644 --- a/internal/cmd/stack/run_confirm.go +++ b/internal/cmd/stack/run_confirm.go @@ -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 } diff --git a/internal/cmd/stack/run_discard.go b/internal/cmd/stack/run_discard.go index b3d2a4f..1ced6b0 100644 --- a/internal/cmd/stack/run_discard.go +++ b/internal/cmd/stack/run_discard.go @@ -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 } diff --git a/internal/cmd/stack/run_logs.go b/internal/cmd/stack/run_logs.go index 2d17047..3880570 100644 --- a/internal/cmd/stack/run_logs.go +++ b/internal/cmd/stack/run_logs.go @@ -11,6 +11,10 @@ import ( "github.com/spacelift-io/spacectl/internal/cmd/authenticated" ) +// 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) { @@ -28,21 +32,6 @@ func runLogsWithAction(ctx context.Context, stack, run string, acFn actionOnRunS 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, nil) - close(lines) - }() - - for line := range lines { - fmt.Print(line) - } - - return -} - func runStates(ctx context.Context, stack, run string, sink chan<- string, acFn actionOnRunState) (*structs.RunStateTransition, error) { var query struct { Stack *struct { diff --git a/internal/cmd/stack/run_retry.go b/internal/cmd/stack/run_retry.go index 8e5a17a..8023233 100644 --- a/internal/cmd/stack/run_retry.go +++ b/internal/cmd/stack/run_retry.go @@ -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 } diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index 86a63ef..080bed9 100644 --- a/internal/cmd/stack/stack.go +++ b/internal/cmd/stack/stack.go @@ -140,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, diff --git a/internal/cmd/stack/task_command.go b/internal/cmd/stack/task_command.go index 8f00573..3ae6389 100644 --- a/internal/cmd/stack/task_command.go +++ b/internal/cmd/stack/task_command.go @@ -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 }