MCPcopy Index your code
hub / github.com/coder/coder / logs

Method logs

cli/logs.go:19–96  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17)
18
19func (r *RootCmd) logs() *serpent.Command {
20 var (
21 buildNumberArg int64
22 followArg bool
23 )
24 cmd := &serpent.Command{
25 Use: "logs <workspace>",
26 Short: "View logs for a workspace",
27 Long: "View logs for a workspace",
28 Middleware: serpent.Chain(
29 serpent.RequireNArgs(1),
30 ),
31 Options: serpent.OptionSet{
32 {
33 Name: "Build Number",
34 Flag: "build-number",
35 FlagShorthand: "n",
36 Description: "Only show logs for a specific build number. Defaults to 0, which maps to the most recent build (build numbers start at 1). Negative values are treated as offsets—for example, -1 refers to the previous build.",
37 Value: serpent.Int64Of(&buildNumberArg),
38 Default: "0",
39 },
40 {
41 Name: "Follow",
42 Flag: "follow",
43 FlagShorthand: "f",
44 Description: "Follow logs as they are emitted.",
45 Value: serpent.BoolOf(&followArg),
46 Default: "false",
47 },
48 },
49 Handler: func(inv *serpent.Invocation) error {
50 ctx := inv.Context()
51 client, err := r.InitClient(inv)
52 if err != nil {
53 return err
54 }
55 ws, err := client.ResolveWorkspace(inv.Context(), inv.Args[0])
56 if err != nil {
57 return xerrors.Errorf("failed to get workspace: %w", err)
58 }
59 bld := ws.LatestBuild
60 buildNumber := buildNumberArg
61
62 // User supplied a negative build number, treat it as an offset from the latest build
63 if buildNumber < 0 {
64 buildNumber = int64(ws.LatestBuild.BuildNumber) + buildNumberArg
65 if buildNumber < 1 {
66 return xerrors.Errorf("invalid build number offset: %d latest build number: %d", buildNumberArg, ws.LatestBuild.BuildNumber)
67 }
68 }
69
70 // Fetch specific build if requested
71 if buildNumber > 0 {
72 wb, err := client.WorkspaceBuildByUsernameAndWorkspaceNameAndBuildNumber(ctx, ws.OwnerName, ws.Name, strconv.FormatInt(buildNumber, 10))
73 if err != nil {
74 return xerrors.Errorf("failed to get build %d: %w", buildNumberArg, err)
75 }
76 bld = wb

Callers 1

CoreSubcommandsMethod · 0.95

Calls 7

InitClientMethod · 0.95
InfofFunction · 0.92
workspaceLogsFunction · 0.85
ResolveWorkspaceMethod · 0.80
ContextMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected