From 5779ea0f400b7e8df223fe320ab07727227a2f3c Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 5 Jun 2024 17:16:17 -0600 Subject: Save issues to yaml file --- pkg/db/client.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'pkg/db') 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, + ) } -- cgit v1.2.3