/**
* hn share — Notebook sharing commands
*/
import { HnClient } from '../client.js'
import { table, log, error, jsonOut } from '../output.js'
export async function shareCommand (subcommand, cmdArgs, flags) {
const client = new HnClient(flags)
switch (subcommand) {
case 'create':
return await createShare(client, cmdArgs, flags)
case 'list':
return await listShares(client, cmdArgs, flags)
case 'revoke':
return await revokeShare(client, cmdArgs, flags)
default:
error('Usage: hn share [create|list|revoke] <notebook> [options]')
}
}
async function createShare (client, cmdArgs, flags) {
const notebook = cmdArgs[0]
if (!notebook) {
error('Usage: hn share create <notebook> [--permissions read|write] [--label "..."]')
}
const permissions = flags.permissions
? flags.permissions.split(',').map(p => p.trim())
: ['read']
const label = flags.label || ''
const result = await client.post(`/api/notebooks/${encodeURIComponent(notebook)}/share`, {
permissions,
label
})
if (flags.json) {
jsonOut(result)
return
}
log(`Share token created for notebook '${notebook}'`)
log('')
log(` Token: ${result.token}`)
log(` Token Hash: ${result.tokenHash}`)
log(` Permissions: ${result.permissions.join(', ')}`)
if (result.label) log(` Label: ${result.label}`)
log('')
log('Share this token with the recipient. They can configure it with:')
log(` hn config set token ${result.token}`)
}
async function listShares (client, cmdArgs, flags) {
const notebook = cmdArgs[0]
if (!notebook) {
error('Usage: hn share list <notebook>')
}
const shares = await client.get(`/api/notebooks/${encodeURIComponent(notebook)}/shares`)
if (flags.json) {
jsonOut(shares)
return
}
if (shares.length === 0) {
log(`No shares for notebook '${notebook}'.`)
return
}
table(shares, [
{ key: 'tokenHash', label: 'TOKEN HASH', width: 16 },
{ key: 'permissions', label: 'PERMISSIONS', width: 14 },
{ key: 'label', label: 'LABEL', width: 20 },
{ key: 'sharedBy', label: 'SHARED BY', width: 16 },
{ key: 'sharedAt', label: 'SHARED AT', width: 24 }
])
}
async function revokeShare (client, cmdArgs, flags) {
const notebook = cmdArgs[0]
const tokenHash = cmdArgs[1]
if (!notebook || !tokenHash) {
error('Usage: hn share revoke <notebook> <tokenHash>')
}
const result = await client.del(`/api/notebooks/${encodeURIComponent(notebook)}/shares/${encodeURIComponent(tokenHash)}`)
if (flags.json) {
jsonOut(result)
return
}
log(`Revoked share ${tokenHash.substring(0, 16)}... for notebook '${notebook}'`)
}
export default shareCommand