aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/yaml_util.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-07-04 22:44:42 -0400
committerhistoria <[not public]>2026-07-04 22:44:42 -0400
commitad22ea3526a146ea8ce305c8375c35836465cc30 (patch)
tree479cec511e596bb09d3f3792dcf10a2201e9b7f8 /internal/admin/yaml_util.go
parent7b009e904524f30cbff6346730e8d8fe4b046a6b (diff)
downloadthehouseoficarus-ad22ea3526a146ea8ce305c8375c35836465cc30.tar.gz
feat: admin gui move objects between directories and better toolbar wrapping
Diffstat (limited to 'internal/admin/yaml_util.go')
-rw-r--r--internal/admin/yaml_util.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/admin/yaml_util.go b/internal/admin/yaml_util.go
index 4d0a814..465be38 100644
--- a/internal/admin/yaml_util.go
+++ b/internal/admin/yaml_util.go
@@ -2,6 +2,7 @@ package admin
import (
"bytes"
+ "fmt"
"log"
"os"
"path/filepath"
@@ -154,3 +155,46 @@ func backupFile(path string) []byte {
func snapshotFile(path string) ([]byte, error) {
return os.ReadFile(path)
}
+
+func findEntityPath(dataDir, subdir, id string) (string, error) {
+ _, path, err := readYAMLFile(dataDir, subdir, id)
+ if err == nil {
+ return path, nil
+ }
+ _, path, err = findYAMLFileInSubdirs(dataDir, subdir, id)
+ return path, err
+}
+
+func moveEntityFile(dataDir, subdir, id, targetDir string) (string, string, []byte, error) {
+ oldPath, err := findEntityPath(dataDir, subdir, id)
+ if err != nil {
+ return "", "", nil, fmt.Errorf("entity not found: %s/%s", subdir, id)
+ }
+ data, err := os.ReadFile(oldPath)
+ if err != nil {
+ return "", "", nil, fmt.Errorf("read: %w", err)
+ }
+ var newPath string
+ if targetDir == "" {
+ newPath = filepath.Join(dataDir, subdir, id+".yaml")
+ } else {
+ destDir := filepath.Join(dataDir, subdir, targetDir)
+ if _, err := os.Stat(destDir); os.IsNotExist(err) {
+ return "", "", nil, fmt.Errorf("directory does not exist: %s", targetDir)
+ }
+ newPath = filepath.Join(destDir, id+".yaml")
+ }
+ if oldPath == newPath {
+ return "", "", nil, fmt.Errorf("already in target directory")
+ }
+ if _, err := os.Stat(newPath); err == nil {
+ return "", "", nil, fmt.Errorf("entity already exists in target directory")
+ }
+ if err := os.WriteFile(newPath, data, 0644); err != nil {
+ return "", "", nil, fmt.Errorf("write: %w", err)
+ }
+ if err := os.Remove(oldPath); err != nil {
+ return "", "", nil, fmt.Errorf("remove old: %w", err)
+ }
+ return oldPath, newPath, data, nil
+}