aboutsummaryrefslogtreecommitdiffstats
path: root/golings/cmd/verify.go
blob: b240c94a06b062e3a41f0553ef634ed692eaf5f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cmd

import (
	"fmt"
	"os"

	"github.com/fatih/color"
	"github.com/mauricioabreu/golings/golings/exercises"
	"github.com/schollz/progressbar/v3"
	"github.com/spf13/cobra"
)

func init() {
	rootCmd.AddCommand(cmdVerify)
}

var cmdVerify = &cobra.Command{
	Use:   "verify",
	Short: "Verify all exercises",
	Run: func(cmd *cobra.Command, args []string) {
		verified, err := exercises.Verify()
		if err != nil {
			color.Red(err.Error())
			os.Exit(1)
		}

		bar := progressbar.NewOptions(
			verified.Total,
			progressbar.OptionSetWidth(50),
			progressbar.OptionEnableColorCodes(true),
			progressbar.OptionSetPredictTime(false),
			progressbar.OptionSetElapsedTime(false),
			progressbar.OptionSetDescription("[cyan][reset] Running exercises"),
			progressbar.OptionSetTheme(progressbar.Theme{
				Saucer:        "[yellow]=[reset]",
				SaucerHead:    "[yellow]>[reset]",
				SaucerPadding: " ",
				BarStart:      "[",
				BarEnd:        "]",
			}),
		)
		bar.RenderBlank()

		for v := range verified.Verified {
			bar.Describe(fmt.Sprintf("Running %s", v.Exercise.Name))
			bar.Add(1)
			if v.Err != "" {
				fmt.Print("\n\n")
				color.Cyan("Failed to compile the exercise %s\n\n", v.Exercise.Path)
				color.White("Check the error below: \n\n")
				color.Red(v.Err)
				os.Exit(1)
			}
		}

		color.Green("Congratulations!!!")
		color.Green("You passed all the exercises")
	},
}