diff --git a/src/commands/beo.ts b/src/commands/beo.ts index 6e2484f..14e0861 100644 --- a/src/commands/beo.ts +++ b/src/commands/beo.ts @@ -2,7 +2,7 @@ import { Command } from 'commander' import { CryptoUtils } from '@biological-sovereignty-protocol/sdk' import { loadConfig } from '../lib/config.js' import * as api from '../lib/api.js' -import { success, error, table, info, warn, requireKey } from '../lib/output.js' +import { success, error, table, info, warn, requireKey, json, getOutputFormat } from '../lib/output.js' export function registerBEOCommands(program: Command) { const beo = program.command('create') @@ -51,6 +51,10 @@ export function registerBEOCommands(program: Command) { try { if (!domain.endsWith('.bsp')) domain += '.bsp' const result = await api.get(`/api/beos/domain/${encodeURIComponent(domain)}`) + if (getOutputFormat() === 'json') { + json(result.beo) + return + } success(`BEO found: ${domain}`) console.log() table({ diff --git a/src/commands/ieo.ts b/src/commands/ieo.ts index 1a4d30a..5b6a8e4 100644 --- a/src/commands/ieo.ts +++ b/src/commands/ieo.ts @@ -2,7 +2,7 @@ import { Command } from 'commander' import { CryptoUtils } from '@biological-sovereignty-protocol/sdk' import { loadConfig } from '../lib/config.js' import * as api from '../lib/api.js' -import { success, error, table, info, warn, requireKey } from '../lib/output.js' +import { success, error, table, info, warn, requireKey, json, getOutputFormat } from '../lib/output.js' export function registerIEOCommands(program: Command) { const ieo = program.command('ieo').description('Manage Institutional Entity Objects') @@ -130,6 +130,10 @@ export function registerIEOCommands(program: Command) { const qs = params.toString() const result = await api.get(`/api/ieos${qs ? '?' + qs : ''}`) + if (getOutputFormat() === 'json') { + json(result.ieos) + return + } success(`${result.count} IEO(s) found`) for (const ieo of result.ieos) { console.log() diff --git a/src/index.ts b/src/index.ts index aa6aefd..669f14e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { registerConsentCommands } from './commands/consent.js' import { registerIEOCommands } from './commands/ieo.js' import { registerExchangeCommands } from './commands/exchange.js' import { registerConfigCommands } from './commands/config.js' +import { setOutputFormat } from './lib/output.js' const program = new Command() @@ -13,6 +14,11 @@ program .name('bsp') .version('1.0.0') .description('Biological Sovereignty Protocol — CLI\nhttps://biologicalsovereigntyprotocol.com') + .option('--output ', 'Output format: table, json', 'table') + .hook('preAction', (thisCommand) => { + const opts = thisCommand.opts() + if (opts.output) setOutputFormat(opts.output as 'table' | 'json') + }) registerBEOCommands(program) registerConsentCommands(program) diff --git a/src/lib/output.ts b/src/lib/output.ts index 9964e09..c77adbf 100644 --- a/src/lib/output.ts +++ b/src/lib/output.ts @@ -1,4 +1,17 @@ +type OutputFormat = 'table' | 'json' + +let _outputFormat: OutputFormat = 'table' + +export function setOutputFormat(fmt: OutputFormat): void { + _outputFormat = fmt +} + +export function getOutputFormat(): OutputFormat { + return _outputFormat +} + export function success(msg: string): void { + if (_outputFormat === 'json') return console.log(`\x1b[32m✓\x1b[0m ${msg}`) } @@ -7,14 +20,20 @@ export function error(msg: string): void { } export function warn(msg: string): void { + if (_outputFormat === 'json') return console.log(`\x1b[33m!\x1b[0m ${msg}`) } export function info(msg: string): void { + if (_outputFormat === 'json') return console.log(` ${msg}`) } export function table(rows: Record): void { + if (_outputFormat === 'json') { + console.log(JSON.stringify(rows, null, 2)) + return + } const maxKey = Math.max(...Object.keys(rows).map(k => k.length)) for (const [key, val] of Object.entries(rows)) { const label = key.padEnd(maxKey)