From 2c5ca7316df5a2235030172444211fa8e23b9f24 Mon Sep 17 00:00:00 2001 From: MaurĂ­cio Antunes Date: Fri, 18 Nov 2022 08:37:17 -0300 Subject: refactor: make commands to be configurable via root cmd --- golings/cmd/hint.go | 26 ++++++++------- golings/cmd/root.go | 4 +-- golings/cmd/verify.go | 90 ++++++++++++++++++++++++++------------------------- 3 files changed, 62 insertions(+), 58 deletions(-) diff --git a/golings/cmd/hint.go b/golings/cmd/hint.go index e0b3f08..aaa27aa 100644 --- a/golings/cmd/hint.go +++ b/golings/cmd/hint.go @@ -8,16 +8,18 @@ import ( "github.com/spf13/cobra" ) -var cmdHint = &cobra.Command{ - Use: "hint", - Short: "Get a hint for an exercise", - Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), - Run: func(cmd *cobra.Command, args []string) { - exercise, err := exercises.Find(args[0], "info.toml") - if err != nil { - color.Red(err.Error()) - os.Exit(1) - } - color.Yellow(exercise.Hint) - }, +func HintCmd(infoFile string) *cobra.Command { + return &cobra.Command{ + Use: "hint", + Short: "Get a hint for an exercise", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Run: func(cmd *cobra.Command, args []string) { + exercise, err := exercises.Find(args[0], "info.toml") + if err != nil { + color.Red(err.Error()) + os.Exit(1) + } + color.Yellow(exercise.Hint) + }, + } } diff --git a/golings/cmd/root.go b/golings/cmd/root.go index c0e08af..4f79311 100644 --- a/golings/cmd/root.go +++ b/golings/cmd/root.go @@ -15,10 +15,10 @@ func NewRootCmd(version string) *cobra.Command { Version: version, } - rootCmd.AddCommand(cmdHint) + rootCmd.AddCommand(HintCmd("info.toml")) rootCmd.AddCommand(ListCmd("info.toml")) rootCmd.AddCommand(RunCmd("info.toml")) - rootCmd.AddCommand(cmdVerify) + rootCmd.AddCommand(VerifyCmd("info.toml")) return rootCmd } diff --git a/golings/cmd/verify.go b/golings/cmd/verify.go index 6e1e341..0e020e2 100644 --- a/golings/cmd/verify.go +++ b/golings/cmd/verify.go @@ -10,54 +10,56 @@ import ( "github.com/spf13/cobra" ) -var cmdVerify = &cobra.Command{ - Use: "verify", - Short: "Verify all exercises", - Run: func(cmd *cobra.Command, args []string) { - exs, err := exercises.List("info.toml") - if err != nil { - color.Red(err.Error()) - os.Exit(1) - } - - bar := progressbar.NewOptions( - len(exs), - 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: "]", - }), - ) - if err := bar.RenderBlank(); err != nil { - color.Red(err.Error()) - os.Exit(1) - } - - for _, e := range exs { - bar.Describe(fmt.Sprintf("Running %s", e.Name)) - result, _ := exercises.Run(e.Name, "info.toml") - if err := bar.Add(1); err != nil { +func VerifyCmd(infoFile string) *cobra.Command { + return &cobra.Command{ + Use: "verify", + Short: "Verify all exercises", + Run: func(cmd *cobra.Command, args []string) { + exs, err := exercises.List(infoFile) + if err != nil { color.Red(err.Error()) os.Exit(1) } - if result.Err != "" { - fmt.Print("\n\n") - color.Cyan("Failed to compile the exercise %s\n\n", e.Path) - color.White("Check the output below: \n\n") - color.Red(result.Err) - color.Red(result.Out) + + bar := progressbar.NewOptions( + len(exs), + 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: "]", + }), + ) + if err := bar.RenderBlank(); err != nil { + color.Red(err.Error()) os.Exit(1) } - } - color.Green("Congratulations!!!") - color.Green("You passed all the exercises") - }, + for _, e := range exs { + bar.Describe(fmt.Sprintf("Running %s", e.Name)) + result, _ := exercises.Run(e.Name, "info.toml") + if err := bar.Add(1); err != nil { + color.Red(err.Error()) + os.Exit(1) + } + if result.Err != "" { + fmt.Print("\n\n") + color.Cyan("Failed to compile the exercise %s\n\n", e.Path) + color.White("Check the output below: \n\n") + color.Red(result.Err) + color.Red(result.Out) + os.Exit(1) + } + } + + color.Green("Congratulations!!!") + color.Green("You passed all the exercises") + }, + } } -- cgit v1.2.3-70-g09d2