OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / bin / pear-git-cli

160 lines · text ref:
#!/usr/bin/env bare
/**
 * pear-git CLI - Manage P2P remotes and petnames
 *
 * Usage:
 *   pear-git remote add <name> <key>    Add a petname for a server
 *   pear-git remote remove <name>       Remove a petname
 *   pear-git remote list                List all petnames
 *   pear-git info                       Show local identity info
 */

// Load Node.js compatibility for Bare runtime
await import('bare-node-runtime/global')

import { PetnameManager } from '../lib/p2p/discovery/petnames.js'

const args = process.argv.slice(2)
const command = args[0]
const subcommand = args[1]

async function main() {
  const petnames = new PetnameManager()
  await petnames.init()

  switch (command) {
    case 'remote':
      await handleRemote(petnames, subcommand, args.slice(2))
      break

    case 'info':
      await handleInfo(petnames)
      break

    case 'help':
    case '--help':
    case '-h':
    default:
      showHelp()
      break
  }
}

async function handleRemote(petnames, subcommand, args) {
  switch (subcommand) {
    case 'add': {
      const [name, key] = args
      if (!name || !key) {
        console.error('Usage: pear-git remote add <name> <key>')
        process.exit(1)
      }

      try {
        const result = await petnames.add(name, key)
        console.log(`Added petname: ${result.name} -> ${result.key.slice(0, 16)}...`)
        console.log('')
        console.log('You can now clone with:')
        console.log(`  git clone pear://${name}/<repo>`)
      } catch (err) {
        console.error(`Error: ${err.message}`)
        process.exit(1)
      }
      break
    }

    case 'remove':
    case 'rm': {
      const [name] = args
      if (!name) {
        console.error('Usage: pear-git remote remove <name>')
        process.exit(1)
      }

      try {
        await petnames.remove(name)
        console.log(`Removed petname: ${name}`)
      } catch (err) {
        console.error(`Error: ${err.message}`)
        process.exit(1)
      }
      break
    }

    case 'list':
    case 'ls': {
      const remotes = petnames.list()

      if (remotes.length === 0) {
        console.log('No petnames configured.')
        console.log('')
        console.log('Add one with:')
        console.log('  pear-git remote add <name> <server-key>')
        return
      }

      console.log('Configured petnames:')
      console.log('')
      for (const remote of remotes) {
        console.log(`  ${remote.name}`)
        console.log(`    Key: ${remote.key.slice(0, 16)}...`)
        console.log(`    Added: ${new Date(remote.addedAt).toLocaleDateString()}`)
        console.log('')
      }
      break
    }

    default:
      console.error(`Unknown remote command: ${subcommand}`)
      console.error('Try: add, remove, list')
      process.exit(1)
  }
}

async function handleInfo(petnames) {
  const remotes = petnames.list()

  console.log('Pear-Git P2P Configuration')
  console.log('==========================')
  console.log('')
  console.log(`Config directory: ${petnames.configDir}`)
  console.log(`Petnames: ${remotes.length}`)
  console.log('')

  if (remotes.length > 0) {
    console.log('Use `pear-git remote list` to see all petnames.')
  } else {
    console.log('No petnames configured yet.')
    console.log('')
    console.log('To add a remote server:')
    console.log('  pear-git remote add myserver <64-char-hex-key>')
  }
}

function showHelp() {
  console.log(`
pear-git - P2P Git remote management

Usage:
  pear-git remote add <name> <key>    Add a petname for a server
  pear-git remote remove <name>       Remove a petname
  pear-git remote list                List all petnames
  pear-git info                       Show configuration info
  pear-git help                       Show this help

Examples:
  # Add a petname for a server
  pear-git remote add tabertron zupx3qzpwj61hdt1bxanabctcyxqqu3arogctwim4qt3wfbzn4bo

  # Clone using the petname
  git clone pear://tabertron/wirecore

  # Or use the full key directly
  git clone pear://zupx3qzpwj61hdt1bxanabctcyxqqu3arogctwim4qt3wfbzn4bo/wirecore
`)
}

main().catch(err => {
  console.error(`Error: ${err.message}`)
  process.exit(1)
})