|
| 1 | +package trainings |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/ThreeDotsLabs/cli/trainings/config" |
| 8 | + "github.com/ThreeDotsLabs/cli/trainings/files" |
| 9 | + "github.com/ThreeDotsLabs/cli/trainings/genproto" |
| 10 | + "github.com/manifoldco/promptui" |
| 11 | + "github.com/mergestat/timediff" |
| 12 | + "github.com/pkg/errors" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | +) |
| 15 | + |
| 16 | +func (h *Handlers) Checkout(ctx context.Context) error { |
| 17 | + trainingRoot, err := h.config.FindTrainingRoot() |
| 18 | + if errors.Is(err, config.TrainingRootNotFoundError) { |
| 19 | + h.printNotInATrainingDirectory() |
| 20 | + return nil |
| 21 | + } |
| 22 | + |
| 23 | + trainingRootFs := newTrainingRootFs(trainingRoot) |
| 24 | + |
| 25 | + resp, err := h.newGrpcClient().GetSolutions(ctx, &genproto.GetSolutionsRequest{ |
| 26 | + ExerciseId: h.config.ExerciseConfig(trainingRootFs).ExerciseID, |
| 27 | + Token: h.config.GlobalConfig().Token, |
| 28 | + }) |
| 29 | + if err != nil { |
| 30 | + return fmt.Errorf("failed to get solutions: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + logrus.WithFields(logrus.Fields{ |
| 34 | + "resp": resp, |
| 35 | + "err": err, |
| 36 | + }).Debug("Received solutions from server") |
| 37 | + |
| 38 | + items := []string{"(cancel)"} |
| 39 | + for _, solution := range resp.Solutions { |
| 40 | + text := "" |
| 41 | + if solution.Successful { |
| 42 | + text += "✅" |
| 43 | + } else { |
| 44 | + text += "❌" |
| 45 | + } |
| 46 | + text += " " |
| 47 | + text += solution.VerificationId |
| 48 | + text += " " |
| 49 | + text += timediff.TimeDiff(solution.ExecutedAt.AsTime()) |
| 50 | + |
| 51 | + items = append(items, text) |
| 52 | + } |
| 53 | + |
| 54 | + selectUI := promptui.Select{ |
| 55 | + Label: "Select a solution to checkout", |
| 56 | + Items: items, |
| 57 | + Size: 10, |
| 58 | + Templates: &promptui.SelectTemplates{ |
| 59 | + Label: "{{ . }}", |
| 60 | + Active: "{{ . | cyan }}", |
| 61 | + Inactive: "{{ . }}", |
| 62 | + }, |
| 63 | + HideSelected: true, |
| 64 | + } |
| 65 | + |
| 66 | + index, _, err := selectUI.Run() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + if index == 0 { |
| 72 | + fmt.Println("Cancelled") |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + getResp, err := h.newGrpcClient().GetSolutionFiles(ctx, &genproto.GetSolutionFilesRequest{ |
| 77 | + ExecutionId: resp.Solutions[index-1].VerificationId, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to get solution files: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + if err := h.writeExerciseFiles(files.NewFilesWithConfig(true, true), getSolutionFilesToExerciseSolution(getResp), trainingRootFs); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + err = addModuleToWorkspace(trainingRoot, getResp.Dir) |
| 88 | + if err != nil { |
| 89 | + logrus.WithError(err).Warn("Failed to add module to workspace") |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
0 commit comments