aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cmd/root.go23
-rw-r--r--src/cmd/run.go22
-rw-r--r--src/exercises/runner.go9
-rw-r--r--src/main.go4
4 files changed, 57 insertions, 1 deletions
diff --git a/src/cmd/root.go b/src/cmd/root.go
new file mode 100644
index 0000000..e0164a7
--- /dev/null
+++ b/src/cmd/root.go
@@ -0,0 +1,23 @@
+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
new file mode 100644
index 0000000..876788e
--- /dev/null
+++ b/src/cmd/run.go
@@ -0,0 +1,22 @@
+package cmd
+
+import (
+ "fmt"
+
+ "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) {
+ out, err := exercises.Run(args[0])
+ fmt.Println(out, err)
+ },
+}
diff --git a/src/exercises/runner.go b/src/exercises/runner.go
new file mode 100644
index 0000000..1fe2a32
--- /dev/null
+++ b/src/exercises/runner.go
@@ -0,0 +1,9 @@
+package exercises
+
+import "os/exec"
+
+func Run(exercise string) (string, error) {
+ cmd := exec.Command("go", "run", "./exercises/"+exercise+".go")
+ cOut, err := cmd.CombinedOutput()
+ return string(cOut), err
+}
diff --git a/src/main.go b/src/main.go
index 7905807..3b64929 100644
--- a/src/main.go
+++ b/src/main.go
@@ -1,5 +1,7 @@
package main
-func main() {
+import "github.com/mauricioabreu/golings/src/cmd"
+func main() {
+ cmd.Execute()
}