/**
* Link CLI Commands — Multi-writer device linking
*/
import { HnClient } from '../client.js'
import { log, error } from '../output.js'
export async function linkCommand (subcommand, args, flags) {
const client = new HnClient(flags)
switch (subcommand) {
case 'create': {
log('Creating link invite (waiting for peer)...')
const data = await client.post('/api/multiwriter/link', {})
log(`Invite: ${data.invite}`)
log('')
log('Share this invite with the other device.')
log('On the other device, run: hn link join <invite>')
break
}
case 'join': {
const invite = args[0]
if (!invite) {
error('Usage: hn link join <invite-hex>')
return
}
log('Joining...')
const data = await client.post('/api/multiwriter/join', { invite })
log('Linked successfully!')
if (data.localWriterKey) {
log(`Writer key: ${data.localWriterKey}`)
}
break
}
case 'status': {
const data = await client.get('/api/multiwriter/status')
log(`Linked: ${data.linked}`)
const regKey = data.registryKey
? (typeof data.registryKey === 'string' ? data.registryKey : Buffer.from(data.registryKey).toString('hex'))
: 'none'
log(`Registry key: ${regKey}`)
log(`Peers: ${data.linkedPeers || 0}`)
log(`Mirrored drives: ${data.mirroredDrives || 0}`)
log(`Shared users: ${data.sharedUsers || 0}`)
if (data.registeredDrives && data.registeredDrives.length > 0) {
log('Drives:')
for (const d of data.registeredDrives) {
log(` ${d.slug} (${d.driveKey.substring(0, 16)}...)`)
}
}
break
}
case 'sync': {
log('Syncing...')
const data = await client.post('/api/multiwriter/sync', {})
log(`Drives: ${data.mirroredDrives || 0}, Users: ${data.sharedUsers || 0}, Peers: ${data.linkedPeers || 0}`)
break
}
case 'add-writer': {
const key = args[0]
if (!key) {
error('Usage: hn link add-writer <writer-key>')
return
}
await client.post('/api/multiwriter/add-writer', { writerKey: key })
log('Writer added.')
break
}
default:
log(`hn link — Device linking
Commands:
create Create link invite
join <hex> Join with invite
status Show link status
sync Trigger mirror sync
add-writer <k> Add writer key (fallback)`)
}
}