summaryrefslogtreecommitdiff
path: root/pkg/db
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2024-06-05 17:16:17 -0600
committermo khan <mo@mokhan.ca>2024-06-05 17:16:17 -0600
commit5779ea0f400b7e8df223fe320ab07727227a2f3c (patch)
treec29c4ba835a6149f1709a00492062e10483852c5 /pkg/db
parent8f77a3bbcd298e3839e8276276ee4bcd2a826c3b (diff)
Save issues to yaml file
Diffstat (limited to 'pkg/db')
-rw-r--r--pkg/db/client.go24
1 files changed, 21 insertions, 3 deletions
diff --git a/pkg/db/client.go b/pkg/db/client.go
index a656505..4f43403 100644
--- a/pkg/db/client.go
+++ b/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,
+ )
}