aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cmd/list.go27
-rw-r--r--src/exercises/list.go45
-rw-r--r--src/printer/list.go18
3 files changed, 90 insertions, 0 deletions
diff --git a/src/cmd/list.go b/src/cmd/list.go
new file mode 100644
index 0000000..103ea21
--- /dev/null
+++ b/src/cmd/list.go
@@ -0,0 +1,27 @@
+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/exercises/list.go b/src/exercises/list.go
new file mode 100644
index 0000000..3719527
--- /dev/null
+++ b/src/exercises/list.go
@@ -0,0 +1,45 @@
+package exercises
+
+import (
+ "os"
+
+ "github.com/pelletier/go-toml/v2"
+)
+
+type State int
+
+const (
+ Pending State = iota + 1
+ Done
+)
+
+func (s State) String() string {
+ return [...]string{"Pending", "Done"}[s-1]
+}
+
+type Exercise struct {
+ Name string
+ Path string
+ Mode string
+ Hint string
+ State State
+}
+
+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
+}
diff --git a/src/printer/list.go b/src/printer/list.go
new file mode 100644
index 0000000..bd65513
--- /dev/null
+++ b/src/printer/list.go
@@ -0,0 +1,18 @@
+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"})
+ for _, ex := range exs {
+ t.AppendRow(table.Row{ex.Name, ex.Path})
+ }
+ t.Render()
+}