Skip to content

Commit

Permalink
feat: add gpg-id and default opts to gpg command
Browse files Browse the repository at this point in the history
  • Loading branch information
deevus committed May 8, 2023
1 parent 8e3e31e commit 08f2c2a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const PassConfig = struct {
characterSet: []const u8,
characterSetNoSymbols: []const u8,
gpgOpts: []const u8,
gpgId: ?[]const u8,

pub fn init(allocator: std.mem.Allocator) !PassConfig {
const home = try getHome(allocator);
Expand All @@ -21,6 +22,7 @@ pub const PassConfig = struct {
const character_set = try getCharacterSet(allocator);
const character_set_no_symbols = try getCharacterSetNoSymbols(allocator);
const gpg_opts = try getGpgOpts(allocator);
const gpg_id = try getGpgId(allocator, prefix);

return PassConfig{
.allocator = allocator,
Expand All @@ -32,6 +34,7 @@ pub const PassConfig = struct {
.characterSet = character_set,
.characterSetNoSymbols = character_set_no_symbols,
.gpgOpts = gpg_opts,
.gpgId = gpg_id,
};
}

Expand Down Expand Up @@ -98,4 +101,24 @@ pub const PassConfig = struct {

return try std.fmt.allocPrint(allocator, "{s} --quiet --yes --compress-algo=none --no-encrypt-to", .{customOpts});
}

fn getGpgId(allocator: std.mem.Allocator, prefix: []const u8) !?[]const u8 {
const gpg_id_path = try std.fs.path.join(allocator, &.{ prefix, ".gpg-id" });
defer allocator.free(gpg_id_path);

const file = std.fs.openFileAbsolute(gpg_id_path, .{}) catch |err| {
if (err == error.FileNotFound) {
return null;
}

return err;
};
defer file.close();

var gpg_id = try allocator.alloc(u8, 16);

_ = try file.read(gpg_id);

return gpg_id;
}
};
26 changes: 23 additions & 3 deletions src/gpg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,34 @@ pub const Gpg = struct {
});
defer self.allocator.free(absolute_path);

const args = .{
var args = ArrayList([]const u8).init(self.allocator);
defer args.deinit();

if (self.config.gpgId) |gpg_id| {
try args.appendSlice(&.{
"--default-key",
gpg_id,
});
}

var gpg_opts = std.mem.split(u8, self.config.gpgOpts, " ");
while (gpg_opts.next()) |opt| {
if (opt.len > 0) try args.append(opt);
}

try args.appendSlice(&.{
"--decrypt",
absolute_path,
};
});

var result = try self.execute(self.allocator, &args);
var result = try self.execute(self.allocator, args.items);
defer self.allocator.free(result.stderr);

// TODO: handle freeing of result.stdout

if (result.stdout.len == 0) {
defer self.allocator.free(result.stdout);

return error.InvalidOutput;
}

Expand Down

0 comments on commit 08f2c2a

Please sign in to comment.