refactor(config): use new Environ methods and new Io

This commit is contained in:
utox39
2026-02-11 15:44:07 +01:00
parent a7d1baef92
commit f60c4e56b2
2 changed files with 7 additions and 7 deletions

View File

@@ -62,22 +62,22 @@ pub fn getModulesTypes(allocator: std.mem.Allocator, config: ?std.json.Parsed(Co
return modules_list; return modules_list;
} }
pub fn readConfigFile(allocator: std.mem.Allocator) !?std.json.Parsed(Config) { pub fn readConfigFile(allocator: std.mem.Allocator, io: std.Io, environ: std.process.Environ) !?std.json.Parsed(Config) {
const home = try std.process.getEnvVarOwned(allocator, "HOME"); const home = try std.process.Environ.getAlloc(environ, allocator, "HOME");
defer allocator.free(home); defer allocator.free(home);
const config_abs_path = try std.mem.concat(allocator, u8, &.{ home, "/.config/zigfetch/config.json" }); const config_abs_path = try std.mem.concat(allocator, u8, &.{ home, "/.config/zigfetch/config.json" });
defer allocator.free(config_abs_path); defer allocator.free(config_abs_path);
const config_file = std.fs.openFileAbsolute(config_abs_path, .{ .mode = .read_only }) catch |err| switch (err) { const config_file = std.Io.Dir.openFileAbsolute(io, config_abs_path, .{ .mode = .read_only }) catch |err| switch (err) {
error.FileNotFound => return null, error.FileNotFound => return null,
else => return err, else => return err,
}; };
defer config_file.close(); defer config_file.close(io);
const file_size = (try config_file.stat()).size; const file_size = (try config_file.stat(io)).size;
const config_data = try utils.readFile(allocator, config_file, file_size); const config_data = try utils.readFile(allocator, io, config_file, file_size);
defer allocator.free(config_data); defer allocator.free(config_data);
return try std.json.parseFromSlice(Config, allocator, config_data, .{ .allocate = .alloc_always }); return try std.json.parseFromSlice(Config, allocator, config_data, .{ .allocate = .alloc_always });

View File

@@ -21,7 +21,7 @@ pub fn main(init: std.process.Init) !void {
} }
} }
const conf = try config.readConfigFile(allocator, io); const conf = try config.readConfigFile(allocator, io, init.minimal.environ);
defer if (conf) |c| c.deinit(); defer if (conf) |c| c.deinit();
const modules_types = try config.getModulesTypes(allocator, conf); const modules_types = try config.getModulesTypes(allocator, conf);