diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/hint.go | 27 | ||||
-rw-r--r-- | src/cmd/list.go | 27 | ||||
-rw-r--r-- | src/cmd/root.go | 23 | ||||
-rw-r--r-- | src/cmd/run.go | 33 | ||||
-rw-r--r-- | src/exercises/exercise.go | 38 | ||||
-rw-r--r-- | src/exercises/exercises_suite_test.go | 53 | ||||
-rw-r--r-- | src/exercises/list.go | 44 | ||||
-rw-r--r-- | src/exercises/runner.go | 29 | ||||
-rw-r--r-- | src/main.go | 7 | ||||
-rw-r--r-- | src/printer/list.go | 18 |
10 files changed, 0 insertions, 299 deletions
diff --git a/src/cmd/hint.go b/src/cmd/hint.go deleted file mode 100644 index a315a91..0000000 --- a/src/cmd/hint.go +++ /dev/null @@ -1,27 +0,0 @@ -package cmd - -import ( - "os" - - "github.com/fatih/color" - "github.com/mauricioabreu/golings/src/exercises" - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(cmdHint) -} - -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]) - if err != nil { - color.Red(err.Error()) - os.Exit(1) - } - color.Yellow(exercise.Hint) - }, -} diff --git a/src/cmd/list.go b/src/cmd/list.go deleted file mode 100644 index 103ea21..0000000 --- a/src/cmd/list.go +++ /dev/null @@ -1,27 +0,0 @@ -package cmd - -import ( - "os" - - "github.com/fatih/color" - "github.com/mauricioabreu/golings/src/exercises" - "github.com/mauricioabreu/golings/src/printer" - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(cmdList) -} - -var cmdList = &cobra.Command{ - Use: "list", - Short: "List all exercises", - Run: func(cmd *cobra.Command, args []string) { - exs, err := exercises.List() - if err != nil { - color.Red(err.Error()) - os.Exit(1) - } - printer.PrintList(os.Stdout, exs) - }, -} diff --git a/src/cmd/root.go b/src/cmd/root.go deleted file mode 100644 index e0164a7..0000000 --- a/src/cmd/root.go +++ /dev/null @@ -1,23 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - - "github.com/spf13/cobra" -) - -var rootCmd = &cobra.Command{ - Use: "golings", - Short: "Learn go through interactive exercises", - Run: func(cmd *cobra.Command, args []string) { - - }, -} - -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) - } -} diff --git a/src/cmd/run.go b/src/cmd/run.go deleted file mode 100644 index 300478f..0000000 --- a/src/cmd/run.go +++ /dev/null @@ -1,33 +0,0 @@ -package cmd - -import ( - "os" - - "github.com/fatih/color" - "github.com/mauricioabreu/golings/src/exercises" - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(cmdRun) -} - -var cmdRun = &cobra.Command{ - Use: "run [exercise]", - Short: "Run a single exercise", - Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), - Run: func(cmd *cobra.Command, args []string) { - result, err := exercises.Run(args[0]) - if err != nil { - color.Red("Compilation of %s failed! Compiler error message:\n\n%s", result.Exercise.Path, result.Err) - color.Yellow("If you feel stuck, ask a hint by executing `golings hint %s`", result.Exercise.Name) - os.Exit(1) - } else { - color.Green("Congratulations!\n\n") - color.Green("Remove the 'I AM NOT DONE' from the file to keep going\n") - color.Green("Here is the output of your program:\n\n") - color.Cyan(result.Out) - os.Exit(0) - } - }, -} diff --git a/src/exercises/exercise.go b/src/exercises/exercise.go deleted file mode 100644 index 22700c8..0000000 --- a/src/exercises/exercise.go +++ /dev/null @@ -1,38 +0,0 @@ -package exercises - -import ( - "os" - "regexp" -) - -var notDoneRegex = regexp.MustCompile(`(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE`) - -type Exercise struct { - Name string - Path string - Mode string - Hint string -} - -func (e Exercise) State() State { - data, err := os.ReadFile(e.Path) - if err != nil { - return Pending - } - - if notDoneRegex.Match(data) { - return Pending - } - return Done -} - -type State int - -const ( - Pending State = iota + 1 - Done -) - -func (s State) String() string { - return [...]string{"Pending", "Done"}[s-1] -} diff --git a/src/exercises/exercises_suite_test.go b/src/exercises/exercises_suite_test.go deleted file mode 100644 index c916326..0000000 --- a/src/exercises/exercises_suite_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package exercises_test - -import ( - "os" - "testing" - - "github.com/mauricioabreu/golings/src/exercises" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -func TestExercises(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Exercises Suite") -} - -var _ = Describe("Exercises", func() { - Describe("Checking exercise state", func() { - When("'I AM NOT DONE' comment is still there", func() { - It("has the Pending state", func() { - file, err := os.CreateTemp("/tmp", "exercise*.go") - file.Write([]byte(`// exercise1.go - // I AM NOT DONE - package main - - func main() { - - } - `)) - Expect(err).To(BeNil()) - - defer os.Remove(file.Name()) - - ex := exercises.Exercise{Path: file.Name()} - - Expect(ex.State()).To(Equal(exercises.Pending)) - }) - }) - - When("'I AM NOT DONE' comment is not there", func() { - It("has the Done state", func() { - file, err := os.CreateTemp("/tmp", "exercise*.go") - Expect(err).To(BeNil()) - - defer os.Remove(file.Name()) - - ex := exercises.Exercise{Path: file.Name()} - - Expect(ex.State()).To(Equal(exercises.Done)) - }) - }) - }) -}) diff --git a/src/exercises/list.go b/src/exercises/list.go deleted file mode 100644 index c0c7261..0000000 --- a/src/exercises/list.go +++ /dev/null @@ -1,44 +0,0 @@ -package exercises - -import ( - "errors" - "os" - - "github.com/pelletier/go-toml/v2" -) - -var ErrExerciseNotFound = errors.New("exercise not found") - -type Info struct { - Exercises []Exercise -} - -func List() ([]Exercise, error) { - var info Info - - data, err := os.ReadFile("info.toml") - if err != nil { - return info.Exercises, err - } - - if err := toml.Unmarshal(data, &info); err != nil { - return info.Exercises, err - } - - return info.Exercises, nil -} - -func Find(exercise string) (Exercise, error) { - exs, err := List() - if err != nil { - return Exercise{}, err - } - - for _, ex := range exs { - if ex.Name == exercise { - return ex, nil - } - } - - return Exercise{}, ErrExerciseNotFound -} diff --git a/src/exercises/runner.go b/src/exercises/runner.go deleted file mode 100644 index d034093..0000000 --- a/src/exercises/runner.go +++ /dev/null @@ -1,29 +0,0 @@ -package exercises - -import ( - "bytes" - "fmt" - "os/exec" -) - -type Result struct { - Exercise Exercise - Out string - Err string -} - -func Run(name string) (Result, error) { - exercise, err := Find(name) - if err != nil { - return Result{}, err - } - - cmd := exec.Command("go", "run", fmt.Sprintf("./%s", exercise.Path)) - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - - err = cmd.Run() - - return Result{Exercise: exercise, Out: stdout.String(), Err: stderr.String()}, err -} diff --git a/src/main.go b/src/main.go deleted file mode 100644 index 3b64929..0000000 --- a/src/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "github.com/mauricioabreu/golings/src/cmd" - -func main() { - cmd.Execute() -} diff --git a/src/printer/list.go b/src/printer/list.go deleted file mode 100644 index cd38c9a..0000000 --- a/src/printer/list.go +++ /dev/null @@ -1,18 +0,0 @@ -package printer - -import ( - "io" - - "github.com/jedib0t/go-pretty/v6/table" - "github.com/mauricioabreu/golings/src/exercises" -) - -func PrintList(o io.Writer, exs []exercises.Exercise) { - t := table.NewWriter() - t.SetOutputMirror(o) - t.AppendHeader(table.Row{"Name", "Path", "State"}) - for _, ex := range exs { - t.AppendRow(table.Row{ex.Name, ex.Path, ex.State()}) - } - t.Render() -} |