From d00055aa3e7072e75c5880da94e6e51779dea540 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Fri, 1 May 2026 10:30:59 -0400 Subject: [PATCH] feat(config): Added data path. --- fixtures/default_config.json | 7 ++++--- fixtures/single-file.db | Bin 0 -> 12288 bytes src/Config.zig | 16 ++++++---------- src/Db.zig | 30 ++++++++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 fixtures/single-file.db diff --git a/fixtures/default_config.json b/fixtures/default_config.json index 5e91902..e0b2e99 100644 --- a/fixtures/default_config.json +++ b/fixtures/default_config.json @@ -1,8 +1,9 @@ { + "db_path": "~/.envr/data.age", "keys": [ { - "private": "/home/spencer/.ssh/id_ed25519", - "public": "/home/spencer/.ssh/id_ed25519.pub" + "private": "~/.ssh/id_ed25519", + "public": "~/.ssh/id_ed25519.pub" } ], "scan": { @@ -17,4 +18,4 @@ "~" ] } -} +} \ No newline at end of file diff --git a/fixtures/single-file.db b/fixtures/single-file.db new file mode 100644 index 0000000000000000000000000000000000000000..2095cd0ade85e67b545df90006768f210af951e7 GIT binary patch literal 12288 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU|?imU|?oI044?o1{MStERV#+%4B5F z>k{JS|G~h-_l<$?8~<*sszwDzLtr!nMnhmU1V%$(Gz3ONU^E0qLtr!nMnhmU1V%%E zCLz%1#Kk z4w9eZ4N+YV>eq9Cf|eK5(dQ3g;12=uM)7C}jE2By2#kinXb6mkz-S1JhQMeDjE2By z2#kinXb6mk0I?y^sL9A?8QjR5Sf^i5l%JKFT%xa+npdWmT9KGrkdqp%l%84Qke*qR zQJSQeoS$n|TAW%0Q>mApS)vqcn3iZ{Vq|J*l$>Ogl5Cigl$K(YWSMM{nq-_}YG`3* zYHXO6YHF68Y>{kkYHpH}WMpD#VQGl4_pf7~mQ2?HXwtqg0$7pIaH9SdbZ? zomv^A6qDx|5D@R`7iL?MT3nKum(B(5|372ke@3kDM%9gmz-S1JhQMeDjE2By2#kin gXb6mkz-S1JhQMeDjE2By2n@~;kYu*x#6B1S0JQa^rvLx| literal 0 HcmV?d00001 diff --git a/src/Config.zig b/src/Config.zig index a5e0586..a5efb64 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -1,5 +1,7 @@ const std = @import("std"); +db_path: []const u8 = "~/.envr/data.age", + /// Keys that are available for encryption keys: []const SSHKeyPair = &.{ .from_pub_path("~/.ssh/id_ed25519.pub"), @@ -120,11 +122,7 @@ test "loading the default config from disk matches expected values" { test "saving to a new file upserts the file" { const io = std.testing.io; - var cfg: @This() = .{ - .keys = &.{ - .from_pub_path("~/.ssh/id_ed25519.pub"), - }, - }; + var cfg: @This() = .{}; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -148,6 +146,7 @@ test "saving to a new file upserts the file" { const want = \\{ + \\ "db_path": "~/.envr/data.age", \\ "keys": [ \\ { \\ "private": "~/.ssh/id_ed25519", @@ -175,11 +174,7 @@ test "saving to a new file upserts the file" { test "saving to an existing file updates the file" { const io = std.testing.io; - var cfg: @This() = .{ - .keys = &.{ - .from_pub_path("~/.ssh/id_ed25519.pub"), - }, - }; + var cfg: @This() = .{}; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -201,6 +196,7 @@ test "saving to an existing file updates the file" { const want = \\{ + \\ "db_path": "~/.envr/data.age", \\ "keys": [ \\ { \\ "private": "~/.ssh/id_ed25519", diff --git a/src/Db.zig b/src/Db.zig index 9a390a7..bda2903 100644 --- a/src/Db.zig +++ b/src/Db.zig @@ -23,8 +23,11 @@ pub fn open( gpa: std.mem.Allocator, opts: OpenOptions, ) !@This() { - // TODO: Read from config? - const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" }); + // FIXME: cheating here + const db_path = try std.fs.path.join(gpa, &.{ + opts.home, + opts.config.db_path[2..], + }); defer gpa.free(db_path); var db = try new(opts.config); @@ -166,6 +169,27 @@ pub fn close( } } +/// Returns a list of all the .env files present in the database. +/// The caller is responsible for freeing memory +fn list(self: @This(), gpa: std.mem.Allocator) ![]EnvFile { + var stmt = self.sql_db.prepare( + "select path, remotes, sha256, contents from envr_env_files", + ); + defer stmt.deinit(); + + return stmt.all([]const EnvFile, gpa, .{}, .{}); +} + +const EnvFile = struct { + // TODO: Should use file_name in the struct and derive from the path. + path: []const u8, + /// dir is derived from Path, and is not stored in the database. + dir: []const u8, + remotes: [][]const u8, + sha256: []const u8, + contents: []const u8, +}; + test { std.testing.refAllDecls(@import("age.zig")); } @@ -257,8 +281,10 @@ 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); + // TODO: Pass testing keys var db: @This() = try .open(io, gpa, .{ .home = home, .tmp = tmp }); + // TODO: Get rid of direct access const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" }); defer gpa.free(db_path);