Skip to content

Commit 9d7b603

Browse files
add debug flag for submit
1 parent 9eadd9f commit 9d7b603

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

client/lessons.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ type lessonSubmissionCLI struct {
191191
CLIResults []CLIStepResult
192192
}
193193

194+
type SubmissionDebugData struct {
195+
Endpoint string
196+
RequestBody string
197+
ResponseStatusCode int
198+
ResponseBody string
199+
}
200+
194201
type LessonSubmissionEvent struct {
195202
ResultSlug VerificationResultSlug
196203
StructuredErrCLI *StructuredErrCLI
@@ -213,26 +220,37 @@ const (
213220
)
214221

215222
func SubmitCLILesson(uuid string, results []CLIStepResult) (LessonSubmissionEvent, error) {
223+
result, _, err := SubmitCLILessonWithDebug(uuid, results)
224+
return result, err
225+
}
226+
227+
func SubmitCLILessonWithDebug(uuid string, results []CLIStepResult) (LessonSubmissionEvent, SubmissionDebugData, error) {
228+
endpoint := fmt.Sprintf("/v1/lessons/%v/", uuid)
229+
debug := SubmissionDebugData{Endpoint: endpoint}
230+
216231
bytes, err := json.Marshal(lessonSubmissionCLI{CLIResults: results})
217232
if err != nil {
218-
return LessonSubmissionEvent{}, err
233+
return LessonSubmissionEvent{}, debug, err
219234
}
220-
endpoint := fmt.Sprintf("/v1/lessons/%v/", uuid)
235+
debug.RequestBody = string(bytes)
236+
221237
resp, code, err := fetchWithAuthAndPayload("POST", endpoint, bytes)
238+
debug.ResponseStatusCode = code
239+
debug.ResponseBody = string(resp)
222240
if err != nil {
223-
return LessonSubmissionEvent{}, err
241+
return LessonSubmissionEvent{}, debug, err
224242
}
225243
if code == 402 {
226-
return LessonSubmissionEvent{}, fmt.Errorf("to run and submit the tests for this lesson, you must have an active Boot.dev membership\nhttps://boot.dev/pricing")
244+
return LessonSubmissionEvent{}, debug, fmt.Errorf("to run and submit the tests for this lesson, you must have an active Boot.dev membership\nhttps://boot.dev/pricing")
227245
}
228246
if code != 200 {
229-
return LessonSubmissionEvent{}, fmt.Errorf("failed to submit CLI lesson (code %v): %s", code, string(resp))
247+
return LessonSubmissionEvent{}, debug, fmt.Errorf("failed to submit CLI lesson (code %v): %s", code, string(resp))
230248
}
231249

232250
result := LessonSubmissionEvent{}
233251
err = json.Unmarshal(resp, &result)
234252
if err != nil {
235-
return LessonSubmissionEvent{}, err
253+
return LessonSubmissionEvent{}, debug, err
236254
}
237-
return result, nil
255+
return result, debug, nil
238256
}

cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
func init() {
88
rootCmd.AddCommand(runCmd)
99
runCmd.Flags().BoolVarP(&forceSubmit, "submit", "s", false, "shortcut flag to submit after running")
10+
runCmd.Flags().BoolVar(&debugSubmission, "debug", false, "log submission request/response debug output")
1011
}
1112

1213
// runCmd represents the run command

cmd/submit.go

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"runtime"
9+
"time"
710

811
"github.com/bootdotdev/bootdev/checks"
912
api "github.com/bootdotdev/bootdev/client"
@@ -14,10 +17,14 @@ import (
1417
"github.com/spf13/viper"
1518
)
1619

17-
var forceSubmit bool
20+
var (
21+
forceSubmit bool
22+
debugSubmission bool
23+
)
1824

1925
func init() {
2026
rootCmd.AddCommand(submitCmd)
27+
submitCmd.Flags().BoolVar(&debugSubmission, "debug", false, "log submission request/response debug output")
2128
}
2229

2330
// submitCmd represents the submit command
@@ -71,7 +78,20 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
7178
cliResults := checks.CLIChecks(data, overrideBaseURL, ch)
7279

7380
if isSubmit {
74-
submissionEvent, err := api.SubmitCLILesson(lessonUUID, cliResults)
81+
var submissionEvent api.LessonSubmissionEvent
82+
if debugSubmission {
83+
var debugPath string
84+
var debugWriteErr error
85+
defer func() {
86+
reportDebugFileWrite(debugPath, debugWriteErr)
87+
}()
88+
89+
var debugData api.SubmissionDebugData
90+
submissionEvent, debugData, err = api.SubmitCLILessonWithDebug(lessonUUID, cliResults)
91+
debugPath, debugWriteErr = writeSubmissionDebugFile(lessonUUID, debugData)
92+
} else {
93+
submissionEvent, err = api.SubmitCLILesson(lessonUUID, cliResults)
94+
}
7595
if err != nil {
7696
return err
7797
}
@@ -82,3 +102,42 @@ func submissionHandler(cmd *cobra.Command, args []string) error {
82102
}
83103
return nil
84104
}
105+
106+
func reportDebugFileWrite(path string, err error) {
107+
if err != nil {
108+
fmt.Fprintf(os.Stderr, "warning: failed to write submission debug output: %v\n", err)
109+
return
110+
}
111+
fmt.Fprintf(os.Stderr, "Submission debug output written to %s\n", path)
112+
}
113+
114+
func writeSubmissionDebugFile(lessonUUID string, data api.SubmissionDebugData) (string, error) {
115+
now := time.Now()
116+
timestamp := now.Format("20060102-150405")
117+
filename := fmt.Sprintf("bootdev-submit-debug-%s-%s.txt", lessonUUID, timestamp)
118+
status := "unavailable"
119+
if data.ResponseStatusCode != 0 {
120+
status = fmt.Sprintf("%d", data.ResponseStatusCode)
121+
}
122+
123+
contents := fmt.Sprintf(
124+
"bootdev submit debug\nTimestamp: %s\nLesson UUID: %s\nEndpoint: %s\n\n=== Request JSON ===\n%s\n\n=== Response ===\nStatus Code: %s\n%s\n",
125+
now.Format(time.RFC3339),
126+
lessonUUID,
127+
data.Endpoint,
128+
data.RequestBody,
129+
status,
130+
data.ResponseBody,
131+
)
132+
133+
if err := os.WriteFile(filename, []byte(contents), 0o600); err != nil {
134+
return "", err
135+
}
136+
137+
absPath, err := filepath.Abs(filename)
138+
if err != nil {
139+
return filename, nil
140+
}
141+
142+
return absPath, nil
143+
}

0 commit comments

Comments
 (0)