/** * Configuration file management */ import { homedir } from 'os'; import { join } from 'path'; import { parse, stringify } from 'yaml'; import { logger } from '../utils/logger.js'; import type { ClientConfig, ServerConfig } from './schema.js'; import { ClientConfigSchema, ServerConfigSchema } from './schema.js'; const CONFIG_DIR = join(homedir(), '.config', 'usbip-supervisor'); const SERVER_CONFIG_PATH = join(CONFIG_DIR, 'server.yaml'); const CLIENT_CONFIG_PATH = join(CONFIG_DIR, 'client.yaml'); /** * Ensure config directory exists */ async function ensureConfigDir(): Promise { try { await Bun.write(join(CONFIG_DIR, '.keep'), ''); } catch (error) { // Directory creation handled by Bun.write } } /** * Load server configuration */ export async function loadServerConfig(customPath?: string): Promise { const configPath = customPath || SERVER_CONFIG_PATH; try { const file = Bun.file(configPath); const exists = await file.exists(); if (!exists) { logger.warn(`Config file not found: ${configPath}, using defaults`); return ServerConfigSchema.parse({}); } const content = await file.text(); const data = parse(content); const config = ServerConfigSchema.parse(data); logger.info(`Loaded server config from ${configPath}`); return config; } catch (error) { logger.error(`Failed to load server config: ${error}`); throw error; } } /** * Save server configuration */ export async function saveServerConfig(config: ServerConfig, customPath?: string): Promise { const configPath = customPath || SERVER_CONFIG_PATH; try { await ensureConfigDir(); const yaml = stringify(config); await Bun.write(configPath, yaml); logger.info(`Saved server config to ${configPath}`); } catch (error) { logger.error(`Failed to save server config: ${error}`); throw error; } } /** * Load client configuration */ export async function loadClientConfig(customPath?: string): Promise { const configPath = customPath || CLIENT_CONFIG_PATH; try { const file = Bun.file(configPath); const exists = await file.exists(); if (!exists) { logger.warn(`Config file not found: ${configPath}, using defaults`); throw new Error('Client config file not found. Run with --configure to create one.'); } const content = await file.text(); const data = parse(content); const config = ClientConfigSchema.parse(data); logger.info(`Loaded client config from ${configPath}`); return config; } catch (error) { logger.error(`Failed to load client config: ${error}`); throw error; } } /** * Save client configuration */ export async function saveClientConfig(config: ClientConfig, customPath?: string): Promise { const configPath = customPath || CLIENT_CONFIG_PATH; try { await ensureConfigDir(); const yaml = stringify(config); await Bun.write(configPath, yaml); logger.info(`Saved client config to ${configPath}`); } catch (error) { logger.error(`Failed to save client config: ${error}`); throw error; } } /** * Get default config paths */ export function getConfigPaths() { return { server: SERVER_CONFIG_PATH, client: CLIENT_CONFIG_PATH, dir: CONFIG_DIR, }; }