2018-04-04 12:42:57 +00:00
|
|
|
import { CommandDefinition } from 'capitano';
|
2018-04-11 17:31:03 +00:00
|
|
|
import { stripIndent } from 'common-tags';
|
2020-02-27 14:55:30 +00:00
|
|
|
import { getBalenaSdk } from '../utils/lazy';
|
2018-04-04 12:42:57 +00:00
|
|
|
|
|
|
|
export const generate: CommandDefinition<{
|
|
|
|
name: string;
|
|
|
|
}> = {
|
|
|
|
signature: 'api-key generate <name>',
|
|
|
|
description: 'Generate a new API key with the given name',
|
2018-04-11 17:31:03 +00:00
|
|
|
help: stripIndent`
|
|
|
|
This command generates a new API key for the current user, with the given
|
|
|
|
name. The key will be logged to the console.
|
2018-04-04 12:42:57 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
This key can be used to log into the CLI using 'balena login --token <key>',
|
2018-04-11 17:31:03 +00:00
|
|
|
or to authenticate requests to the API with an 'Authorization: Bearer <key>' header.
|
2018-04-04 12:42:57 +00:00
|
|
|
|
2018-04-11 17:31:03 +00:00
|
|
|
Examples:
|
2018-04-04 12:42:57 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
$ balena api-key generate "Jenkins Key"
|
2018-04-11 17:31:03 +00:00
|
|
|
`,
|
2020-02-28 13:12:28 +00:00
|
|
|
async action(params) {
|
|
|
|
await getBalenaSdk()
|
2020-02-27 14:55:30 +00:00
|
|
|
.models.apiKey.create(params.name)
|
2018-04-11 17:31:03 +00:00
|
|
|
.then(key => {
|
|
|
|
console.log(stripIndent`
|
|
|
|
Registered api key '${params.name}':
|
|
|
|
|
|
|
|
${key}
|
|
|
|
|
|
|
|
This key will not be shown again, so please save it now.
|
|
|
|
`);
|
2020-02-28 13:12:28 +00:00
|
|
|
});
|
2018-04-04 12:42:57 +00:00
|
|
|
},
|
|
|
|
};
|