From fc692d292261c980496b41fe43dad2da4751b40b Mon Sep 17 00:00:00 2001 From: waseem-medhat Date: Wed, 5 Aug 2026 19:11:24 +0300 Subject: [PATCH] run or submit without uuid --- client/lessons.go | 34 +++++++++++++++++++++++++++++ cmd/run.go | 6 +++--- cmd/submit.go | 54 +++++++++++++++++++++++++++++++++++++++++------ version.txt | 2 +- 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/client/lessons.go b/client/lessons.go index 4865887..d5bbf05 100644 --- a/client/lessons.go +++ b/client/lessons.go @@ -1,6 +1,7 @@ package api import ( + "errors" "fmt" "github.com/goccy/go-json" @@ -9,8 +10,11 @@ import ( type Lesson struct { Lesson struct { Type string + Title string LessonDataCLI *LessonDataCLI } + ChapterNumber int + LessonNumber int } type LessonDataCLI struct { @@ -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 diff --git a/cmd/run.go b/cmd/run.go index 9e27fb0..0cadb87 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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, } diff --git a/cmd/submit.go b/cmd/submit.go index 58ad901..21b3c7c 100644 --- a/cmd/submit.go +++ b/cmd/submit.go @@ -31,9 +31,9 @@ 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, } @@ -41,12 +41,52 @@ var submitCmd = &cobra.Command{ 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") } diff --git a/version.txt b/version.txt index 46aa51b..d3aa769 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.30.1 +v1.31.0