Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions client/lessons.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"errors"
"fmt"

"github.com/goccy/go-json"
Expand All @@ -9,8 +10,11 @@ import (
type Lesson struct {
Lesson struct {
Type string
Title string
LessonDataCLI *LessonDataCLI
}
ChapterNumber int
LessonNumber int
}

type LessonDataCLI struct {
Expand Down Expand Up @@ -174,6 +178,36 @@ func FetchLesson(uuid string) (*Lesson, error) {
return &data, nil
}

type NextCLILesson struct {
LessonUUID string
LessonTitle string
ChapterTitle string
ChapterNumber int
LessonNumber int
}

func FetchNextCLILesson() (*NextCLILesson, error) {
resp, code, err := fetchWithAuthAndPayload("GET", "/v1/lessons/next_cli", []byte{})
if err != nil {
return nil, err
}
if code != 200 {
var errResp struct {
Error string `json:"error"`
}
if err := json.Unmarshal(resp, &errResp); err == nil && errResp.Error != "" {
return nil, errors.New(errResp.Error)
}
return nil, fmt.Errorf("failed to detect your next lesson (code %v): %s", code, string(resp))
}

var data NextCLILesson
if err := json.Unmarshal(resp, &data); err != nil {
return nil, err
}
return &data, nil
}

type CLIStepResult struct {
CLICommandResult *CLICommandResult
HTTPRequestResult *HTTPRequestResult
Expand Down
6 changes: 3 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func init() {

// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run UUID",
Args: cobra.MatchAll(cobra.RangeArgs(1, 10)),
Short: "Run a lesson without submitting",
Use: "run [UUID]",
Args: cobra.MatchAll(cobra.MaximumNArgs(1)),
Short: "Run a lesson without submitting. Runs your next lesson when no UUID is given",
PreRun: compose(requireUpdated, requireAuth),
RunE: submissionHandler,
}
54 changes: 47 additions & 7 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,62 @@ func init() {

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

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

lesson, err := api.FetchLesson(lessonUUID)
if err != nil {
return err
var lesson *api.Lesson
var lessonUUID string
if len(args) > 0 {
lessonUUID = args[0]
fetchedLesson, err := api.FetchLesson(lessonUUID)
if err != nil {
return err
}
action := "Running"
if isSubmit {
action = "Submitting"
}
fmt.Printf(
"%s lesson:\n%d.%d - %s\n",
action,
fetchedLesson.ChapterNumber,
fetchedLesson.LessonNumber,
fetchedLesson.Lesson.Title,
)
lesson = fetchedLesson
} else {
nextLesson, err := api.FetchNextCLILesson()
if err != nil {
return err
}
lessonLabel := fmt.Sprintf("%d.%d - %s", nextLesson.ChapterNumber, nextLesson.LessonNumber, nextLesson.LessonTitle)
if isSubmit {
if !promptToContinue(
"Submitting next lesson:",
lessonLabel,
"Continue?",
) {
return errors.New("submission canceled")
}
} else {
fmt.Printf("Running next lesson:\n%s\n", lessonLabel)
}
lessonUUID = nextLesson.LessonUUID
fetchedLesson, err := api.FetchLesson(lessonUUID)
if err != nil {
return err
}
lesson = fetchedLesson
}

if lesson.Lesson.Type != "type_cli" {
return errors.New("unable to run lesson: unsupported lesson type")
}
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.30.1
v1.31.0