/**
* pgh label commands
*
* Subcommands: list, create, update, delete
*/
import { table, jsonOut, log, error } from '../output.js'
import { getRepo, getNamespace } from '../config.js'
function resolveRepo (flags) {
const repo = getRepo(flags)
if (!repo) error('Repository required. Use --repo <name> or set defaultRepo in config.')
return repo
}
function resolveNs (flags) {
return getNamespace(flags) || '_'
}
export async function labelCommand (client, sub, args, flags) {
switch (sub) {
case 'list':
case 'ls':
return labelList(client, flags)
case 'create':
case 'new':
return labelCreate(client, args[0], flags)
case 'update':
return labelUpdate(client, args[0], flags)
case 'delete':
case 'rm':
return labelDelete(client, args[0], flags)
default:
labelHelp()
}
}
async function labelList (client, flags) {
const repo = resolveRepo(flags)
const ns = resolveNs(flags)
const data = await client.get(`/${ns}/${repo}/labels`)
if (flags.json) {
jsonOut(data)
return
}
const labels = data.labels || []
if (labels.length === 0) {
log('No labels.')
return
}
table(labels, [
{ key: 'name', label: 'NAME', width: 20 },
{ key: 'color', label: 'COLOR', width: 10 },
{ key: 'description', label: 'DESCRIPTION', width: 40 }
])
}
async function labelCreate (client, name, flags) {
if (!name) error('Usage: pgh label create <name> --color <hex>')
const color = flags.color
if (!color) error('Label color required. Use --color <hex>')
const repo = resolveRepo(flags)
const ns = resolveNs(flags)
const body = {
name,
color: color.startsWith('#') ? color : '#' + color,
description: flags.description || flags.d || ''
}
const data = await client.post(`/${ns}/${repo}/labels`, body)
if (flags.json) {
jsonOut(data)
return
}
log(`Label "${name}" created`)
}
async function labelUpdate (client, name, flags) {
if (!name) error('Usage: pgh label update <name> [--color <hex>] [--description <text>]')
const repo = resolveRepo(flags)
const ns = resolveNs(flags)
const body = {}
if (flags.color) body.color = flags.color.startsWith('#') ? flags.color : '#' + flags.color
if (flags.description || flags.d) body.description = flags.description || flags.d
if (flags.displayName) body.displayName = flags.displayName
const data = await client.patch(`/${ns}/${repo}/labels/${encodeURIComponent(name)}`, body)
if (flags.json) {
jsonOut(data)
return
}
log(`Label "${name}" updated`)
}
async function labelDelete (client, name, flags) {
if (!name) error('Usage: pgh label delete <name>')
const repo = resolveRepo(flags)
const ns = resolveNs(flags)
await client.del(`/${ns}/${repo}/labels/${encodeURIComponent(name)}`)
log(`Label "${name}" deleted`)
}
export function labelHelp () {
log(`Usage: pgh label <subcommand> [args] [flags]
Subcommands:
list List all labels
create <name> --color <hex> Create a label
update <name> Update a label
delete <name> Delete a label
Flags:
--repo <name> Repository name
--namespace <ns> Namespace (default: _)
--color <hex> Label color (e.g. ff0000 or #ff0000)
-d, --description <text> Label description
--displayName <text> Label display name
--json Output as JSON`)
}