- Notifications
You must be signed in to change notification settings - Fork1k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -13,11 +13,12 @@ func (r *RootCmd) tasksCommand() *serpent.Command { | ||
return i.Command.HelpHandler(i) | ||
}, | ||
Children: []*serpent.Command{ | ||
r.taskCreate(), | ||
r.taskDelete(), | ||
r.taskList(), | ||
r.taskLogs(), | ||
r.taskSend(), | ||
r.taskStatus(), | ||
}, | ||
Comment on lines 15 to 22 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. drive-by: sort | ||
} | ||
return cmd | ||
Original file line number | Diff line number | Diff 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. suggestion: at this point, we can probably use some kind of helper There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Original file line number | Diff line number | Diff 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) | ||
} | ||
}) | ||
} | ||
} |
Uh oh!
There was an error while loading.Please reload this page.