diff --git a/bin/project/token/edit/examples.md b/bin/project/token/edit/examples.md new file mode 100644 index 000000000..e3355fb7b --- /dev/null +++ b/bin/project/token/edit/examples.md @@ -0,0 +1,3 @@ +```bash +{{command_name}} --token 5bec35a2680cffd11f0f1be5 --project MyProject +``` \ No newline at end of file diff --git a/bin/project/token/edit/index.js b/bin/project/token/edit/index.js new file mode 100644 index 000000000..71c29ea30 --- /dev/null +++ b/bin/project/token/edit/index.js @@ -0,0 +1,77 @@ +'use strict'; + +const Cli = require('lib/cli'); +const inquirer = require('inquirer'); +const yaml = require('js-yaml'); + + +async function prompt_yaml_editor(options) { + const data = await inquirer.prompt({ + type: 'editor', + name: 'input', + message: options.message, + default: yaml.safeDump(options.defaultValue), + validate: function (text) { + let content; + try { + content = yaml.safeLoad(text); + } catch (e) { + return `You can not change the data format (YAML). The data entered is not a valid YAML document.\n${e.message}`; + } + return options.validator ? options.validator(content) : true; + }, + }); + return yaml.safeLoad(data.input); +} + +module.exports = resource => { + const options = { + [resource.name]: { + description: `${resource.title} ID`, + type: 'string', + required: true, + }, + }; + + return Cli.createCommand('edit', { + description: `Edit ${resource.title}`, + resource: resource, + dirname: __dirname, + options: Object.assign({}, options, resource.options), + handler: async args => { + const url = `${resource.url(args)}/${args[resource.name]}`; + const result = await args.helpers.api.get(url); + const input = await prompt_yaml_editor({ + defaultValue: { + name: result.name, + access: result.access.map(access => ({ + method: access.method, + path: access.path, + })), + }, + message: 'Specify the name and access policy of the token.', + }); + const requests = []; + if ('name' in input && input.name !== result.name) { + requests.push(args.helpers.api.patch(`${args.$node.parent.config.url(args)}/${args[resource.name]}`, { + name: input.name, + })); + } + if ('access' in input) { + // Add new access + requests.push(...input.access + .filter(access => !result.access.find(acc => acc.path === access.path && acc.method === access.method)) + .map(access => args.helpers.api.post(`${url}/access`, access))); + // Remove missing access + requests.push(...result.access + .filter(access => !input.access.find(acc => acc.path === access.path && acc.method === access.method)) + .map(access => args.helpers.api.delete(`${url}/access/${access._id}`))); + } + for (const req in requests) { + await req; // Perform asynchronously for now. + } + return args.helpers.api.get(url) + .then(result => args.helpers.sendOutput(args, result)); + }, + }); +}; diff --git a/bin/project/token/index.js b/bin/project/token/index.js index dda35c92f..dba960670 100644 --- a/bin/project/token/index.js +++ b/bin/project/token/index.js @@ -25,6 +25,7 @@ module.exports = parent => { category.addChild(require('./add')(resource)); category.addChild(require('./access')(resource)); + category.addChild(require('./edit')(resource)); return category; }; diff --git a/docs/project.md b/docs/project.md index d05dc734b..eb79232cc 100644 --- a/docs/project.md +++ b/docs/project.md @@ -24,6 +24,7 @@ * [h1 project token access show](#h1-project-token-access-show) - Show access rule * [h1 project token access delete](#h1-project-token-access-delete) - Delete access rule * [h1 project token access add](#h1-project-token-access-add) - Add access rule + * [h1 project token edit](#h1-project-token-edit) - Edit token * [h1 project notification](#h1-project-notification) - Manage your notifications * [h1 project notification credits](#h1-project-notification-credits) - Manage your threshold of credit limits * [h1 project notification credits add](#h1-project-notification-credits-add) - Add credits limits @@ -550,6 +551,31 @@ h1 project token access add --project 6oAoJqgyLZP4Le9UUNHrEOYP --method POST --p | ---- | ------- | ----------- | | ```--project PROJECT``` | | Project ID or name. Active project by default | +## h1 project token edit + +Edit token + +### Syntax + +```h1 project token edit | --token TOKEN [--project PROJECT]``` +### Example + +```bash +h1 project token edit --token 5bec35a2680cffd11f0f1be5 +``` + +### Required arguments + +| Name | Default | Description | +| ---- | ------- | ----------- | +| ```--token TOKEN``` | | token ID | + +### Optional arguments + +| Name | Default | Description | +| ---- | ------- | ----------- | +| ```--project PROJECT``` | | Project ID or name. Active project by default | + ## h1 project notification Manage your notifications