mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
refactor: Fixed up env_file_sync.
This commit is contained in:
50
db.odin
50
db.odin
@@ -387,38 +387,29 @@ new_env_file :: proc(path: string) -> (EnvFile, bool) {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
db_sync :: proc(d: ^Db, f: ^EnvFile) -> (SyncFlag, string) {
|
|
||||||
return env_file_sync(f, .TrustFilesystem, d)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If SyncFlag is .BackedUp, Caller is responsible for calling delete on f.contents and f.Sha256
|
// If SyncFlag is .BackedUp, Caller is responsible for calling delete on f.contents and f.Sha256
|
||||||
env_file_sync :: proc(f: ^EnvFile, dir: SyncDirection, d: ^Db) -> (SyncFlag, string) {
|
db_sync :: proc(d: ^Db, f: ^EnvFile) -> (SyncFlag, string) {
|
||||||
result: SyncFlag = {}
|
result: SyncFlag = {}
|
||||||
|
|
||||||
_, stat_err := os.stat(f.Dir, context.allocator)
|
if !os.exists(f.Dir) {
|
||||||
if stat_err != nil {
|
assert(d != nil)
|
||||||
moved_dirs: [dynamic]string
|
moved_dirs, dirs_ok := find_moved_dirs(d, f)
|
||||||
|
if !dirs_ok {
|
||||||
if d != nil {
|
return {.Error}, "failed to find moved dirs"
|
||||||
dirs, dirs_ok := find_moved_dirs(d, f)
|
|
||||||
if !dirs_ok {
|
|
||||||
return {.Error}, "failed to find moved dirs"
|
|
||||||
}
|
|
||||||
moved_dirs = dirs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(moved_dirs) == 0 {
|
switch len(moved_dirs) {
|
||||||
|
case 0:
|
||||||
return {.Error}, "directory missing"
|
return {.Error}, "directory missing"
|
||||||
} else if len(moved_dirs) == 1 {
|
case 1:
|
||||||
update_dir(f, moved_dirs[0])
|
update_dir(f, moved_dirs[0])
|
||||||
result = {.DirUpdated}
|
result = {.DirUpdated}
|
||||||
} else {
|
case:
|
||||||
return {.Error}, "multiple directories found"
|
return {.Error}, "multiple directories found"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, file_stat_err := os.stat(f.Path, context.allocator)
|
if !os.exists(f.Path) {
|
||||||
if file_stat_err != nil {
|
|
||||||
write_err := os.write_entire_file(f.Path, f.contents)
|
write_err := os.write_entire_file(f.Path, f.contents)
|
||||||
if write_err != nil {
|
if write_err != nil {
|
||||||
msg, _ := strings.concatenate({"failed to write file: ", fmt.tprintf("%v", write_err)})
|
msg, _ := strings.concatenate({"failed to write file: ", fmt.tprintf("%v", write_err)})
|
||||||
@@ -428,7 +419,9 @@ env_file_sync :: proc(f: ^EnvFile, dir: SyncDirection, d: ^Db) -> (SyncFlag, str
|
|||||||
return result + {.Restored}, ""
|
return result + {.Restored}, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Use temp allocator?
|
||||||
data, read_err := os.read_entire_file_from_path(f.Path, context.allocator)
|
data, read_err := os.read_entire_file_from_path(f.Path, context.allocator)
|
||||||
|
defer delete(data)
|
||||||
if read_err != nil {
|
if read_err != nil {
|
||||||
msg, _ := strings.concatenate(
|
msg, _ := strings.concatenate(
|
||||||
{"failed to read file for SHA comparison: ", fmt.tprintf("%v", read_err)},
|
{"failed to read file for SHA comparison: ", fmt.tprintf("%v", read_err)},
|
||||||
@@ -445,22 +438,12 @@ env_file_sync :: proc(f: ^EnvFile, dir: SyncDirection, d: ^Db) -> (SyncFlag, str
|
|||||||
return result, ""
|
return result, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
switch dir {
|
if env_file_backup(f) {
|
||||||
case .TrustDatabase:
|
|
||||||
write_err := os.write_entire_file(f.Path, f.contents)
|
|
||||||
if write_err != nil {
|
|
||||||
msg, _ := strings.concatenate({"failed to write file: ", fmt.tprintf("%v", write_err)})
|
|
||||||
return {.Error}, msg
|
|
||||||
}
|
|
||||||
return result + {.Restored}, ""
|
|
||||||
case .TrustFilesystem:
|
|
||||||
if !env_file_backup(f) {
|
|
||||||
return {.Error}, "failed to backup file"
|
|
||||||
}
|
|
||||||
return result + {.BackedUp}, ""
|
return result + {.BackedUp}, ""
|
||||||
|
} else {
|
||||||
|
return {.Error}, "failed to backup file"
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
find_moved_dirs :: proc(d: ^Db, f: ^EnvFile) -> ([dynamic]string, bool) {
|
find_moved_dirs :: proc(d: ^Db, f: ^EnvFile) -> ([dynamic]string, bool) {
|
||||||
@@ -530,7 +513,6 @@ get_git_remotes :: proc(dir: string) -> [dynamic]string {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return remotes
|
return remotes
|
||||||
}
|
}
|
||||||
defer ini.delete_map(m)
|
|
||||||
|
|
||||||
for section_name, section in m {
|
for section_name, section in m {
|
||||||
if strings.has_prefix(section_name, "remote ") {
|
if strings.has_prefix(section_name, "remote ") {
|
||||||
|
|||||||
Reference in New Issue
Block a user