Skip to content

Commit 249b5fd

Browse files
committed
simplify output states
1 parent 05aa7e3 commit 249b5fd

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

cli/cliui/agent.go

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,7 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
8080

8181
sw := &stageWriter{w: writer}
8282

83-
showInitialConnection := true
8483
showStartupLogs := false
85-
86-
printInitialConnection := func() error {
87-
showInitialConnection = false
88-
89-
// Since we were waiting for the agent to connect, also show
90-
// startup logs.
91-
showStartupLogs = true
92-
93-
stage := "Waiting for the workspace agent to connect"
94-
sw.Start(stage)
95-
for agent.Status == codersdk.WorkspaceAgentConnecting {
96-
if agent, err = fetch(); err != nil {
97-
return xerrors.Errorf("fetch: %w", err)
98-
}
99-
}
100-
101-
if agent.Status == codersdk.WorkspaceAgentTimeout {
102-
now := time.Now()
103-
sw.Log(now, codersdk.LogLevelInfo, "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace.")
104-
sw.Log(now, codersdk.LogLevelInfo, troubleshootingMessage(agent, "https://coder.com/docs/v2/latest/templates#agent-connection-issues"))
105-
for agent.Status == codersdk.WorkspaceAgentTimeout {
106-
if agent, err = fetch(); err != nil {
107-
return xerrors.Errorf("fetch: %w", err)
108-
}
109-
}
110-
}
111-
sw.Complete(stage, agent.FirstConnectedAt.Sub(agent.CreatedAt))
112-
return nil
113-
}
114-
11584
for {
11685
// It doesn't matter if we're connected or not, if the agent is
11786
// shutting down, we don't know if it's coming back.
@@ -121,25 +90,36 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
12190

12291
switch agent.Status {
12392
case codersdk.WorkspaceAgentConnecting, codersdk.WorkspaceAgentTimeout:
124-
err = printInitialConnection()
125-
if err != nil {
126-
return xerrors.Errorf("initial connection: %w", err)
93+
// Since we were waiting for the agent to connect, also show
94+
// startup logs if applicable.
95+
showStartupLogs = true
96+
97+
stage := "Waiting for the workspace agent to connect"
98+
sw.Start(stage)
99+
for agent.Status == codersdk.WorkspaceAgentConnecting {
100+
if agent, err = fetch(); err != nil {
101+
return xerrors.Errorf("fetch: %w", err)
102+
}
103+
}
104+
105+
if agent.Status == codersdk.WorkspaceAgentTimeout {
106+
now := time.Now()
107+
sw.Log(now, codersdk.LogLevelInfo, "The workspace agent is having trouble connecting, wait for it to connect or restart your workspace.")
108+
sw.Log(now, codersdk.LogLevelInfo, troubleshootingMessage(agent, "https://coder.com/docs/v2/latest/templates#agent-connection-issues"))
109+
for agent.Status == codersdk.WorkspaceAgentTimeout {
110+
if agent, err = fetch(); err != nil {
111+
return xerrors.Errorf("fetch: %w", err)
112+
}
113+
}
127114
}
115+
sw.Complete(stage, agent.FirstConnectedAt.Sub(agent.CreatedAt))
128116

129117
case codersdk.WorkspaceAgentConnected:
130118
if !showStartupLogs && agent.LifecycleState == codersdk.WorkspaceAgentLifecycleReady {
131119
// The workspace is ready, there's nothing to do but connect.
132120
return nil
133121
}
134122

135-
if showInitialConnection {
136-
// Like with provisioner build logs, show a bit of history.
137-
err = printInitialConnection()
138-
if err != nil {
139-
return xerrors.Errorf("initial connection: %w", err)
140-
}
141-
}
142-
143123
stage := "Running workspace agent startup script"
144124
follow := opts.Wait
145125
if !follow {
@@ -192,10 +172,10 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
192172
case codersdk.WorkspaceAgentLifecycleReady:
193173
sw.Complete(stage, agent.ReadyAt.Sub(*agent.StartedAt))
194174
case codersdk.WorkspaceAgentLifecycleStartError:
175+
sw.Fail(stage, agent.ReadyAt.Sub(*agent.StartedAt))
195176
// Use zero time (omitted) to separate these from the startup logs.
196177
sw.Log(time.Time{}, codersdk.LogLevelWarn, "Warning: The startup script exited with an error and your workspace may be incomplete.")
197178
sw.Log(time.Time{}, codersdk.LogLevelWarn, troubleshootingMessage(agent, "https://coder.com/docs/v2/latest/templates#startup-script-exited-with-an-error"))
198-
sw.Fail(stage, agent.ReadyAt.Sub(*agent.StartedAt))
199179
default:
200180
switch {
201181
case agent.LifecycleState.Starting():
@@ -216,7 +196,9 @@ func Agent(ctx context.Context, writer io.Writer, opts AgentOptions) error {
216196
return nil
217197

218198
case codersdk.WorkspaceAgentDisconnected:
219-
showInitialConnection = false
199+
// If the agent was still starting during disconnect, we'll
200+
// show startup logs.
201+
showStartupLogs = agent.LifecycleState.Starting()
220202

221203
stage := "The workspace agent lost connection"
222204
sw.Start(stage)
@@ -239,3 +221,9 @@ func troubleshootingMessage(agent codersdk.WorkspaceAgent, url string) string {
239221
}
240222
return m
241223
}
224+
225+
type closeFunc func() error
226+
227+
func (c closeFunc) Close() error {
228+
return c()
229+
}

cli/cliui/agent_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ func TestAgent(t *testing.T) {
161161
},
162162
},
163163
want: []string{
164-
"⧗ Waiting for the workspace agent to connect",
165-
"✔ Waiting for the workspace agent to connect",
166164
"⧗ Running workspace agent startup script",
167165
"Hello world",
168166
"Bye now",
@@ -193,13 +191,11 @@ func TestAgent(t *testing.T) {
193191
},
194192
},
195193
want: []string{
196-
"⧗ Waiting for the workspace agent to connect",
197-
"✔ Waiting for the workspace agent to connect",
198194
"⧗ Running workspace agent startup script",
199195
"Hello world",
196+
"✘ Running workspace agent startup script",
200197
"Warning: The startup script exited with an error and your workspace may be incomplete.",
201198
"For more information and troubleshooting, see",
202-
"✘ Running workspace agent startup script",
203199
},
204200
},
205201
{
@@ -245,8 +241,6 @@ func TestAgent(t *testing.T) {
245241
},
246242
},
247243
want: []string{
248-
"⧗ Waiting for the workspace agent to connect",
249-
"✔ Waiting for the workspace agent to connect",
250244
"⧗ Running workspace agent startup script",
251245
"Hello world",
252246
"✔ Running workspace agent startup script",

0 commit comments

Comments
 (0)