Skip to content

Commit

Permalink
Remove the runLogs function
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik committed Aug 4, 2023
1 parent f0edad7 commit 9352cec
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 21 deletions.
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
19 changes: 4 additions & 15 deletions internal/cmd/stack/run_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
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
2 changes: 1 addition & 1 deletion internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 9352cec

Please sign in to comment.