Skip to content

Commit cb6552f

Browse files
committed
more efficient impl for processing states
1 parent e4e77a4 commit cb6552f

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

cmd/ghcs/create.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func showStatus(ctx context.Context, log *output.Logger, apiClient *api.API, use
112112
}
113113

114114
var lastState codespaces.PostCreateState
115+
finishedStates := make(map[string]bool)
115116
var breakNextState bool
116117

117118
for {
@@ -122,25 +123,31 @@ func showStatus(ctx context.Context, log *output.Logger, apiClient *api.API, use
122123

123124
var inProgress bool
124125
for _, state := range stateUpdate.PostCreateStates {
125-
switch state.Status {
126-
case codespaces.PostCreateStateRunning:
127-
if lastState != state {
128-
lastState = state
129-
log.Print(state.Name)
130-
} else {
131-
log.Print(".")
132-
}
126+
if _, found := finishedStates[state.Name]; found {
127+
continue // skip this state as we've processed it already
128+
}
133129

134-
inProgress = true
135-
case codespaces.PostCreateStateFailed:
136-
if lastState.Name == state.Name && lastState.Status != state.Status {
130+
if state.Name != lastState.Name {
131+
log.Print(state.Name)
132+
133+
if state.Status == codespaces.PostCreateStateRunning {
134+
inProgress = true
137135
lastState = state
138-
log.Print(".Failed\n")
136+
log.Print("...")
137+
break
138+
} else {
139+
finishedStates[state.Name] = true
140+
log.Println("..." + state.Status)
139141
}
140-
case codespaces.PostCreateStateSuccess:
141-
if lastState.Name == state.Name && lastState.Status != state.Status {
142-
lastState = state
143-
log.Print(".Success\n")
142+
} else {
143+
if state.Status == codespaces.PostCreateStateRunning {
144+
inProgress = true
145+
log.Print(".")
146+
break
147+
} else {
148+
finishedStates[state.Name] = true
149+
log.Println(state.Status)
150+
lastState = codespaces.PostCreateState{} // reset the value
144151
}
145152
}
146153
}

internal/codespaces/codespaces.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ type logger interface {
6262
Println(v ...interface{}) (int, error)
6363
}
6464

65+
func connectionReady(codespace *api.Codespace) bool {
66+
ready := codespace.Environment.Connection.SessionID != ""
67+
ready = ready && codespace.Environment.Connection.SessionToken != ""
68+
ready = ready && codespace.Environment.Connection.RelayEndpoint != ""
69+
ready = ready && codespace.Environment.Connection.RelaySAS != ""
70+
ready = ready && codespace.Environment.State == api.CodespaceEnvironmentStateAvailable
71+
return ready
72+
}
73+
6574
func ConnectToLiveshare(ctx context.Context, log logger, apiClient *api.API, userLogin, token string, codespace *api.Codespace) (client *liveshare.Client, err error) {
6675
var startedCodespace bool
6776
if codespace.Environment.State != api.CodespaceEnvironmentStateAvailable {
@@ -73,7 +82,7 @@ func ConnectToLiveshare(ctx context.Context, log logger, apiClient *api.API, use
7382
}
7483

7584
retries := 0
76-
for codespace.Environment.Connection.SessionID == "" || codespace.Environment.State != api.CodespaceEnvironmentStateAvailable {
85+
for !connectionReady(codespace) {
7786
if retries > 1 {
7887
if retries%2 == 0 {
7988
log.Print(".")

internal/codespaces/states.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"strings"
89
"time"
910

1011
"github.com/github/ghcs/api"
1112
)
1213

1314
type PostCreateStateStatus string
1415

16+
func (p PostCreateStateStatus) String() string {
17+
return strings.Title(string(p))
18+
}
19+
1520
const (
1621
PostCreateStateRunning PostCreateStateStatus = "running"
1722
PostCreateStateSuccess PostCreateStateStatus = "succeeded"

0 commit comments

Comments
 (0)