From 59d4890eae438886cdaf2a187dee00efb2358e93 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 4 Apr 2018 14:42:57 +0200 Subject: [PATCH] Add 'api-key generate' command Change-Type: minor --- capitanodoc.ts | 4 ++++ doc/cli.markdown | 20 ++++++++++++++++++++ lib/actions/api-key.ts | 27 +++++++++++++++++++++++++++ lib/actions/index.coffee | 3 ++- lib/app.coffee | 13 ++++++++----- package.json | 2 +- 6 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 lib/actions/api-key.ts diff --git a/capitanodoc.ts b/capitanodoc.ts index a7457fc7..991c40f4 100644 --- a/capitanodoc.ts +++ b/capitanodoc.ts @@ -63,6 +63,10 @@ environment variable (in the same standard URL format).\ `, categories: [ + { + title: 'Api keys', + files: [ 'build/actions/api-key.js' ], + }, { title: 'Application', files: [ 'build/actions/app.js' ] diff --git a/doc/cli.markdown b/doc/cli.markdown index a0ef074a..ee456355 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -62,6 +62,10 @@ environment variable (in the same standard URL format). # Table of contents +- Api keys + + - [api-key generate <name>](#api-key-generate-name-) + - Application - [app create <name>](#app-create-name-) @@ -180,6 +184,22 @@ environment variable (in the same standard URL format). - [util available-drives](#util-available-drives) +# Api keys + +## api-key generate <name> + + +This command generates a new API key for the current user, with the given +name. The key will be logged to the console. + +This key can be used to log into the CLI using 'resin login --token ', +or to authenticate requests to the API with an 'Authorization: Bearer ' header. + +Examples: + + $ resin api-key generate "Jenkins Key" + + # Application ## app create <name> diff --git a/lib/actions/api-key.ts b/lib/actions/api-key.ts new file mode 100644 index 00000000..c33cf229 --- /dev/null +++ b/lib/actions/api-key.ts @@ -0,0 +1,27 @@ +import { CommandDefinition } from 'capitano'; + +export const generate: CommandDefinition<{ + name: string; +}> = { + signature: 'api-key generate ', + description: 'Generate a new API key with the given name', + help: ` +This command generates a new API key for the current user, with the given +name. The key will be logged to the console. + +This key can be used to log into the CLI using 'resin login --token ', +or to authenticate requests to the API with an 'Authorization: Bearer ' header. + +Examples: + + $ resin api-key generate "Jenkins Key" +`, + async action(params, _options, done) { + const resin = (await import('resin-sdk')).fromSharedOptions(); + + resin.models.apiKey.create(params.name).then(key => { + console.log(`Registered api key '${params.name}':\n${key}`); + done(); + }); + }, +}; diff --git a/lib/actions/index.coffee b/lib/actions/index.coffee index 6137e9f9..b6303e11 100644 --- a/lib/actions/index.coffee +++ b/lib/actions/index.coffee @@ -16,9 +16,10 @@ limitations under the License. module.exports = wizard: require('./wizard') + apiKey: require('./api-key') app: require('./app') - info: require('./info') auth: require('./auth') + info: require('./info') device: require('./device') env: require('./environment-variables') keys: require('./keys') diff --git a/lib/app.coffee b/lib/app.coffee index 4faf13d7..6df8f88b 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -114,11 +114,8 @@ capitano.command(actions.help.help) # ---------- Wizard Module ---------- capitano.command(actions.wizard.wizard) -# ---------- Auth Module ---------- -capitano.command(actions.auth.login) -capitano.command(actions.auth.logout) -capitano.command(actions.auth.signup) -capitano.command(actions.auth.whoami) +# ---------- Api key module ---------- +capitano.command(actions.apiKey.generate) # ---------- App Module ---------- capitano.command(actions.app.create) @@ -127,6 +124,12 @@ capitano.command(actions.app.remove) capitano.command(actions.app.restart) capitano.command(actions.app.info) +# ---------- Auth Module ---------- +capitano.command(actions.auth.login) +capitano.command(actions.auth.logout) +capitano.command(actions.auth.signup) +capitano.command(actions.auth.whoami) + # ---------- Device Module ---------- capitano.command(actions.device.list) capitano.command(actions.device.supported) diff --git a/package.json b/package.json index d8efbe4f..d4f5a9fe 100644 --- a/package.json +++ b/package.json @@ -143,7 +143,7 @@ "resin-multibuild": "^0.5.1", "resin-preload": "^6.2.0", "resin-release": "^1.2.0", - "resin-sdk": "9.0.0-beta16", + "resin-sdk": "9.0.0-beta17", "resin-sdk-preconfigured": "^6.9.0", "resin-settings-client": "^3.6.1", "resin-stream-logger": "^0.1.0",