Skip to content

Commit a2b8710

Browse files
committed
run or submit without uuid
1 parent 1f96099 commit a2b8710

4 files changed

Lines changed: 85 additions & 11 deletions

File tree

client/lessons.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/goccy/go-json"
@@ -9,8 +10,11 @@ import (
910
type Lesson struct {
1011
Lesson struct {
1112
Type string
13+
Title string
1214
LessonDataCLI *LessonDataCLI
1315
}
16+
ChapterNumber int
17+
LessonNumber int
1418
}
1519

1620
type LessonDataCLI struct {
@@ -174,6 +178,36 @@ func FetchLesson(uuid string) (*Lesson, error) {
174178
return &data, nil
175179
}
176180

181+
type NextCLILesson struct {
182+
LessonUUID string
183+
LessonTitle string
184+
ChapterTitle string
185+
ChapterNumber int
186+
LessonNumber int
187+
}
188+
189+
func FetchNextCLILesson() (*NextCLILesson, error) {
190+
resp, code, err := fetchWithAuthAndPayload("GET", "/v1/lessons/next_cli", []byte{})
191+
if err != nil {
192+
return nil, err
193+
}
194+
if code != 200 {
195+
var errResp struct {
196+
Error string `json:"error"`
197+
}
198+
if err := json.Unmarshal(resp, &errResp); err == nil && errResp.Error != "" {
199+
return nil, errors.New(errResp.Error)
200+
}
201+
return nil, fmt.Errorf("failed to detect your next lesson (code %v): %s", code, string(resp))
202+
}
203+
204+
var data NextCLILesson
205+
if err := json.Unmarshal(resp, &data); err != nil {
206+
return nil, err
207+
}
208+
return &data, nil
209+
}
210+
177211
type CLIStepResult struct {
178212
CLICommandResult *CLICommandResult
179213
HTTPRequestResult *HTTPRequestResult

cmd/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func init() {
1313

1414
// runCmd represents the run command
1515
var runCmd = &cobra.Command{
16-
Use: "run UUID",
17-
Args: cobra.MatchAll(cobra.RangeArgs(1, 10)),
18-
Short: "Run a lesson without submitting",
16+
Use: "run [UUID]",
17+
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
18+
Short: "Run a lesson without submitting. Runs your next lesson when no UUID is given",
1919
PreRun: compose(requireUpdated, requireAuth),
2020
RunE: submissionHandler,
2121
}

cmd/submit.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,62 @@ func init() {
3131

3232
// submitCmd represents the submit command
3333
var submitCmd = &cobra.Command{
34-
Use: "submit UUID",
35-
Args: cobra.MatchAll(cobra.RangeArgs(1, 10)),
36-
Short: "Submit a lesson",
34+
Use: "submit [UUID]",
35+
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
36+
Short: "Submit a lesson. Submits your next lesson when no UUID is given",
3737
PreRun: compose(requireUpdated, requireAuth),
3838
RunE: submissionHandler,
3939
}
4040

4141
func submissionHandler(cmd *cobra.Command, args []string) error {
4242
cmd.SilenceUsage = true
4343
isSubmit := cmd.Name() == "submit" || forceSubmit
44-
lessonUUID := args[0]
4544

46-
lesson, err := api.FetchLesson(lessonUUID)
47-
if err != nil {
48-
return err
45+
var lesson *api.Lesson
46+
var lessonUUID string
47+
if len(args) > 0 {
48+
lessonUUID = args[0]
49+
fetchedLesson, err := api.FetchLesson(lessonUUID)
50+
if err != nil {
51+
return err
52+
}
53+
action := "Running"
54+
if isSubmit {
55+
action = "Submitting"
56+
}
57+
fmt.Printf(
58+
"%s lesson:\n%d.%d - %s\n",
59+
action,
60+
fetchedLesson.ChapterNumber,
61+
fetchedLesson.LessonNumber,
62+
fetchedLesson.Lesson.Title,
63+
)
64+
lesson = fetchedLesson
65+
} else {
66+
nextLesson, err := api.FetchNextCLILesson()
67+
if err != nil {
68+
return err
69+
}
70+
lessonLabel := fmt.Sprintf("%d.%d - %s", nextLesson.ChapterNumber, nextLesson.LessonNumber, nextLesson.LessonTitle)
71+
if isSubmit {
72+
if !promptToContinue(
73+
"Submitting next lesson:",
74+
lessonLabel,
75+
"Continue?",
76+
) {
77+
return errors.New("submission canceled")
78+
}
79+
} else {
80+
fmt.Printf("Running next lesson:\n%s\n", lessonLabel)
81+
}
82+
lessonUUID = nextLesson.LessonUUID
83+
fetchedLesson, err := api.FetchLesson(lessonUUID)
84+
if err != nil {
85+
return err
86+
}
87+
lesson = fetchedLesson
4988
}
89+
5090
if lesson.Lesson.Type != "type_cli" {
5191
return errors.New("unable to run lesson: unsupported lesson type")
5292
}

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.30.1
1+
v1.31.0

0 commit comments

Comments
 (0)