mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
fix: Fixed some leaks in backup and scan.
This commit is contained in:
@@ -15,7 +15,10 @@ cmd_backup :: proc(cmd: ^Command) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: allow new_env_file to accept allocator?
|
||||||
|
// TODO: Write a test that covers this leak
|
||||||
file, ok := new_env_file(path)
|
file, ok := new_env_file(path)
|
||||||
|
defer delete_envfile(&file)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,11 @@ cmd_scan :: proc(cmd: ^Command) {
|
|||||||
selected, result := multi_select("Select .env files to backup:", files[:])
|
selected, result := multi_select("Select .env files to backup:", files[:])
|
||||||
defer delete(selected)
|
defer delete(selected)
|
||||||
if result == .Cancel {
|
if result == .Cancel {
|
||||||
fmt.wprintln(cmd.out, ansi.CSI + ansi.FAINT + ansi.SGR + "Cancelled." + ANSI_RESET, flush = false)
|
fmt.wprintln(
|
||||||
|
cmd.out,
|
||||||
|
ansi.CSI + ansi.FAINT + ansi.SGR + "Cancelled." + ANSI_RESET,
|
||||||
|
flush = false,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +85,9 @@ cmd_scan :: proc(cmd: ^Command) {
|
|||||||
if !selected[i] {
|
if !selected[i] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// TODO: Test cover this leak
|
||||||
env_file, ok := new_env_file(files[i])
|
env_file, ok := new_env_file(files[i])
|
||||||
|
defer delete_envfile(&env_file)
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.wprintf(cmd.err, "Error reading %s\n", files[i], flush = false)
|
fmt.wprintf(cmd.err, "Error reading %s\n", files[i], flush = false)
|
||||||
continue
|
continue
|
||||||
@@ -96,12 +102,23 @@ cmd_scan :: proc(cmd: ^Command) {
|
|||||||
if added_count > 0 {
|
if added_count > 0 {
|
||||||
fmt.wprintf(
|
fmt.wprintf(
|
||||||
cmd.out,
|
cmd.out,
|
||||||
ansi.CSI + ansi.BOLD + ";" + ansi.FG_GREEN + ansi.SGR + "Successfully added %d file(s) to backup." + ANSI_RESET + "\n",
|
ansi.CSI +
|
||||||
|
ansi.BOLD +
|
||||||
|
";" +
|
||||||
|
ansi.FG_GREEN +
|
||||||
|
ansi.SGR +
|
||||||
|
"Successfully added %d file(s) to backup." +
|
||||||
|
ANSI_RESET +
|
||||||
|
"\n",
|
||||||
added_count,
|
added_count,
|
||||||
flush = false,
|
flush = false,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
fmt.wprintln(cmd.out, ansi.CSI + ansi.FAINT + ansi.SGR + "No files were added." + ANSI_RESET, flush = false)
|
fmt.wprintln(
|
||||||
|
cmd.out,
|
||||||
|
ansi.CSI + ansi.FAINT + ansi.SGR + "No files were added." + ANSI_RESET,
|
||||||
|
flush = false,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
db.odin
2
db.odin
@@ -387,6 +387,7 @@ db_delete :: proc(db: ^Db, path: string) -> bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Caller is responsible for the returned memory
|
||||||
new_env_file :: proc(path: string) -> (EnvFile, bool) {
|
new_env_file :: proc(path: string) -> (EnvFile, bool) {
|
||||||
abs_path, abs_err := filepath.abs(path)
|
abs_path, abs_err := filepath.abs(path)
|
||||||
if abs_err != nil {
|
if abs_err != nil {
|
||||||
@@ -400,7 +401,6 @@ new_env_file :: proc(path: string) -> (EnvFile, bool) {
|
|||||||
remotes := get_git_remotes(dir, context.allocator)
|
remotes := get_git_remotes(dir, context.allocator)
|
||||||
|
|
||||||
data, read_err := os.read_entire_file_from_path(abs_path, context.allocator)
|
data, read_err := os.read_entire_file_from_path(abs_path, context.allocator)
|
||||||
defer delete(data)
|
|
||||||
if read_err != nil {
|
if read_err != nil {
|
||||||
fmt.printf("Error reading file %s: %v\n", abs_path, read_err)
|
fmt.printf("Error reading file %s: %v\n", abs_path, read_err)
|
||||||
return EnvFile{}, false
|
return EnvFile{}, false
|
||||||
|
|||||||
@@ -348,6 +348,7 @@ test_new_env_file :: proc(t: ^testing.T) {
|
|||||||
file, ok := new_env_file(env_path)
|
file, ok := new_env_file(env_path)
|
||||||
testing.expect(t, ok, "new_env_file should succeed")
|
testing.expect(t, ok, "new_env_file should succeed")
|
||||||
if !ok do return
|
if !ok do return
|
||||||
|
defer delete(file.contents)
|
||||||
defer delete(file.Remotes)
|
defer delete(file.Remotes)
|
||||||
defer delete(file.Sha256)
|
defer delete(file.Sha256)
|
||||||
defer delete(file.Path)
|
defer delete(file.Path)
|
||||||
|
|||||||
Reference in New Issue
Block a user