Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: use cliu.OutputFormatter
  • Loading branch information
DanielleMaywood committed Sep 25, 2025
commit e6bc27c61bc4fcdc9513cc3a3549c589ba264d01
24 changes: 18 additions & 6 deletions cli/exp_task_logs.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package cli

import (
"encoding/json"
"fmt"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)

func (r *RootCmd) taskLogs() *serpent.Command {
formatter := cliui.NewOutputFormatter(
cliui.TableFormat(
[]codersdk.TaskLogEntry{},
[]string{
"type",
"content",
},
),
cliui.JSONFormat(),
)

cmd := &serpent.Command{
Use: "logs <task>",
Short: "Show a task's logs",
Expand Down Expand Up @@ -46,16 +58,16 @@ func (r *RootCmd) taskLogs() *serpent.Command {
return xerrors.Errorf("get task logs: %w", err)
}

enc := json.NewEncoder(inv.Stdout)
for _, log := range logs.Logs {
if err := enc.Encode(log); err != nil {
return xerrors.Errorf("encode task log: %w", err)
}
out, err := formatter.Format(ctx, logs.Logs)
if err != nil {
return xerrors.Errorf("format task logs: %w", err)
}

_, _ = fmt.Fprintln(inv.Stdout, out)
return nil
},
}

formatter.AttachOptions(&cmd.Options)
return cmd
}
48 changes: 32 additions & 16 deletions cli/exp_task_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/util/slice"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
Expand Down Expand Up @@ -48,12 +47,13 @@ func Test_TaskLogs(t *testing.T) {

tests := []struct {
args []string
expectTable string
expectLogs []codersdk.TaskLogEntry
expectError string
handler func(t *testing.T, ctx context.Context) http.HandlerFunc
}{
{
args: []string{taskName},
args: []string{taskName, "--output", "json"},
expectLogs: taskLogs,
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -73,7 +73,7 @@ func Test_TaskLogs(t *testing.T) {
},
},
{
args: []string{taskID.String()},
args: []string{taskID.String(), "--output", "json"},
expectLogs: taskLogs,
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -88,6 +88,25 @@ func Test_TaskLogs(t *testing.T) {
}
},
},
{
args: []string{taskID.String()},
expectTable: `
TYPE CONTENT
input What is 1 + 1?
output 2`,
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case fmt.Sprintf("/api/experimental/tasks/me/%s/logs", taskID.String()):
httpapi.Write(ctx, w, http.StatusOK, codersdk.TaskLogsResponse{
Logs: taskLogs,
})
default:
t.Errorf("unexpected path: %s", r.URL.Path)
}
}
},
},
{
args: []string{"doesnotexist"},
expectError: httpapi.ResourceNotFoundResponse.Message,
Expand Down Expand Up @@ -163,21 +182,18 @@ func Test_TaskLogs(t *testing.T) {
assert.ErrorContains(t, err, tt.expectError)
}

if tt.expectLogs != nil {
lines := strings.Split(stdout.String(), "\n")
lines = slice.Filter(lines, func(s string) bool {
return s != ""
})

require.Len(t, lines, len(tt.expectLogs))
if tt.expectTable != "" {
if diff := tableDiff(tt.expectTable, stdout.String()); diff != "" {
t.Errorf("unexpected output diff (-want +got):\n%s", diff)
}
}

for i, line := range lines {
var log codersdk.TaskLogEntry
err = json.NewDecoder(strings.NewReader(line)).Decode(&log)
require.NoError(t, err)
if tt.expectLogs != nil {
var logs []codersdk.TaskLogEntry
err = json.NewDecoder(strings.NewReader(stdout.String())).Decode(&logs)
require.NoError(t, err)

assert.Equal(t, tt.expectLogs[i], log)
}
assert.Equal(t, tt.expectLogs, logs)
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions codersdk/aitasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ const (
//
// Experimental: This type is experimental and may change in the future.
type TaskLogEntry struct {
ID int `json:"id"`
Content string `json:"content"`
Type TaskLogType `json:"type" enum:"input,output"`
Time time.Time `json:"time" format:"date-time"`
ID int `json:"id" table:"id"`
Content string `json:"content" table:"content"`
Type TaskLogType `json:"type" enum:"input,output" table:"type"`
Time time.Time `json:"time" format:"date-time" table:"time,default_sort"`
}

// TaskLogsResponse contains the logs for a task.
Expand Down