import * as fs from "node:fs"; import * as path from "node:path"; import type { DockerLogsConfig } from "./types"; import { DEFAULT_CONFIG } from "./types"; export function loadConfig(cwd: string): DockerLogsConfig { const configPath = path.join(cwd, ".dockerlogs.yml"); if (!fs.existsSync(configPath)) return { ...DEFAULT_CONFIG }; try { const content = fs.readFileSync(configPath, "utf-8"); const result: Record = {}; for (const line of content.split("\n")) { const m = line.match(/^\s*(\w[\w.]*):\s*(.+)$/); if (m) { let val = m[2].trim(); if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) { val = val.slice(1, -1); } result[m[1]] = val; } } return { const tail = parseInt(result["tail"] as string); return { tail: isNaN(tail) ? DEFAULT_CONFIG.tail : tail, }; } catch { return { ...DEFAULT_CONFIG }; } }