Skip to content

Commit

Permalink
fix(cli): remove interactive prompting on CLI execute command (#1347)
Browse files Browse the repository at this point in the history
using Debug instead of `console.log` for logging, so that the console can be used for stdout.

fixes #1281
  • Loading branch information
mirceanis committed Feb 22, 2024
1 parent 8697e97 commit d8e2167
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 101 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"json-schema": "0.4.0",
"lerna": "7.3.0",
"lerna-changelog": "2.2.0",
"oas-resolver": "2.5.6",
"openapi-types": "12.1.3",
"prettier": "3.0.2",
"rimraf": "5.0.1",
Expand Down
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"json-schema": "^0.4.0",
"json5": "^2.2.3",
"jsonpointer": "^5.0.1",
"oas-resolver": "^2.5.6",
"openapi-types": "^12.1.3",
"passport": "^0.6.0",
"passport-http-bearer": "^1.0.1",
Expand Down
70 changes: 11 additions & 59 deletions packages/cli/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Command } from 'commander'
import inquirer from 'inquirer'
import { getAgent } from './setup.js'
import fs from 'fs'
import OasResolver from 'oas-resolver'
import fuzzy from 'fuzzy'
import Debug from 'debug'

const debug = Debug('veramo:cli:execute')

const execute = new Command('execute')
.description('Execute agent method')
Expand All @@ -19,8 +21,6 @@ const execute = new Command('execute')
let argsFile = options.argsFile
let argsObj

const { openapi } = await OasResolver.resolve(agent.getSchema(), null, { resolveInternal: true })

if (!method) {
const answers = await inquirer.prompt({
// @ts-ignore
Expand All @@ -38,78 +38,30 @@ const execute = new Command('execute')
})
method = answers.method
}
const methodApi = openapi.components.methods[method]

if (!argsString && !argsFile) {
// parse schema, generate options

const questions = []
// console.log(methodApi.arguments.type)
if (methodApi.arguments.type === 'object') {
for (const property in methodApi.arguments.properties) {
const propertySchema = methodApi.arguments.properties[property]
// console.log({property, propertySchema})
let question: any = {
name: property,
message: property + ' - ' + propertySchema.description,
}

// TODO handle anyOf
switch (propertySchema.type) {
case 'object':
question.type = 'input'
question.filter = (input: string) => JSON.parse(input === '' ? '{}' : input)
question.transformer = (input: object) => JSON.stringify(input)
break
case 'string':
question.type = 'input'
break
case 'number':
question.type = 'number'
break
case 'boolean':
question.type = 'confirm'
break
// TODO
case 'array':
default:
console.log(`Method argument type ${propertySchema.type} not supported yet`)
process.exit(1)
}

if (question.type) {
if (propertySchema.enum) {
question.type = 'list'
question.choices = propertySchema.enum
}
questions.push(question)
}
}
}

argsObj = await inquirer.prompt(questions)
console.error(`No arguments provided for execute method=${method}`)
process.exit(1)
} else {
debug(`Attempting to extract method arguments from file (${argsFile})`)
if (argsFile) {
console.log({ argsFile })
argsString = fs.readFileSync(argsFile).toString('utf-8')
}
try {
argsObj = JSON.parse(argsString!)
} catch (e: any) {
console.error('could not parse arguments JSON')
process.exit(1)
}
}

console.log('\nMethod: ', method)
console.log('\nArguments: ', JSON.stringify(argsObj, null, 2))
debug('\nMethod: ', method)
debug('\nArguments: ', JSON.stringify(argsObj, null, 2))

const result = await agent.execute(method, argsObj)

console.log(
'\nResult',
methodApi.returnType.description ? `(${methodApi.returnType.description}):` : ':',
JSON.stringify(result, null, 2),
)
debug('\nResult: ', JSON.stringify(result, null, 2))
console.log(JSON.stringify(result))
} catch (e: any) {
console.error(e.message)
}
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/imported-types/imported-modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
declare module 'cors'
declare module 'oas-resolver'
6 changes: 3 additions & 3 deletions packages/credential-w3c/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,10 @@ export class CredentialPlugin implements IAgentPlugin {

/**
* Checks if a key is suitable for signing JWT payloads.
* @param key
* @param context
* @param key - the key to check for support
* @param context - This reserved param is automatically added and handled by the framework, *do not override*
*
* @internal
* @beta
*/
async matchKeyForJWT(key: IKey, context: IssuerAgentContext): Promise<boolean> {
switch (key.type) {
Expand Down
37 changes: 1 addition & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d8e2167

Please sign in to comment.