Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(cli): add exp task logs#19915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
DanielleMaywood merged 2 commits intomainfromdanielle/tasks/cli-logs
Sep 26, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
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
5 changes: 3 additions & 2 deletionscli/exp_task.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,11 +13,12 @@ func (r *RootCmd) tasksCommand() *serpent.Command {
return i.Command.HelpHandler(i)
},
Children: []*serpent.Command{
r.taskList(),
r.taskCreate(),
r.taskStatus(),
r.taskDelete(),
r.taskList(),
r.taskLogs(),
r.taskSend(),
r.taskStatus(),
},
Comment on lines 15 to 22
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

drive-by: sort

}
return cmd
Expand Down
73 changes: 73 additions & 0 deletionscli/exp_task_logs.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
package cli

import (
"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",
Middleware: serpent.Chain(
serpent.RequireNArgs(1),
),
Handler: func(inv *serpent.Invocation) error {
client, err := r.InitClient(inv)
if err != nil {
return err
}

var (
ctx = inv.Context()
exp = codersdk.NewExperimentalClient(client)
task = inv.Args[0]
taskID uuid.UUID
)

if id, err := uuid.Parse(task); err == nil {
taskID = id
} else {
ws, err := namedWorkspace(ctx, client, task)
if err != nil {
return xerrors.Errorf("resolve task %q: %w", task, err)
}

taskID = ws.ID
}
Comment on lines +45 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

suggestion: at this point, we can probably use some kind of helpertaskByNameOrID

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Possibly. I'm curious about what it would look like though because I don't want to make an unnecessary API call.


logs, err := exp.TaskLogs(ctx, codersdk.Me, taskID)
if err != nil {
return xerrors.Errorf("get task logs: %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
}
200 changes: 200 additions & 0 deletionscli/exp_task_logs_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
package cli_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)

func Test_TaskLogs(t *testing.T) {
t.Parallel()

var (
clock = time.Date(2025, 8, 26, 12, 34, 56, 0, time.UTC)

taskID = uuid.MustParse("11111111-1111-1111-1111-111111111111")
taskName = "task-workspace"

taskLogs = []codersdk.TaskLogEntry{
{
ID: 0,
Content: "What is 1 + 1?",
Type: codersdk.TaskLogTypeInput,
Time: clock,
},
{
ID: 1,
Content: "2",
Type: codersdk.TaskLogTypeOutput,
Time: clock.Add(1 * time.Second),
},
}
)

tests := []struct {
args []string
expectTable string
expectLogs []codersdk.TaskLogEntry
expectError string
handler func(t *testing.T, ctx context.Context) http.HandlerFunc
}{
{
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) {
switch r.URL.Path {
case fmt.Sprintf("/api/v2/users/me/workspace/%s", taskName):
httpapi.Write(ctx, w, http.StatusOK, codersdk.Workspace{
ID: taskID,
})
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{taskID.String(), "--output", "json"},
expectLogs: taskLogs,
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{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,
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v2/users/me/workspace/doesnotexist":
httpapi.ResourceNotFound(w)
default:
t.Errorf("unexpected path: %s", r.URL.Path)
}
}
},
},
{
args: []string{uuid.Nil.String()}, // uuid does not exist
expectError: httpapi.ResourceNotFoundResponse.Message,
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", uuid.Nil.String()):
httpapi.ResourceNotFound(w)
default:
t.Errorf("unexpected path: %s", r.URL.Path)
}
}
},
},
{
args: []string{"err-fetching-logs"},
expectError: assert.AnError.Error(),
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v2/users/me/workspace/err-fetching-logs":
httpapi.Write(ctx, w, http.StatusOK, codersdk.Workspace{
ID: taskID,
})
case fmt.Sprintf("/api/experimental/tasks/me/%s/logs", taskID.String()):
httpapi.InternalServerError(w, assert.AnError)
default:
t.Errorf("unexpected path: %s", r.URL.Path)
}
}
},
},
}

for _, tt := range tests {
t.Run(strings.Join(tt.args, ","), func(t *testing.T) {
t.Parallel()

var (
ctx = testutil.Context(t, testutil.WaitShort)
srv = httptest.NewServer(tt.handler(t, ctx))
client = codersdk.New(testutil.MustURL(t, srv.URL))
args = []string{"exp", "task", "logs"}
stdout strings.Builder
err error
)

t.Cleanup(srv.Close)

inv, root := clitest.New(t, append(args, tt.args...)...)
inv.Stdout = &stdout
inv.Stderr = &stdout
clitest.SetupConfig(t, client, root)

err = inv.WithContext(ctx).Run()
if tt.expectError == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tt.expectError)
}

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

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, logs)
}
})
}
}
8 changes: 4 additions & 4 deletionscodersdk/aitasks.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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
Loading

[8]ページ先頭

©2009-2025 Movatter.jp