aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/yaml_util.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/admin/yaml_util.go')
-rw-r--r--internal/admin/yaml_util.go62
1 files changed, 61 insertions, 1 deletions
diff --git a/internal/admin/yaml_util.go b/internal/admin/yaml_util.go
index 519a04c..b869ffe 100644
--- a/internal/admin/yaml_util.go
+++ b/internal/admin/yaml_util.go
@@ -214,6 +214,52 @@ func createEntityDir(dataDir, subdir, name string) error {
return os.Mkdir(dirPath, 0755)
}
+func renameEntityFile(dataDir, subdir, oldID, newID string) (string, string, []byte, error) {
+ oldPath, err := findEntityPath(dataDir, subdir, oldID)
+ if err != nil {
+ return "", "", nil, fmt.Errorf("entity not found: %s/%s", subdir, oldID)
+ }
+ dir := filepath.Dir(oldPath)
+ newPath := filepath.Join(dir, newID+".yaml")
+ if oldPath == newPath {
+ return "", "", nil, fmt.Errorf("new name is same as current")
+ }
+ if _, err := os.Stat(newPath); err == nil {
+ return "", "", nil, fmt.Errorf("entity %s already exists", newID)
+ }
+ data, err := os.ReadFile(oldPath)
+ if err != nil {
+ return "", "", nil, fmt.Errorf("read: %w", err)
+ }
+ 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
+}
+
+func renameEntityDir(dataDir, subdir, oldName, newName string) error {
+ oldName = strings.TrimSpace(oldName)
+ newName = strings.TrimSpace(newName)
+ if oldName == "" || newName == "" || strings.ContainsAny(oldName, "/\\") || oldName == "." || oldName == ".." {
+ return fmt.Errorf("invalid old directory name")
+ }
+ if strings.ContainsAny(newName, "/\\") || newName == "." || newName == ".." {
+ return fmt.Errorf("invalid new directory name")
+ }
+ oldPath := filepath.Join(dataDir, subdir, oldName)
+ if _, err := os.Stat(oldPath); os.IsNotExist(err) {
+ return fmt.Errorf("directory %s does not exist", oldName)
+ }
+ newPath := filepath.Join(dataDir, subdir, newName)
+ if _, err := os.Stat(newPath); err == nil {
+ return fmt.Errorf("directory %s already exists", newName)
+ }
+ return os.Rename(oldPath, newPath)
+}
+
func deleteEntityDir(dataDir, subdir, dir string) ([]ChangeDesc, error) {
dir = strings.TrimSpace(dir)
if dir == "" || strings.ContainsAny(dir, "/\\") || dir == "." || dir == ".." {
@@ -225,13 +271,27 @@ func deleteEntityDir(dataDir, subdir, dir string) ([]ChangeDesc, error) {
return nil, fmt.Errorf("directory not found: %s", dir)
}
+ rootDir := filepath.Join(dataDir, subdir)
+ var clashes []string
+ for _, e := range entries {
+ if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" {
+ continue
+ }
+ if _, err := os.Stat(filepath.Join(rootDir, e.Name())); err == nil {
+ clashes = append(clashes, e.Name())
+ }
+ }
+ if len(clashes) > 0 {
+ return nil, fmt.Errorf("files already exist in root: %s", strings.Join(clashes, ", "))
+ }
+
var changes []ChangeDesc
for _, e := range entries {
if e.IsDir() || filepath.Ext(e.Name()) != ".yaml" {
continue
}
oldPath := filepath.Join(srcDir, e.Name())
- newPath := filepath.Join(dataDir, subdir, e.Name())
+ newPath := filepath.Join(rootDir, e.Name())
data, err := os.ReadFile(oldPath)
if err != nil {
return nil, fmt.Errorf("read %s: %w", e.Name(), err)