/** pgh config set|get|list */
import { join } from '#node:path'
import { homedir } from '#node:os'
import { readFileSync, writeFileSync, mkdirSync } from '#node:fs'
const CONFIG_DIR = join(homedir(), '.config', 'pear-git-v3')
const CONFIG_FILE = join(CONFIG_DIR, 'config.json')
export const usage = `pgh config — local CLI config
pgh config list Show all config (default)
pgh config get <key>
pgh config set <key> <value>`
function loadConfig () {
try { return JSON.parse(readFileSync(CONFIG_FILE, 'utf8')) } catch { return {} }
}
function saveConfig (cfg) {
mkdirSync(CONFIG_DIR, { recursive: true })
writeFileSync(CONFIG_FILE, JSON.stringify(cfg, null, 2) + '\n')
}
export async function run (client, args) {
const sub = args[0]
if (!sub || sub === 'list' || sub === 'ls') return loadConfig()
if (sub === 'set') {
const key = args[1]
const value = args.slice(2).join(' ')
if (!key || !value) throw new Error('usage: pgh config set <key> <value>')
const cfg = loadConfig()
cfg[key] = value
saveConfig(cfg)
return { [key]: value }
}
if (sub === 'get') {
const key = args[1]
if (!key) throw new Error('usage: pgh config get <key>')
const cfg = loadConfig()
return { [key]: cfg[key] ?? null }
}
throw new Error('unknown config subcommand: ' + sub + '\n' + usage)
}