respect xdg config, and recursively mkdir if .config doesnt exist.
Browse files
+20 −2 src/main.zig
| @@ -105,9 +105,13 @@ fn print_help() void { | |||
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | fn get_config_path(allocator: std.mem.Allocator) ![]u8 { | 107 | fn get_config_path(allocator: std.mem.Allocator) ![]u8 { |
| 108 | - const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir; | 108 | + const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: { |
| 109 | - const config_path = try std.fs.path.join(allocator, &.{ home, ".config", "oxwm", "config.lua" }); | 109 | + const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir; |
| 110 | + break :blk try std.fs.path.join(allocator, &.{ home, ".config" }); | ||
| 111 | + }; | ||
| 112 | + defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home); | ||
| 110 | 113 | ||
| 114 | + const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" }); | ||
| 111 | return config_path; | 115 | return config_path; |
| 112 | } | 116 | } |
| 113 | 117 | ||
| @@ -117,6 +121,20 @@ fn init_config(allocator: std.mem.Allocator) void { | |||
| 117 | 121 | ||
| 118 | const template = @embedFile("templates/config.lua"); | 122 | const template = @embedFile("templates/config.lua"); |
| 119 | 123 | ||
| 124 | + if (std.fs.path.dirname(config_path)) |dir_path| { | ||
| 125 | + var root = std.fs.openDirAbsolute("/", .{}) catch |err| { | ||
| 126 | + std.debug.print("error: could not open root directory: {}\n", .{err}); | ||
| 127 | + return; | ||
| 128 | + }; | ||
| 129 | + defer root.close(); | ||
| 130 | + | ||
| 131 | + const relative_path = std.mem.trimLeft(u8, dir_path, "/"); | ||
| 132 | + root.makePath(relative_path) catch |err| { | ||
| 133 | + std.debug.print("error: could not create config directory: {}\n", .{err}); | ||
| 134 | + return; | ||
| 135 | + }; | ||
| 136 | + } | ||
| 137 | + | ||
| 120 | const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| { | 138 | const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| { |
| 121 | std.debug.print("error: could not create config file: {}\n", .{err}); | 139 | std.debug.print("error: could not create config file: {}\n", .{err}); |
| 122 | return; | 140 | return; |