Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/commands/beo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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({
Expand Down
6 changes: 5 additions & 1 deletion src/commands/ieo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ 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()

program
.name('bsp')
.version('1.0.0')
.description('Biological Sovereignty Protocol — CLI\nhttps://biologicalsovereigntyprotocol.com')
.option('--output <format>', '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)
Expand Down
19 changes: 19 additions & 0 deletions src/lib/output.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}

Expand All @@ -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<string, string | number | boolean | null>): 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)
Expand Down
Loading