Commit b4dcf5e

mo khan <mo@mokhan.ca>
2025-01-13 17:41:02
Only update storage when file does not exist
1 parent 77bddf6
pkg/db/storage.go
@@ -2,6 +2,7 @@ package db
 
 import (
 	"bytes"
+	"errors"
 	"fmt"
 	"io/ioutil"
 	"os"
@@ -30,6 +31,10 @@ func New[T Entity](dir string) *Storage[T] {
 }
 
 func (db *Storage[T]) Save(item T) error {
+	if db.fileExistsFor(item) {
+		return nil
+	}
+
 	w := new(bytes.Buffer)
 	x.Check(serde.To(w, item, serde.YAML))
 	if env.Fetch("DUMP", "") != "" {
@@ -42,3 +47,13 @@ func (db *Storage[T]) Save(item T) error {
 func (db *Storage[T]) filePathFor(item T) string {
 	return fmt.Sprintf("%v/%v.yaml", db.dir, item.Identifier())
 }
+
+func (db *Storage[T]) fileExistsFor(item T) bool {
+	_, err := os.Stat(db.filePathFor(item))
+
+	if err != nil && errors.Is(err, os.ErrNotExist) {
+		return false
+	}
+
+	return true
+}
.tool-versions
@@ -1,1 +1,1 @@
-golang 1.22.3
+golang 1.23.4
go.mod
@@ -1,6 +1,6 @@
 module gitlab.com/mokhax/stanuki
 
-go 1.22
+go 1.23
 
 require (
 	github.com/magefile/mage v1.15.0