OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / lib / cli / commands / auth.js

76 lines · javascript ref:
/**
 * pgh auth commands
 *
 * Subcommands: status, whoami, test
 */

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

export async function authCommand (client, sub, args, flags) {
  switch (sub) {
    case 'status':
      return authStatus(client, flags)
    case 'whoami':
      return authWhoami(client, flags)
    case 'test':
      return authTest(client, flags)
    default:
      authHelp()
  }
}

async function authStatus (client, flags) {
  const data = await client.get('/api/admin/stats')

  if (flags.json) {
    jsonOut(data)
    return
  }

  log(`Server:       ${client.server}`)
  log(`Uptime:       ${Math.floor(data.server.uptime)}s`)
  log(`Repos:        ${data.server.repos}`)
  log(`Users:        ${data.registry.users}`)
  log(`Identities:   ${data.registry.identities}`)
  log(`Rate limits:  ${data.rateLimiter.cached} cached`)
}

async function authWhoami (client, flags) {
  const data = await client.get('/api/admin/stats')

  if (flags.json) {
    jsonOut(data)
    return
  }

  if (client.token) {
    log(`Authenticated via bearer token`)
    log(`Server: ${client.server}`)
  } else {
    log(`No token configured`)
    log(`Server: ${client.server}`)
  }
}

async function authTest (client, flags) {
  try {
    await client.get('/api/repos')
    log('Auth OK')
  } catch {
    error('Auth failed - check token and server URL')
  }
}

export function authHelp () {
  log(`Usage: pgh auth <subcommand> [flags]

Subcommands:
  status               Server status + auth info
  whoami               Show current auth identity
  test                 Test authentication

Flags:
  --json               Output as JSON
  --server URL         Server URL`)
}