Skip to content

Commit 46ee45b

Browse files
committed
simplify the state iteration
1 parent 55f0dad commit 46ee45b

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

cmd/ghcs/create.go

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,47 +92,43 @@ func Create() error {
9292
var lastState codespaces.PostCreateState
9393
var breakNextState bool
9494

95-
PollStates:
9695
for {
97-
select {
98-
case stateUpdate := <-states:
99-
if stateUpdate.Err != nil {
100-
return fmt.Errorf("receive state update: %v", err)
101-
}
96+
stateUpdate := <-states
97+
if stateUpdate.Err != nil {
98+
return fmt.Errorf("receive state update: %v", err)
99+
}
102100

103-
var inProgress bool
104-
for _, state := range stateUpdate.PostCreateStates {
105-
switch state.Status {
106-
case codespaces.PostCreateStateRunning:
107-
if lastState != state {
108-
lastState = state
109-
fmt.Print(state.Name)
110-
} else {
111-
fmt.Print(".")
112-
}
113-
114-
inProgress = true
115-
break
116-
case codespaces.PostCreateStateFailed:
117-
if lastState.Name == state.Name && lastState.Status != state.Status {
118-
lastState = state
119-
fmt.Print(".Failed\n")
120-
}
121-
case codespaces.PostCreateStateSuccess:
122-
if lastState.Name == state.Name && lastState.Status != state.Status {
123-
lastState = state
124-
fmt.Print(".Success\n")
125-
}
101+
var inProgress bool
102+
for _, state := range stateUpdate.PostCreateStates {
103+
switch state.Status {
104+
case codespaces.PostCreateStateRunning:
105+
if lastState != state {
106+
lastState = state
107+
log.Print(state.Name)
108+
} else {
109+
log.Print(".")
126110
}
127-
}
128111

129-
switch {
130-
case !inProgress && !breakNextState:
131-
breakNextState = true
132-
case !inProgress && breakNextState:
133-
break PollStates
112+
inProgress = true
113+
break
114+
case codespaces.PostCreateStateFailed:
115+
if lastState.Name == state.Name && lastState.Status != state.Status {
116+
lastState = state
117+
log.Print(".Failed\n")
118+
}
119+
case codespaces.PostCreateStateSuccess:
120+
if lastState.Name == state.Name && lastState.Status != state.Status {
121+
lastState = state
122+
log.Print(".Success\n")
123+
}
134124
}
135125
}
126+
127+
if !inProgress && !breakNextState {
128+
breakNextState = true
129+
} else if !inProgress && breakNextState {
130+
break
131+
}
136132
}
137133

138134
log.Printf("Codespace created: %s\n", codespace.Name)

cmd/ghcs/ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func SSH(sshProfile, codespaceName string, sshServerPort int) error {
6464
return fmt.Errorf("error creating liveshare terminal: %v", err)
6565
}
6666

67-
log.Println("Preparing SSH...")
67+
log.Print("Preparing SSH...")
6868
if sshProfile == "" {
6969
containerID, err := getContainerID(ctx, log, terminal)
7070
if err != nil {

internal/codespaces/codespaces.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func ConnectToLiveshare(ctx context.Context, log logger, apiClient *api.API, use
6666
var startedCodespace bool
6767
if codespace.Environment.State != api.CodespaceEnvironmentStateAvailable {
6868
startedCodespace = true
69-
log.Println("Starting your codespace...")
69+
log.Print("Starting your codespace...")
7070
if err := apiClient.StartCodespace(ctx, token, codespace); err != nil {
7171
return nil, fmt.Errorf("error starting codespace: %v", err)
7272
}
@@ -97,6 +97,7 @@ func ConnectToLiveshare(ctx context.Context, log logger, apiClient *api.API, use
9797
if startedCodespace {
9898
fmt.Print("\n")
9999
}
100+
100101
log.Println("Connecting to your codespace...")
101102

102103
lsclient, err := liveshare.NewClient(

0 commit comments

Comments
 (0)