Commit 5779ea0

mo khan <mo@mokhan.ca>
2024-06-05 23:16:17
Save issues to yaml file
1 parent 8f77a3b
pkg/db/client.go
@@ -1,17 +1,26 @@
 package db
 
 import (
+	"bytes"
+	"fmt"
+	"io/ioutil"
 	"os"
 	"path/filepath"
 
+	"github.com/xlgmokha/x/pkg/env"
+	"github.com/xlgmokha/x/pkg/serde"
 	"github.com/xlgmokha/x/pkg/x"
 )
 
-type Storage[T any] struct {
+type Paramable interface {
+	ToParam() string
+}
+
+type Storage[T Paramable] struct {
 	dir string
 }
 
-func New[T any](dir string) *Storage[T] {
+func New[T Paramable](dir string) *Storage[T] {
 	fullPath := x.Must(filepath.Abs(dir))
 	x.Check(os.MkdirAll(fullPath, 0700))
 
@@ -21,5 +30,14 @@ func New[T any](dir string) *Storage[T] {
 }
 
 func (db *Storage[T]) Save(item T) error {
-	return nil
+	w := new(bytes.Buffer)
+	x.Check(serde.To(w, item, serde.YAML))
+	if env.Fetch("DUMP", "") != "" {
+		fmt.Println(w.String())
+	}
+	return ioutil.WriteFile(
+		fmt.Sprintf("%v/%v.yaml", db.dir, item.ToParam()),
+		w.Bytes(),
+		0700,
+	)
 }
pkg/gitlab/issue.go
@@ -1,6 +1,7 @@
 package gitlab
 
 import (
+	"fmt"
 	"io"
 	"time"
 
@@ -27,6 +28,10 @@ type Issue struct {
 	Labels      []string   `json:"labels"`
 }
 
+func (issue *Issue) ToParam() string {
+	return fmt.Sprintf("%v", issue.ID)
+}
+
 func FromIssues(r io.Reader) ([]Issue, error) {
 	return serde.From[[]Issue](r, serde.JSON)
 }
.gitignore
@@ -0,0 +1,1 @@
+/db
go.mod
@@ -5,12 +5,13 @@ go 1.22
 require (
 	github.com/magefile/mage v1.15.0
 	github.com/stretchr/testify v1.8.0
-	github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1
+	github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8
 )
 
 require (
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/google/jsonapi v1.0.0 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
+	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )
go.sum
@@ -12,10 +12,12 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
 github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
 github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
-github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1 h1:MIqcDqEAcr16QUq/kFJC3xbYoaAjjsxYEJLGtol2U58=
-github.com/xlgmokha/x v0.0.0-20221023040112-0610463739d1/go.mod h1:Hf/0a9FveTBuVKvZ+Emmw6BlJyLGlhgjQwa4dFXp48s=
+github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8 h1:Hmyf8pgNUs3l8TW0YdUarBVAU+hWX87efBukspg4nWc=
+github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8/go.mod h1:C9MUZ3A7PTPbrLNTvu2lKhpM0dFpPHt5yH8YGuYzmKQ=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=