From 13e94956421b36f49259c4b40e2895ce074312de Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Thu, 25 Jun 2026 17:19:11 -0400 Subject: [PATCH] refactor: Replaced `fmt.printf` calls with `fmt.eprintf`. --- TODOS.md | 6 ++---- config.odin | 18 ++++++++--------- crypto.odin | 26 ++++++++++++------------ db.odin | 58 ++++++++++++++++++++++++++--------------------------- 4 files changed, 53 insertions(+), 55 deletions(-) diff --git a/TODOS.md b/TODOS.md index da3991d..c3dcc59 100644 --- a/TODOS.md +++ b/TODOS.md @@ -26,11 +26,9 @@ 13. Test all cmds / terminal branches. -14. Fix error messages to use fmt.eprintf (stderr) instead of fmt.printf (stdout) +14. Pass allocator to findr? -15. Pass allocator to findr? - -16. Update `read_wire_string` to use a slice. +15. Update `read_wire_string` to use a slice. ## Double-check AI output diff --git a/config.odin b/config.odin index de83ca7..8d648b4 100644 --- a/config.odin +++ b/config.odin @@ -30,14 +30,14 @@ load_config :: proc(config_path: string, allocator := context.allocator) -> (Con // TODO: Should we use context.allocator + defer delete()? data, read_err := os.read_entire_file_from_path(config_path, context.temp_allocator) if read_err != nil { - fmt.println("No config file found. Please run `envr init` to generate one.") + fmt.eprintln("No config file found. Please run `envr init` to generate one.") return Config{}, false } cfg: Config err := json.unmarshal(data, &cfg, .JSON5, allocator) if err != nil { - fmt.printf("Error parsing config: %v\n", err) + fmt.eprintf("Error parsing config: %v\n", err) return Config{}, false } cfg.config_path = config_path @@ -79,7 +79,7 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool { if !os.exists(config_dir) { mkdir_err := os.make_directory(config_dir) if mkdir_err != nil { - fmt.printf("Error creating %s directory: %v\n", config_dir, mkdir_err) + fmt.eprintf("Error creating %s directory: %v\n", config_dir, mkdir_err) return false } } @@ -89,7 +89,7 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool { if stat_err == nil { defer os.file_info_delete(info, context.temp_allocator) if info.size > 0 { - fmt.println("Config file already exists. Run again with --force to reinitialize.") + fmt.eprintln("Config file already exists. Run again with --force to reinitialize.") return false } } @@ -101,13 +101,13 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool { context.temp_allocator, ) if marshal_err != nil { - fmt.printf("Error marshaling config: %v\n", marshal_err) + fmt.eprintf("Error marshaling config: %v\n", marshal_err) return false } write_err := os.write_entire_file(cfg.config_path, data) if write_err != nil { - fmt.printf("Error writing config: %v\n", write_err) + fmt.eprintf("Error writing config: %v\n", write_err) return false } @@ -150,19 +150,19 @@ new_config :: proc( find_ssh_private_keys :: proc() -> (keys: [dynamic]string, ok: bool) { home, home_err := os.user_home_dir(context.allocator) if home_err != nil { - fmt.printf("Error getting home dir: %v\n", home_err) + fmt.eprintf("Error getting home dir: %v\n", home_err) return } ssh_dir, join_err := filepath.join([]string{home, ".ssh"}) if join_err != nil { - fmt.printf("Error building ssh path: %v\n", join_err) + fmt.eprintf("Error building ssh path: %v\n", join_err) return } entries, dir_err := os.read_all_directory_by_path(ssh_dir, context.allocator) if dir_err != nil { - fmt.printf("Could not read ~/.ssh directory: %v\n", dir_err) + fmt.eprintf("Could not read ~/.ssh directory: %v\n", dir_err) return } defer os.file_info_slice_delete(entries, context.allocator) diff --git a/crypto.odin b/crypto.odin index bd2b266..2e468b6 100644 --- a/crypto.odin +++ b/crypto.odin @@ -60,7 +60,7 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b &sym_key[0], ) if rc != 0 { - fmt.println("Error: symmetric encryption failed") + fmt.eprintln("Error: symmetric encryption failed") delete(secret_ct) return } @@ -84,7 +84,7 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b &x25519_pairs[0].Private[0], ) if rc != 0 { - fmt.printf("Error: failed to encrypt for recipient %d\n", i) + fmt.eprintf("Error: failed to encrypt for recipient %d\n", i) delete(entries) delete(secret_ct) return @@ -132,13 +132,13 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: bool) { if len(ciphertext) < HEADER_SIZE { - fmt.println("Error: ciphertext too short (header)") + fmt.eprintln("Error: ciphertext too short (header)") return } for i in 0 ..< 4 { if ciphertext[i] != MAGIC_BYTES[i] { - fmt.println("Error: invalid magic bytes") + fmt.eprintln("Error: invalid magic bytes") return } } @@ -166,7 +166,7 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b recipients_end := offset + int(num_recipients) * RECIPIENT_ENTRY_SIZE if recipients_end > len(ciphertext) { - fmt.println("Error: ciphertext too short (recipient data)") + fmt.eprintln("Error: ciphertext too short (recipient data)") return } @@ -222,7 +222,7 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b } if !found { - fmt.println("Error: no matching recipient found") + fmt.eprintln("Error: no matching recipient found") return } @@ -236,14 +236,14 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b &x25519_pairs[matched_pi].Private[0], ) if rc != 0 { - fmt.println("Error: failed to decrypt symmetric key") + fmt.eprintln("Error: failed to decrypt symmetric key") return } ct_data := ciphertext[recipients_end:] pt_len := len(ct_data) - CRYPTO_SECRETBOX_MAC_BYTES if pt_len < 0 { - fmt.println("Error: ciphertext too short (no encrypted data)") + fmt.eprintln("Error: ciphertext too short (no encrypted data)") return } @@ -260,7 +260,7 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b &sym_key[0], ) if rc != 0 { - fmt.println("Error: symmetric decryption failed") + fmt.eprintln("Error: symmetric decryption failed") delete(plaintext) return } @@ -285,21 +285,21 @@ ssh_to_x25519 :: proc( for i in 0 ..< len(keys) { ssh_kp, parse_ok := parse_ssh_private_key(keys[i].private) if !parse_ok { - fmt.printf("Error: failed to parse SSH private key: %s\n", keys[i].private) + fmt.eprintf("Error: failed to parse SSH private key: %s\n", keys[i].private) delete(pairs) return pairs, false } ssh_pub, pub_ok := parse_ssh_public_key(keys[i].public) if !pub_ok { - fmt.printf("Error: failed to parse SSH public key: %s\n", keys[i].public) + fmt.eprintf("Error: failed to parse SSH public key: %s\n", keys[i].public) delete(pairs) return pairs, false } pk_rc := crypto_sign_ed25519_pk_to_curve25519(&pairs[i].Public[0], &ssh_pub[0]) if pk_rc != 0 { - fmt.println("Error: failed to convert ed25519 public key to curve25519") + fmt.eprintln("Error: failed to convert ed25519 public key to curve25519") delete(pairs) return pairs, false } @@ -314,7 +314,7 @@ ssh_to_x25519 :: proc( sk_rc := crypto_sign_ed25519_sk_to_curve25519(&pairs[i].Private[0], &ed25519_sk[0]) if sk_rc != 0 { - fmt.println("Error: failed to convert ed25519 private key to curve25519") + fmt.eprintln("Error: failed to convert ed25519 private key to curve25519") delete(pairs) return pairs, false } diff --git a/db.odin b/db.odin index 750107b..33d59f9 100644 --- a/db.odin +++ b/db.odin @@ -94,14 +94,14 @@ db_init :: proc() -> (db: Db, ok: bool) { conn: sqlite.Db rc := sqlite.open(":memory:", &conn) if rc != sqlite.OK { - fmt.printf("Error opening in-memory database: %s\n", sqlite.errmsg(conn)) + fmt.eprintf("Error opening in-memory database: %s\n", sqlite.errmsg(conn)) return } create_sql: cstring = "CREATE TABLE IF NOT EXISTS envr_env_files (path TEXT PRIMARY KEY NOT NULL, remotes TEXT, sha256 TEXT NOT NULL, contents TEXT NOT NULL)" rc = sqlite.exec(conn, create_sql, nil, nil, nil) if rc != sqlite.OK { - fmt.printf("Error creating table: %s\n", sqlite.errmsg(conn)) + fmt.eprintf("Error creating table: %s\n", sqlite.errmsg(conn)) sqlite.close(conn) return } @@ -119,14 +119,14 @@ db_allocator :: proc(db: ^Db) -> mem.Allocator { db_restore_from_encrypted :: proc(db: ^Db, data_path: string) -> bool { encrypted_data, read_err := os.read_entire_file_from_path(data_path, context.temp_allocator) if read_err != nil { - fmt.printf("Error reading encrypted database: %v\n", read_err) + fmt.eprintf("Error reading encrypted database: %v\n", read_err) return false } // TODO: Use context.temp_allocator plaintext, dec_ok := decrypt(encrypted_data, db.cfg.keys[:]) if !dec_ok { - fmt.println("Error: decryption failed") + fmt.eprintln("Error: decryption failed") return false } defer delete(plaintext) @@ -134,7 +134,7 @@ db_restore_from_encrypted :: proc(db: ^Db, data_path: string) -> bool { n := i64(len(plaintext)) buf := sqlite.malloc64(n) if buf == nil { - fmt.println("Error: failed to allocate buffer for deserialization") + fmt.eprintln("Error: failed to allocate buffer for deserialization") return false } copy(buf[:len(plaintext)], plaintext) @@ -144,7 +144,7 @@ db_restore_from_encrypted :: proc(db: ^Db, data_path: string) -> bool { rc := sqlite.deserialize(db.conn, "main", buf, n, n, flags) if rc != sqlite.OK { sqlite.free(buf) - fmt.printf("Error deserializing database: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error deserializing database: %s\n", sqlite.errmsg(db.conn)) return false } @@ -167,14 +167,14 @@ db_close :: proc(db: ^Db) { if db.changed && len(db.cfg.keys) > 0 { rc := sqlite.exec(db.conn, "VACUUM", nil, nil, nil) if rc != sqlite.OK { - fmt.printf("Error vacuuming database: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error vacuuming database: %s\n", sqlite.errmsg(db.conn)) return } sz: i64 data := sqlite.serialize(db.conn, "main", &sz, 0) if data == nil { - fmt.println("Error: failed to serialize database") + fmt.eprintln("Error: failed to serialize database") return } defer sqlite.free(data) @@ -195,7 +195,7 @@ db_close :: proc(db: ^Db) { write_err := os.write_entire_file(data_path, encrypted) delete(encrypted) if write_err != nil { - fmt.printf("Error writing encrypted database: %v\n", write_err) + fmt.eprintf("Error writing encrypted database: %v\n", write_err) return } @@ -214,7 +214,7 @@ db_list :: proc(db: ^Db) -> ([]EnvFile, bool) { nil, ) if rc != sqlite.OK { - fmt.printf("Error preparing query: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error preparing query: %s\n", sqlite.errmsg(db.conn)) return []EnvFile{}, false } defer sqlite.finalize(stmt) @@ -229,7 +229,7 @@ db_list :: proc(db: ^Db) -> ([]EnvFile, bool) { break } if rc != sqlite.ROW { - fmt.printf("Error stepping query: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error stepping query: %s\n", sqlite.errmsg(db.conn)) #no_bounds_check return results[:], false } @@ -282,7 +282,7 @@ db_insert :: proc(db: ^Db, file: EnvFile) -> bool { stmt: sqlite.Stmt rc := sqlite.prepare_v2(db.conn, sql, -1, &stmt, nil) if rc != sqlite.OK { - fmt.printf("Error preparing insert: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error preparing insert: %s\n", sqlite.errmsg(db.conn)) return false } defer sqlite.finalize(stmt) @@ -292,7 +292,7 @@ db_insert :: proc(db: ^Db, file: EnvFile) -> bool { defer delete(cpath) rc = sqlite.bind_text(stmt, 1, cpath, -1, nil) if rc != sqlite.OK { - fmt.printf("Error binding path: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error binding path: %s\n", sqlite.errmsg(db.conn)) return false } @@ -300,7 +300,7 @@ db_insert :: proc(db: ^Db, file: EnvFile) -> bool { defer delete(cremotes) rc = sqlite.bind_text(stmt, 2, cremotes, -1, nil) if rc != sqlite.OK { - fmt.printf("Error binding remotes: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error binding remotes: %s\n", sqlite.errmsg(db.conn)) return false } @@ -308,7 +308,7 @@ db_insert :: proc(db: ^Db, file: EnvFile) -> bool { defer delete(csha) rc = sqlite.bind_text(stmt, 3, csha, -1, nil) if rc != sqlite.OK { - fmt.printf("Error binding sha256: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error binding sha256: %s\n", sqlite.errmsg(db.conn)) return false } @@ -316,13 +316,13 @@ db_insert :: proc(db: ^Db, file: EnvFile) -> bool { defer delete(ccontents) rc = sqlite.bind_text(stmt, 4, ccontents, -1, nil) if rc != sqlite.OK { - fmt.printf("Error binding contents: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error binding contents: %s\n", sqlite.errmsg(db.conn)) return false } rc = sqlite.step(stmt) if rc != sqlite.DONE { - fmt.printf("Error inserting: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error inserting: %s\n", sqlite.errmsg(db.conn)) return false } @@ -340,7 +340,7 @@ db_fetch :: proc(db: ^Db, path: string) -> (EnvFile, bool) { stmt: sqlite.Stmt rc := sqlite.prepare_v2(db.conn, sql, -1, &stmt, nil) if rc != sqlite.OK { - fmt.printf("Error preparing fetch: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error preparing fetch: %s\n", sqlite.errmsg(db.conn)) return EnvFile{}, false } defer sqlite.finalize(stmt) @@ -351,16 +351,16 @@ db_fetch :: proc(db: ^Db, path: string) -> (EnvFile, bool) { defer delete(cpath, allocator) rc = sqlite.bind_text(stmt, 1, cpath, -1, nil) if rc != sqlite.OK { - fmt.printf("Error binding path: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error binding path: %s\n", sqlite.errmsg(db.conn)) return EnvFile{}, false } rc = sqlite.step(stmt) if rc == sqlite.DONE { - fmt.printf("No file found with path: %s\n", path) + fmt.eprintf("No file found with path: %s\n", path) return EnvFile{}, false } if rc != sqlite.ROW { - fmt.printf("Error fetching: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error fetching: %s\n", sqlite.errmsg(db.conn)) return EnvFile{}, false } @@ -406,7 +406,7 @@ db_delete :: proc(db: ^Db, path: string) -> bool { stmt: sqlite.Stmt rc := sqlite.prepare_v2(db.conn, sql, -1, &stmt, nil) if rc != sqlite.OK { - fmt.printf("Error preparing delete: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error preparing delete: %s\n", sqlite.errmsg(db.conn)) return false } defer sqlite.finalize(stmt) @@ -415,17 +415,17 @@ db_delete :: proc(db: ^Db, path: string) -> bool { defer delete(cpath) rc = sqlite.bind_text(stmt, 1, cpath, -1, nil) if rc != sqlite.OK { - fmt.printf("Error binding path: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error binding path: %s\n", sqlite.errmsg(db.conn)) return false } rc = sqlite.step(stmt) if rc != sqlite.DONE { - fmt.printf("Error deleting: %s\n", sqlite.errmsg(db.conn)) + fmt.eprintf("Error deleting: %s\n", sqlite.errmsg(db.conn)) return false } if sqlite.changes(db.conn) == 0 { - fmt.printf("No file found with path: %s\n", path) + fmt.eprintf("No file found with path: %s\n", path) return false } @@ -437,7 +437,7 @@ db_delete :: proc(db: ^Db, path: string) -> bool { new_env_file :: proc(path: string) -> (EnvFile, bool) { abs_path, abs_err := filepath.abs(path) if abs_err != nil { - fmt.printf("Error getting absolute path: %v\n", abs_err) + fmt.eprintf("Error getting absolute path: %v\n", abs_err) return EnvFile{}, false } @@ -448,7 +448,7 @@ new_env_file :: proc(path: string) -> (EnvFile, bool) { data, read_err := os.read_entire_file_from_path(abs_path, context.allocator) if read_err != nil { - fmt.printf("Error reading file %s: %v\n", abs_path, read_err) + fmt.eprintf("Error reading file %s: %v\n", abs_path, read_err) return EnvFile{}, false } @@ -631,7 +631,7 @@ to_cstring :: proc { string_to_cstring :: proc(s: string, allocator := context.allocator) -> cstring { cs, err := strings.clone_to_cstring(s, allocator) if err != nil { - fmt.printf("Failed to convert string to cstring: %v\n", err) + fmt.eprintf("Failed to convert string to cstring: %v\n", err) panic("Allocation Exception") } return cs @@ -641,7 +641,7 @@ string_to_cstring :: proc(s: string, allocator := context.allocator) -> cstring clone_cstring :: proc(c: cstring, allocator := context.allocator) -> string { str, err := strings.clone_from_cstring(c, allocator) if err != nil { - fmt.printf("Failed to convert string to cstring: %v\n", err) + fmt.eprintf("Failed to convert string to cstring: %v\n", err) delete(str) panic("Allocation Exception") }