/**
* pgh profile commands
*
* Subcommands: view, set
*/
import { jsonOut, log, error } from '../output.js'
export async function profileCommand (client, sub, args, flags) {
switch (sub) {
case 'view':
case undefined:
return profileView(client, flags)
case 'set':
return profileSet(client, args[0], args[1], flags)
default:
profileHelp()
}
}
async function profileView (client, flags) {
const data = await client.get('/api/admin/stats')
if (flags.json) {
jsonOut(data)
return
}
log(`Server: ${client.server}`)
log(`Users: ${data.registry.users}`)
log(`Identities: ${data.registry.identities}`)
log(`Repos: ${data.server.repos}`)
}
async function profileSet (client, key, value, flags) {
if (!key || value === undefined) error('Usage: pgh profile set <key> <value>')
// Build update object from key/value
const updates = {}
if (key === 'name') updates.name = value
else if (key === 'permissions') updates.globalPermissions = value.split(',')
else error(`Unknown profile key: ${key}. Valid keys: name, permissions`)
// Need a userId - get from current auth
const userId = flags.user || flags.userId
if (!userId) error('Usage: pgh profile set <key> <value> --user <userId>')
const data = await client.patch(`/api/admin/users/${encodeURIComponent(userId)}`, updates)
if (flags.json) {
jsonOut(data)
return
}
log(`Updated ${key}.`)
}
export function profileHelp () {
log(`Usage: pgh profile <subcommand> [args] [flags]
Subcommands:
view View server profile info
set <key> <value> Update profile (requires --user)
Keys:
name Display name
permissions Comma-separated global permissions
Flags:
--user ID User ID for updates
--json Output as JSON`)
}