|
9 | 9 |
|
10 | 10 | "github.com/AlecAivazis/survey/v2" |
11 | 11 | "github.com/fatih/camelcase" |
12 | | - "github.com/github/ghcs/api" |
13 | 12 | "github.com/github/ghcs/cmd/ghcs/output" |
| 13 | + "github.com/github/ghcs/internal/api" |
14 | 14 | "github.com/github/ghcs/internal/codespaces" |
15 | 15 | "github.com/spf13/cobra" |
16 | 16 | ) |
@@ -105,12 +105,16 @@ func create(opts *createOptions) error { |
105 | 105 | return nil |
106 | 106 | } |
107 | 107 |
|
| 108 | +// showStatus polls the codespace for a list of post create states and their status. It will keep polling |
| 109 | +// until all states have finished. Once all states have finished, we poll once more to check if any new |
| 110 | +// states have been introduced and stop polling otherwise. |
108 | 111 | func showStatus(ctx context.Context, log *output.Logger, apiClient *api.API, user *api.User, codespace *api.Codespace) error { |
109 | 112 | var lastState codespaces.PostCreateState |
110 | 113 | var breakNextState bool |
111 | 114 |
|
112 | 115 | finishedStates := make(map[string]bool) |
113 | 116 | ctx, stopPolling := context.WithCancel(ctx) |
| 117 | + defer stopPolling() |
114 | 118 |
|
115 | 119 | poller := func(states []codespaces.PostCreateState) { |
116 | 120 | var inProgress bool |
@@ -153,7 +157,12 @@ func showStatus(ctx context.Context, log *output.Logger, apiClient *api.API, use |
153 | 157 | } |
154 | 158 | } |
155 | 159 |
|
156 | | - if err := codespaces.PollPostCreateStates(ctx, log, apiClient, user, codespace, poller); err != nil { |
| 160 | + err := codespaces.PollPostCreateStates(ctx, log, apiClient, user, codespace, poller) |
| 161 | + if err != nil { |
| 162 | + if errors.Is(err, context.Canceled) && breakNextState { |
| 163 | + return nil // we cancelled the context to stop polling, we can ignore the error |
| 164 | + } |
| 165 | + |
157 | 166 | return fmt.Errorf("failed to poll state changes from codespace: %v", err) |
158 | 167 | } |
159 | 168 |
|
|
0 commit comments