const std = @import("std"); const types = @import("types.zig"); const errors = @import("errors.zig"); pub fn load(arena: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map) !types.NinaConfig { const home = environ.get("HOME") orelse { errors.error_info.setSuggestion("set HOME environment variable", .{}); return error.ConfigNotFound; }; const path = try std.fmt.allocPrint(arena, "{s}/.nina.conf", .{home}); const content = std.Io.Dir.readFileAlloc(.cwd(), io, path, arena, .unlimited) catch { errors.error_info.setSuggestion("run nina to set up", .{}); return error.ConfigNotFound; }; return parse(arena, content, home); } fn parse(arena: std.mem.Allocator, input: []const u8, home: []const u8) !types.NinaConfig { var config = types.NinaConfig{}; var machines: std.ArrayList(types.Machine) = .empty; var current: ?types.Machine = null; var lines = std.mem.splitScalar(u8, input, '\n'); while (lines.next()) |line| { const trimmed = std.mem.trim(u8, line, " \t\r"); if (trimmed.len == 0 or trimmed[0] == '#') continue; if (std.mem.eql(u8, trimmed, "[machine]")) { if (current) |m| try machines.append(arena, m); current = .{}; continue; } const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; const key = std.mem.trim(u8, trimmed[0..eq], " \t"); const val = std.mem.trim(u8, trimmed[eq + 1 ..], " \t"); if (current) |*m| { setMachineField(arena, m, key, val, home) catch {}; } else { setConfigField(arena, &config, key, val) catch {}; } } if (current) |m| try machines.append(arena, m); config.machines = try machines.toOwnedSlice(arena); return config; } fn setConfigField(arena: std.mem.Allocator, config: *types.NinaConfig, key: []const u8, val: []const u8) !void { if (std.mem.eql(u8, key, "editor")) { config.editor = try arena.dupe(u8, val); } else if (std.mem.eql(u8, key, "generations")) { config.generations = std.fmt.parseInt(u32, val, 10) catch 5; } else if (std.mem.eql(u8, key, "confirm")) { config.confirm = std.mem.eql(u8, val, "true"); } else if (std.mem.eql(u8, key, "teach")) { config.teach = std.mem.eql(u8, val, "true"); } else if (std.mem.eql(u8, key, "color")) { config.color = std.mem.eql(u8, val, "true"); } } fn setMachineField(arena: std.mem.Allocator, m: *types.Machine, key: []const u8, val: []const u8, home: []const u8) !void { if (std.mem.eql(u8, key, "name")) { m.name = try arena.dupe(u8, val); } else if (std.mem.eql(u8, key, "config")) { m.config_path = try arena.dupe(u8, val); } else if (std.mem.eql(u8, key, "local")) { m.local = std.mem.eql(u8, val, "true"); } else if (std.mem.eql(u8, key, "default")) { m.default = std.mem.eql(u8, val, "true"); } else if (std.mem.eql(u8, key, "host")) { m.host = try arena.dupe(u8, val); } else if (std.mem.eql(u8, key, "user")) { m.user = try arena.dupe(u8, val); } else if (std.mem.eql(u8, key, "ssh_key")) { m.ssh_key = try expandHome(arena, val, home); } } fn expandHome(arena: std.mem.Allocator, path: []const u8, home: []const u8) ![]const u8 { if (!std.mem.startsWith(u8, path, "~")) return arena.dupe(u8, path); return std.fmt.allocPrint(arena, "{s}{s}", .{ home, path[1..] }); } pub fn resolveMachine(config: *const types.NinaConfig, name: ?[]const u8) !types.Machine { if (name) |n| { for (config.machines) |m| { if (std.mem.eql(u8, m.name, n)) return m; } errors.error_info.setMessage("'{s}' is not in your config", .{n}); errors.error_info.setSuggestion("check ~/.nina.conf", .{}); return error.MachineNotFound; } for (config.machines) |m| { if (m.default) return m; } if (config.machines.len > 0) return config.machines[0]; // No machines defined — return a default local machine return .{}; } test "parse basic config" { const input = \\editor = hx \\generations = 3 \\confirm = false \\teach = true \\ \\[machine] \\name = test-box \\local = true \\default = true ; const config = try parse(std.testing.allocator, input, "/home/test"); defer std.testing.allocator.free(config.machines); try std.testing.expectEqualStrings("hx", config.editor); try std.testing.expectEqual(@as(u32, 3), config.generations); try std.testing.expect(!config.confirm); try std.testing.expect(config.teach); try std.testing.expectEqual(@as(usize, 1), config.machines.len); try std.testing.expectEqualStrings("test-box", config.machines[0].name); } test "resolve machine by name" { var machines = [_]types.Machine{ .{ .name = "alpha", .default = true }, .{ .name = "beta" }, }; const config = types.NinaConfig{ .machines = &machines }; const m = try resolveMachine(&config, "beta"); try std.testing.expectEqualStrings("beta", m.name); } test "resolve default machine" { var machines = [_]types.Machine{ .{ .name = "alpha", .default = true }, .{ .name = "beta" }, }; const config = types.NinaConfig{ .machines = &machines }; const m = try resolveMachine(&config, null); try std.testing.expectEqualStrings("alpha", m.name); }