aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--exercises/variables1.go2
-rw-r--r--go.mod10
-rw-r--r--go.sum10
-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
7 files changed, 78 insertions, 2 deletions
diff --git a/exercises/variables1.go b/exercises/variables1.go
index d58a251..50be731 100644
--- a/exercises/variables1.go
+++ b/exercises/variables1.go
@@ -7,6 +7,6 @@ package main
import "fmt"
func main() {
- x := 5
+ x = 5
fmt.Printf("x has the value %d", x)
}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..3e8a556
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,10 @@
+module github.com/mauricioabreu/golings
+
+go 1.19
+
+require github.com/spf13/cobra v1.6.1
+
+require (
+ github.com/inconshreveable/mousetrap v1.0.1 // indirect
+ github.com/spf13/pflag v1.0.5 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..442875a
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,10 @@
+github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
+github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
+github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
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()
}