diff --git a/src/Config.zig b/src/Config.zig index 5bc18ad..a3dd270 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -1,7 +1,9 @@ const std = @import("std"); /// Keys that are available for encryption -keys: []const SSHKeyPair, +keys: []const SSHKeyPair = &.{ + .from_pub_path("~/.ssh/id_ed25519.pub"), +}, /// Rules for how to match the scan command scan: ScanConfig = .default, @@ -12,11 +14,11 @@ pub const SSHKeyPair = struct { /// Caller owns the returned memory pub fn from_path(gpa: std.mem.Allocator, path: []const u8) !SSHKeyPair { - if (std.mem.eql(u8, std.fs.path.extension(path), ".pub")){ + if (std.mem.eql(u8, std.fs.path.extension(path), ".pub")) { return from_pub_path(path); } else { return .{ - .public = try std.mem.concat(gpa, u8, &.{path, ".pub"}), + .public = try std.mem.concat(gpa, u8, &.{ path, ".pub" }), .private = path, }; } @@ -27,7 +29,7 @@ pub const SSHKeyPair = struct { return .{ .public = path, - .private = path[0..path.len - 4], + .private = path[0 .. path.len - 4], }; } }; @@ -119,7 +121,7 @@ test "saving to a new file upserts the file" { var cfg: @This() = .{ .keys = &.{ - .from_pub_path("~/.ssh/id_ed25519.pub"), + .from_pub_path("~/.ssh/id_ed25519.pub"), }, }; diff --git a/src/Db.zig b/src/Db.zig index b432a59..d43cc02 100644 --- a/src/Db.zig +++ b/src/Db.zig @@ -4,6 +4,10 @@ const std = @import("std"); const sqlite = @import("sqlite"); const age = @import("age.zig"); +const Config = @import("Config.zig"); + +/// controls the keys and filepaths used for saving +config: Config, /// The underlying data store. sql_db: sqlite.Db, @@ -17,21 +21,19 @@ changed: bool = false, pub fn open( io: std.Io, gpa: std.mem.Allocator, - /// The path to the home directory - home: []const u8, - /// The path to the /tmp directory - tmp: []const u8, + opts: OpenOptions, ) !@This() { - const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" }); + // TODO: Read from config? + const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" }); defer gpa.free(db_path); - var db = try new(); + var db = try new(opts.config); if (db_exists(io, db_path)) { // const tmp_dir = try std.Io.Dir.cwd().openDir(io, tmp, .{}); // defer tmp_dir.deleteFile(io, "envr.db"); - const tmp_db_path = try std.fs.path.join(gpa, &.{ tmp, "envr.db" }); + const tmp_db_path = try std.fs.path.join(gpa, &.{ opts.tmp, "envr.db" }); defer gpa.free(tmp_db_path); // TODO: Pass key(s) from Config @@ -46,8 +48,18 @@ pub fn open( } } +const OpenOptions = struct { + config: Config = .{}, + + /// The path to the home directory + home: []const u8 = "~/", + /// The path to the /tmp directory + // FIXME: Support windows + tmp: []const u8 = "/tmp", +}; + /// Create a new instance of the database in-memory -fn new() !@This() { +fn new(config: Config) !@This() { var db = try sqlite.Db.init(.{ .mode = .Memory, .open_flags = .{ .write = true, .create = true }, @@ -63,7 +75,10 @@ fn new() !@This() { \\) , .{}, .{}); - return .{ .sql_db = db }; + return .{ + .sql_db = db, + .config = config, + }; } /// Returns true if a file exists at ~/.envr/data.age @@ -224,7 +239,7 @@ test "Closing a fresh database does not create a file" { const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" }); defer gpa.free(tmp); - var db: @This() = try .open(io, gpa, home, tmp); + var db: @This() = try .open(io, gpa, .{ .home = home, .tmp = tmp }); const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" }); defer gpa.free(db_path); @@ -242,6 +257,6 @@ test "Closing a fresh database does not create a file" { ); } -// test "Closing an unmodified database does not create a file" {} +// test "Closing an unmodified database does not update the file" {} // test "Closing a modified database does create a file" {}