feat: add windows support

This commit is contained in:
CJ van den Berg 2024-11-18 17:39:35 +01:00
parent 9e96cbb4c3
commit e3cef17b63
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const cbor = @import("cbor"); const cbor = @import("cbor");
const builtin = @import("builtin");
const application_name = "flow"; const application_name = "flow";
@ -48,18 +49,36 @@ pub fn get_config_dir() ![]const u8 {
} }
fn get_app_config_dir(appname: []const u8) ![]const u8 { fn get_app_config_dir(appname: []const u8) ![]const u8 {
const a = std.heap.c_allocator;
const local = struct { const local = struct {
var config_dir_buffer: [std.posix.PATH_MAX]u8 = undefined; var config_dir_buffer: [std.posix.PATH_MAX]u8 = undefined;
var config_dir: ?[]const u8 = null; var config_dir: ?[]const u8 = null;
}; };
const config_dir = if (local.config_dir) |dir| const config_dir = if (local.config_dir) |dir|
dir dir
else if (std.posix.getenv("XDG_CONFIG_HOME")) |xdg| else if (std.process.getEnvVarOwned(a, "XDG_CONFIG_HOME") catch null) |xdg| ret: {
try std.fmt.bufPrint(&local.config_dir_buffer, "{s}/{s}", .{ xdg, appname }) defer a.free(xdg);
else if (std.posix.getenv("HOME")) |home| break :ret try std.fmt.bufPrint(&local.config_dir_buffer, "{s}/{s}", .{ xdg, appname });
try std.fmt.bufPrint(&local.config_dir_buffer, "{s}/.config/{s}", .{ home, appname }) } else if (std.process.getEnvVarOwned(a, "HOME") catch null) |home| ret: {
else defer a.free(home);
return error.AppConfigDirUnavailable; const dir = try std.fmt.bufPrint(&local.config_dir_buffer, "{s}/.config", .{home});
std.fs.makeDirAbsolute(dir) catch |e| switch (e) {
error.PathAlreadyExists => {},
else => return e,
};
break :ret try std.fmt.bufPrint(&local.config_dir_buffer, "{s}/.config/{s}", .{ home, appname });
} else if (builtin.os.tag == .windows) ret: {
if (std.process.getEnvVarOwned(a, "APPDATA") catch null) |appdata| {
defer a.free(appdata);
const dir = try std.fmt.bufPrint(&local.config_dir_buffer, "{s}/{s}", .{ appdata, appname });
std.fs.makeDirAbsolute(dir) catch |e| switch (e) {
error.PathAlreadyExists => {},
else => return e,
};
break :ret dir;
} else return error.AppConfigDirUnavailable;
} else return error.AppConfigDirUnavailable;
local.config_dir = config_dir; local.config_dir = config_dir;
std.fs.makeDirAbsolute(config_dir) catch |e| switch (e) { std.fs.makeDirAbsolute(config_dir) catch |e| switch (e) {
error.PathAlreadyExists => {}, error.PathAlreadyExists => {},