Skip to content

Commit eeddb52

Browse files
committed
Fix log output
1 parent d36ab53 commit eeddb52

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

cli/cliui/agent.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
8080
if err != nil {
8181
return xerrors.Errorf("fetch: %w", err)
8282
}
83+
logSources := map[uuid.UUID]codersdk.WorkspaceAgentLogSource{}
84+
for _, source := range agent.LogSources {
85+
logSources[source.ID] = source
86+
}
8387

8488
sw := &stageWriter{w: writer}
8589

@@ -123,7 +127,7 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
123127
return nil
124128
}
125129

126-
stage := "Running workspace agent startup script"
130+
stage := "Running workspace agent startup scripts"
127131
follow := opts.Wait
128132
if !follow {
129133
stage += " (non-blocking)"
@@ -173,7 +177,12 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
173177
return nil
174178
}
175179
for _, log := range logs {
176-
sw.Log(log.CreatedAt, log.Level, log.Output)
180+
source, hasSource := logSources[log.SourceID]
181+
output := log.Output
182+
if hasSource && source.DisplayName != "" {
183+
output = source.DisplayName + ": " + output
184+
}
185+
sw.Log(log.CreatedAt, log.Level, output)
177186
lastLog = log
178187
}
179188
}
@@ -195,13 +204,13 @@ func Agent(ctx context.Context, writer io.Writer, agentID uuid.UUID, opts AgentO
195204
case codersdk.WorkspaceAgentLifecycleStartError:
196205
sw.Fail(stage, agent.ReadyAt.Sub(*agent.StartedAt))
197206
// Use zero time (omitted) to separate these from the startup logs.
198-
sw.Log(time.Time{}, codersdk.LogLevelWarn, "Warning: The startup script exited with an error and your workspace may be incomplete.")
207+
sw.Log(time.Time{}, codersdk.LogLevelWarn, "Warning: A startup script exited with an error and your workspace may be incomplete.")
199208
sw.Log(time.Time{}, codersdk.LogLevelWarn, troubleshootingMessage(agent, "https://coder.com/docs/v2/latest/templates#startup-script-exited-with-an-error"))
200209
default:
201210
switch {
202211
case agent.LifecycleState.Starting():
203212
// Use zero time (omitted) to separate these from the startup logs.
204-
sw.Log(time.Time{}, codersdk.LogLevelWarn, "Notice: The startup script is still running and your workspace may be incomplete.")
213+
sw.Log(time.Time{}, codersdk.LogLevelWarn, "Notice: The startup scripts are still running and your workspace may be incomplete.")
205214
sw.Log(time.Time{}, codersdk.LogLevelWarn, troubleshootingMessage(agent, "https://coder.com/docs/v2/latest/templates#your-workspace-may-be-incomplete"))
206215
// Note: We don't complete or fail the stage here, it's
207216
// intentionally left open to indicate this stage didn't

cli/cliui/agent_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func TestAgent(t *testing.T) {
5252
want: []string{
5353
"⧗ Waiting for the workspace agent to connect",
5454
"✔ Waiting for the workspace agent to connect",
55-
"⧗ Running workspace agent startup script (non-blocking)",
56-
"Notice: The startup script is still running and your workspace may be incomplete.",
55+
"⧗ Running workspace agent startup scripts (non-blocking)",
56+
"Notice: The startup scripts are still running and your workspace may be incomplete.",
5757
"For more information and troubleshooting, see",
5858
},
5959
},
@@ -86,8 +86,8 @@ func TestAgent(t *testing.T) {
8686
"The workspace agent is having trouble connecting, wait for it to connect or restart your workspace.",
8787
"For more information and troubleshooting, see",
8888
"✔ Waiting for the workspace agent to connect",
89-
"⧗ Running workspace agent startup script (non-blocking)",
90-
"✔ Running workspace agent startup script (non-blocking)",
89+
"⧗ Running workspace agent startup scripts (non-blocking)",
90+
"✔ Running workspace agent startup scripts (non-blocking)",
9191
},
9292
},
9393
{
@@ -120,7 +120,7 @@ func TestAgent(t *testing.T) {
120120
},
121121
},
122122
{
123-
name: "Startup script logs",
123+
name: "Startup Logs",
124124
opts: cliui.AgentOptions{
125125
FetchInterval: time.Millisecond,
126126
Wait: true,
@@ -152,10 +152,10 @@ func TestAgent(t *testing.T) {
152152
},
153153
},
154154
want: []string{
155-
"⧗ Running workspace agent startup script",
155+
"⧗ Running workspace agent startup scripts",
156156
"Hello world",
157157
"Bye now",
158-
"✔ Running workspace agent startup script",
158+
"✔ Running workspace agent startup scripts",
159159
},
160160
},
161161
{
@@ -181,10 +181,10 @@ func TestAgent(t *testing.T) {
181181
},
182182
},
183183
want: []string{
184-
"⧗ Running workspace agent startup script",
184+
"⧗ Running workspace agent startup scripts",
185185
"Hello world",
186-
"✘ Running workspace agent startup script",
187-
"Warning: The startup script exited with an error and your workspace may be incomplete.",
186+
"✘ Running workspace agent startup scripts",
187+
"Warning: A startup script exited with an error and your workspace may be incomplete.",
188188
"For more information and troubleshooting, see",
189189
},
190190
},
@@ -229,9 +229,9 @@ func TestAgent(t *testing.T) {
229229
},
230230
},
231231
want: []string{
232-
"⧗ Running workspace agent startup script",
232+
"⧗ Running workspace agent startup scripts",
233233
"Hello world",
234-
"✔ Running workspace agent startup script",
234+
"✔ Running workspace agent startup scripts",
235235
},
236236
wantErr: true,
237237
},
@@ -339,6 +339,9 @@ func TestAgent(t *testing.T) {
339339
line := s.Text()
340340
t.Log(line)
341341
if len(tc.want) == 0 {
342+
for i := 0; i < 5; i++ {
343+
t.Log(line)
344+
}
342345
require.Fail(t, "unexpected line", line)
343346
}
344347
require.Contains(t, line, tc.want[0])

0 commit comments

Comments
 (0)