aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMaurĂ­cio Antunes <mauricio.abreua@gmail.com>2022-10-31 07:26:26 -0300
committerMaurĂ­cio Antunes <mauricio.abreua@gmail.com>2022-10-31 07:26:26 -0300
commit633f40fded8e7e3c03dc661a27557d8c4a45b1a9 (patch)
treea9bed6e642cff0ce5ed69ce23dad6ba24ab0aa93 /src
parent41d7496de97e7c31584887ca4d6ed00aeb7d3530 (diff)
feat: improve exercise runner output
Diffstat (limited to 'src')
-rw-r--r--src/cmd/run.go16
-rw-r--r--src/exercises/runner.go20
2 files changed, 25 insertions, 11 deletions
diff --git a/src/cmd/run.go b/src/cmd/run.go
index fb62f67..300478f 100644
--- a/src/cmd/run.go
+++ b/src/cmd/run.go
@@ -1,7 +1,6 @@
package cmd
import (
- "fmt"
"os"
"github.com/fatih/color"
@@ -18,14 +17,17 @@ var cmdRun = &cobra.Command{
Short: "Run a single exercise",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
- out, err := exercises.Run(args[0])
- fmt.Println(out)
+ result, err := exercises.Run(args[0])
if err != nil {
- color.Red("Your exercise is failing: %s", err)
+ 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)
}
- color.Green("Congratulations!")
- color.Green("Remove the 'I AM NOT DONE' from the file to keep going")
- os.Exit(0)
},
}
diff --git a/src/exercises/runner.go b/src/exercises/runner.go
index 1490f13..d034093 100644
--- a/src/exercises/runner.go
+++ b/src/exercises/runner.go
@@ -1,17 +1,29 @@
package exercises
import (
+ "bytes"
"fmt"
"os/exec"
)
-func Run(name string) (string, error) {
+type Result struct {
+ Exercise Exercise
+ Out string
+ Err string
+}
+
+func Run(name string) (Result, error) {
exercise, err := Find(name)
if err != nil {
- return "", err
+ return Result{}, err
}
cmd := exec.Command("go", "run", fmt.Sprintf("./%s", exercise.Path))
- cOut, err := cmd.CombinedOutput()
- return string(cOut), err
+ 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
}