mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
refactor(odin): Ported remove command.
This commit is contained in:
1
cli.odin
1
cli.odin
@@ -41,6 +41,7 @@ IMPLEMENTED_COMMANDS := []string{
|
||||
"list",
|
||||
"backup",
|
||||
"add",
|
||||
"remove",
|
||||
}
|
||||
|
||||
parse_args :: proc() -> (cmd: Command, ok: bool) {
|
||||
|
||||
42
cmd_remove.odin
Normal file
42
cmd_remove.odin
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:path/filepath"
|
||||
import "core:strings"
|
||||
|
||||
cmd_remove :: proc(cmd: ^Command) {
|
||||
if len(cmd.args) != 1 {
|
||||
fmt.println("Usage: envr remove <path>")
|
||||
return
|
||||
}
|
||||
|
||||
path := cmd.args[0]
|
||||
if len(strings.trim_space(path)) == 0 {
|
||||
fmt.println("Error: No path provided")
|
||||
return
|
||||
}
|
||||
|
||||
abs_path: string
|
||||
if filepath.is_abs(path) {
|
||||
abs_path = path
|
||||
} else {
|
||||
resolved, abs_err := filepath.abs(path)
|
||||
if abs_err != nil {
|
||||
fmt.printf("Error getting absolute path: %v\n", abs_err)
|
||||
return
|
||||
}
|
||||
abs_path = resolved
|
||||
}
|
||||
|
||||
db, db_ok := db_open()
|
||||
if !db_ok {
|
||||
return
|
||||
}
|
||||
defer db_close(&db)
|
||||
|
||||
if !db_delete(&db, abs_path) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.printf("Removed %s from the database\n", abs_path)
|
||||
}
|
||||
26
db.odin
26
db.odin
@@ -392,6 +392,32 @@ db_insert :: proc(d: ^Db, file: EnvFile) -> bool {
|
||||
return true
|
||||
}
|
||||
|
||||
db_delete :: proc(d: ^Db, path: string) -> bool {
|
||||
sql := "DELETE FROM envr_env_files WHERE path = ?"
|
||||
stmt: ^rawptr
|
||||
rc := sqlite.prepare_v2(d.db, string_to_cstring(sql), -1, &stmt, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error preparing delete: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
defer sqlite.finalize(stmt)
|
||||
|
||||
rc = sqlite.bind_text(stmt, 1, string_to_cstring(path), -1, nil)
|
||||
rc = sqlite.step(stmt)
|
||||
if rc != sqlite.DONE {
|
||||
fmt.printf("Error deleting: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
|
||||
if sqlite.changes(d.db) == 0 {
|
||||
fmt.printf("No file found with path: %s\n", path)
|
||||
return false
|
||||
}
|
||||
|
||||
d.changed = true
|
||||
return true
|
||||
}
|
||||
|
||||
cstring_to_string :: proc(cs: cstring) -> string {
|
||||
if cs == nil {
|
||||
return ""
|
||||
|
||||
@@ -25,6 +25,8 @@ main :: proc() {
|
||||
cmd_list(&cmd)
|
||||
case "backup", "add":
|
||||
cmd_backup(&cmd)
|
||||
case "remove":
|
||||
cmd_remove(&cmd)
|
||||
case:
|
||||
fmt.printf("Unknown command: %s\n", cmd.name)
|
||||
print_usage()
|
||||
|
||||
@@ -18,10 +18,6 @@ cmd_restore :: proc(cmd: ^Command) {
|
||||
fmt.println("TODO: restore")
|
||||
}
|
||||
|
||||
cmd_remove :: proc(cmd: ^Command) {
|
||||
fmt.println("TODO: remove")
|
||||
}
|
||||
|
||||
cmd_check :: proc(cmd: ^Command) {
|
||||
fmt.println("TODO: check")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user