feat(config): Added data path.

This commit is contained in:
2026-05-01 10:30:59 -04:00
parent 3e6c17520c
commit 8f2c241963
4 changed files with 38 additions and 15 deletions

View File

@@ -1,8 +1,9 @@
{ {
"db_path": "~/.envr/data.age",
"keys": [ "keys": [
{ {
"private": "/home/spencer/.ssh/id_ed25519", "private": "~/.ssh/id_ed25519",
"public": "/home/spencer/.ssh/id_ed25519.pub" "public": "~/.ssh/id_ed25519.pub"
} }
], ],
"scan": { "scan": {
@@ -17,4 +18,4 @@
"~" "~"
] ]
} }
} }

BIN
fixtures/single-file.db Normal file

Binary file not shown.

View File

@@ -1,5 +1,7 @@
const std = @import("std"); const std = @import("std");
db_path: []const u8 = "~/.envr/data.age",
/// Keys that are available for encryption /// Keys that are available for encryption
keys: []const SSHKeyPair = &.{ keys: []const SSHKeyPair = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"), .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" { test "saving to a new file upserts the file" {
const io = std.testing.io; const io = std.testing.io;
var cfg: @This() = .{ var cfg: @This() = .{};
.keys = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
};
var tmp = std.testing.tmpDir(.{}); var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); defer tmp.cleanup();
@@ -148,6 +146,7 @@ test "saving to a new file upserts the file" {
const want = const want =
\\{ \\{
\\ "db_path": "~/.envr/data.age",
\\ "keys": [ \\ "keys": [
\\ { \\ {
\\ "private": "~/.ssh/id_ed25519", \\ "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" { test "saving to an existing file updates the file" {
const io = std.testing.io; const io = std.testing.io;
var cfg: @This() = .{ var cfg: @This() = .{};
.keys = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
};
var tmp = std.testing.tmpDir(.{}); var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); defer tmp.cleanup();
@@ -201,6 +196,7 @@ test "saving to an existing file updates the file" {
const want = const want =
\\{ \\{
\\ "db_path": "~/.envr/data.age",
\\ "keys": [ \\ "keys": [
\\ { \\ {
\\ "private": "~/.ssh/id_ed25519", \\ "private": "~/.ssh/id_ed25519",

View File

@@ -23,8 +23,11 @@ pub fn open(
gpa: std.mem.Allocator, gpa: std.mem.Allocator,
opts: OpenOptions, opts: OpenOptions,
) !@This() { ) !@This() {
// TODO: Read from config? // FIXME: cheating here
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" }); const db_path = try std.fs.path.join(gpa, &.{
opts.home,
opts.config.db_path[2..],
});
defer gpa.free(db_path); defer gpa.free(db_path);
var db = try new(opts.config); 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 { test {
std.testing.refAllDecls(@import("age.zig")); 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" }); const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
defer gpa.free(tmp); defer gpa.free(tmp);
// TODO: Pass testing keys
var db: @This() = try .open(io, gpa, .{ .home = home, .tmp = tmp }); 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" }); const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path); defer gpa.free(db_path);