OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-note / lib / cli / commands / sync.js

99 lines · javascript ref:
/**
 * Sync CLI Commands — P2P replication control
 */

import { HnClient } from '../client.js'
import { log, error } from '../output.js'

export async function syncCommand (subcommand, args, flags) {
  const client = new HnClient(flags)

  switch (subcommand) {
    case 'status': {
      const data = await client.get('/api/sync/status')
      log(`Swarm active: ${data.swarmActive}`)
      log(`Peers connected: ${data.peersConnected}`)
      if (data.announced.length > 0) {
        log(`Announced notebooks: ${data.announced.join(', ')}`)
      } else {
        log('No notebooks announced')
      }
      break
    }

    case 'announce': {
      const slug = args[0]
      if (!slug) {
        error('Usage: hn sync announce <slug>')
        return
      }
      const data = await client.post(`/api/notebooks/${slug}/announce`)
      log(`Announced: ${slug}`)
      log(`Discovery key: ${data.discoveryKey}`)
      break
    }

    case 'unannounce': {
      const slug = args[0]
      if (!slug) {
        error('Usage: hn sync unannounce <slug>')
        return
      }
      await client.post(`/api/notebooks/${slug}/unannounce`)
      log(`Unannounced: ${slug}`)
      break
    }

    case 'add': {
      const driveKey = args[0]
      if (!driveKey) {
        error('Usage: hn sync add <driveKey> [--slug name]')
        return
      }
      const slug = flags.slug || undefined
      const data = await client.post('/api/sync/add', { driveKey, slug })
      log(`Added remote notebook: ${data.slug}`)
      log(`Drive key: ${data.driveKey}`)
      break
    }

    case 'remove': {
      const slug = args[0]
      if (!slug) {
        error('Usage: hn sync remove <slug>')
        return
      }
      await client.delete(`/api/sync/remote/${slug}`)
      log(`Removed remote notebook: ${slug}`)
      break
    }

    case 'info': {
      const slug = args[0]
      if (!slug) {
        error('Usage: hn sync info <slug>')
        return
      }
      const data = await client.get(`/api/notebooks/${slug}/drive-info`)
      log(`Slug: ${data.slug}`)
      log(`Drive key: ${data.driveKey}`)
      log(`Discovery key: ${data.discoveryKey}`)
      log(`Local: ${data.isLocal}`)
      log(`Shared: ${data.isShared}`)
      log(`Announced: ${data.announced}`)
      break
    }

    default:
      log(`hn sync — P2P replication control

Commands:
  status                 Show sync overview (peers, announced notebooks)
  announce <slug>        Share a notebook on the swarm
  unannounce <slug>      Stop sharing a notebook
  add <driveKey> [--slug name]  Add a remote notebook by drive key
  remove <slug>          Remove a remote notebook
  info <slug>            Show drive key + sync status for a notebook`)
  }
}