()
| 11 | ) |
| 12 | |
| 13 | func (r *RootCmd) taskLogs() *serpent.Command { |
| 14 | formatter := cliui.NewOutputFormatter( |
| 15 | cliui.TableFormat( |
| 16 | []codersdk.TaskLogEntry{}, |
| 17 | []string{ |
| 18 | "type", |
| 19 | "content", |
| 20 | }, |
| 21 | ), |
| 22 | cliui.JSONFormat(), |
| 23 | ) |
| 24 | |
| 25 | cmd := &serpent.Command{ |
| 26 | Use: "logs <task>", |
| 27 | Short: "Show a task's logs", |
| 28 | Long: FormatExamples( |
| 29 | Example{ |
| 30 | Description: "Show logs for a given task.", |
| 31 | Command: "coder task logs task1", |
| 32 | }), |
| 33 | Middleware: serpent.Chain( |
| 34 | serpent.RequireNArgs(1), |
| 35 | ), |
| 36 | Handler: func(inv *serpent.Invocation) error { |
| 37 | client, err := r.InitClient(inv) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | |
| 42 | var ( |
| 43 | ctx = inv.Context() |
| 44 | identifier = inv.Args[0] |
| 45 | ) |
| 46 | |
| 47 | task, err := client.TaskByIdentifier(ctx, identifier) |
| 48 | if err != nil { |
| 49 | return xerrors.Errorf("resolve task %q: %w", identifier, err) |
| 50 | } |
| 51 | |
| 52 | logs, err := client.TaskLogs(ctx, codersdk.Me, task.ID) |
| 53 | if err != nil { |
| 54 | return xerrors.Errorf("get task logs: %w", err) |
| 55 | } |
| 56 | |
| 57 | // Handle snapshot responses (paused/initializing/pending tasks). |
| 58 | if logs.Snapshot { |
| 59 | if logs.SnapshotAt == nil { |
| 60 | // No snapshot captured yet. |
| 61 | cliui.Warnf(inv.Stderr, |
| 62 | "Task is %s. No snapshot available (snapshot may have failed during pause, resume your task to view logs).\n", |
| 63 | task.Status) |
| 64 | } |
| 65 | |
| 66 | // Snapshot exists with logs, show warning with count. |
| 67 | if len(logs.Logs) > 0 { |
| 68 | if len(logs.Logs) == 1 { |
| 69 | cliui.Warnf(inv.Stderr, "Task is %s. Showing last 1 message from snapshot.\n", task.Status) |
| 70 | } else { |
no test coverage detected