diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 77ab5de5a..0fb2f5dcd 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -27,4 +27,6 @@ +- xo-cli minor + diff --git a/packages/xo-cli/index.mjs b/packages/xo-cli/index.mjs index 86c5e58dc..993aeab9f 100755 --- a/packages/xo-cli/index.mjs +++ b/packages/xo-cli/index.mjs @@ -46,34 +46,47 @@ async function connect() { return xo } -async function parseRegisterArgs(args) { +async function parseRegisterArgs(args, tokenDescription, acceptToken = false) { const { allowUnauthorized, expiresIn, - _: [ - url, + token, + _: opts, + } = getopts(args, { + alias: { + allowUnauthorized: 'au', + token: 't', + }, + boolean: ['allowUnauthorized'], + stopEarly: true, + string: ['expiresIn', 'token'], + }) + + const result = { + allowUnauthorized, + expiresIn: expiresIn || undefined, + url: opts[0], + } + + if (token !== '') { + if (!acceptToken) { + // eslint-disable-next-line no-throw-literal + throw '`token` option is not accepted by this command' + } + result.token = token + } else { + const [ + , email, password = await new Promise(function (resolve) { process.stdout.write('Password: ') pw(resolve) }), - ], - } = getopts(args, { - alias: { - allowUnauthorized: 'au', - }, - boolean: ['allowUnauthorized'], - stopEarly: true, - string: ['expiresIn'], - }) - - return { - allowUnauthorized, - email, - expiresIn: expiresIn || undefined, - password, - url, + ] = opts + result.token = await _createToken({ ...result, description: tokenDescription, email, password }) } + + return result } async function _createToken({ allowUnauthorized, description, email, expiresIn, password, url }) { @@ -193,16 +206,20 @@ const help = wrap( (function (pkg) { return `Usage: - $name --register [--allowUnauthorized] [--expiresIn duration] [] + $name --register [--allowUnauthorized] [--expiresIn ] [] + $name --register [--allowUnauthorized] [--expiresIn ] --token Registers the XO instance to use. --allowUnauthorized, --au Accept invalid certificate (e.g. self-signed). - --expiresIn duration + --expiresIn Can be used to change the validity duration of the authorization token (default: one month). + --token + An authentication token to use instead of username/password. + $name --createToken … Create an authentication token for XO API. @@ -294,10 +311,8 @@ async function main(args) { COMMANDS.help = help async function createToken(args) { - const opts = await parseRegisterArgs(args) - opts.description = 'xo-cli --createToken' + const { token } = await parseRegisterArgs(args, 'xo-cli --createToken') - const token = await _createToken(opts) console.warn('Authentication token created') console.warn() console.log(token) @@ -305,13 +320,11 @@ async function createToken(args) { COMMANDS.createToken = createToken async function register(args) { - const opts = await parseRegisterArgs(args) - opts.description = 'xo-cli --register' - + const opts = await parseRegisterArgs(args, 'xo-cli --register', true) await config.set({ allowUnauthorized: opts.allowUnauthorized, server: opts.url, - token: await _createToken(opts), + token: opts.token, }) } COMMANDS.register = register