Skip to content

Commit c120a5e

Browse files
lukevmorrisclaude
andcommitted
feat: add depot ci logs subcommand
Adds a new command to fetch and display log output for a CI job attempt. Paginates through all available log pages via the GetJobAttemptLogs RPC. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0d59d49 commit c120a5e

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

pkg/api/ci.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ func CIGetRunStatus(ctx context.Context, token, runID string) (*civ1.GetRunStatu
2626
return resp.Msg, nil
2727
}
2828

29+
// CIGetJobAttemptLogs returns all log lines for a job attempt, paginating through all pages.
30+
func CIGetJobAttemptLogs(ctx context.Context, token, attemptID string) ([]*civ1.LogLine, error) {
31+
client := newCIServiceClient()
32+
var allLines []*civ1.LogLine
33+
var pageToken string
34+
35+
for {
36+
req := &civ1.GetJobAttemptLogsRequest{AttemptId: attemptID, PageToken: pageToken}
37+
resp, err := client.GetJobAttemptLogs(ctx, WithAuthentication(connect.NewRequest(req), token))
38+
if err != nil {
39+
return nil, err
40+
}
41+
allLines = append(allLines, resp.Msg.Lines...)
42+
if resp.Msg.NextPageToken == "" {
43+
break
44+
}
45+
pageToken = resp.Msg.NextPageToken
46+
}
47+
48+
return allLines, nil
49+
}
50+
2951
func newCISecretServiceClient() civ1connect.SecretServiceClient {
3052
baseURL := baseURLFunc()
3153
return civ1connect.NewSecretServiceClient(getHTTPClient(baseURL), baseURL, WithUserAgent())

pkg/cmd/ci/ci.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func NewCmdCI() *cobra.Command {
1212
},
1313
}
1414

15+
cmd.AddCommand(NewCmdLogs())
1516
cmd.AddCommand(NewCmdMigrate())
1617
cmd.AddCommand(NewCmdSecrets())
1718
cmd.AddCommand(NewCmdStatus())

pkg/cmd/ci/logs.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ci
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/depot/cli/pkg/api"
7+
"github.com/depot/cli/pkg/helpers"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
func NewCmdLogs() *cobra.Command {
12+
var token string
13+
14+
cmd := &cobra.Command{
15+
Use: "logs <attempt-id>",
16+
Short: "Fetch logs for a CI job attempt [beta]",
17+
Long: "Fetch and display log output for a CI job attempt.\n\nThis command is in beta and subject to change.",
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
if len(args) == 0 {
20+
return cmd.Help()
21+
}
22+
23+
ctx := cmd.Context()
24+
attemptID := args[0]
25+
26+
tokenVal, err := helpers.ResolveOrgAuth(ctx, token)
27+
if err != nil {
28+
return err
29+
}
30+
if tokenVal == "" {
31+
return fmt.Errorf("missing API token, please run `depot login`")
32+
}
33+
34+
lines, err := api.CIGetJobAttemptLogs(ctx, tokenVal, attemptID)
35+
if err != nil {
36+
return fmt.Errorf("failed to get job attempt logs: %w", err)
37+
}
38+
39+
for _, line := range lines {
40+
fmt.Println(line.Body)
41+
}
42+
43+
return nil
44+
},
45+
}
46+
47+
cmd.Flags().StringVar(&token, "token", "", "Depot API token")
48+
49+
return cmd
50+
}

0 commit comments

Comments
 (0)