import fs from '#node:fs'
import path from '#node:path'
export const usage = `pgh attach <file> — upload a file as a content-addressed attachment
Uploads <file> to the pear-git-v3 daemon over the local UDS (calls the
upload-attachment RPC) and prints the resulting oid. The daemon reads the file
directly by path — no base64 — so it must run on this machine (local UDS).
Serve it back at /files/raw?path=/attachments/<oid>; it replicates cross-daemon
over the V2 overlay. Local UDS = the daemon owner (trusted); no session needed.`
export async function run (client, args, globals) {
const file = args && args[0]
if (!file) { console.error('usage: pgh attach <file>'); process.exit(1) }
const abs = path.resolve(file)
if (!fs.existsSync(abs)) { console.error('pgh attach: file not found: ' + abs); process.exit(1) }
const size = fs.statSync(abs).size
// Local UDS: hand the daemon the absolute path so it reads the bytes directly
// (no base64 blowup on large files). Cross-machine callers can send { data }.
const result = await client.call('upload-attachment', { path: abs })
if (globals && globals.json) { console.log(JSON.stringify(result)); return }
console.log(result.hash)
console.error(' ' + size + ' bytes → ' + result.path)
}