From fb3b3a67234bb112ca99bf28765c831bc8d6a0a6 Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Fri, 19 Oct 2018 03:45:17 +0200 Subject: [PATCH 1/3] POC for interactive service account editor --- bin/project/token/edit/examples.md | 0 bin/project/token/edit/index.js | 72 ++++++++++++++++++++++++++++++ bin/project/token/index.js | 1 + docs/project.md | 20 +++++++++ 4 files changed, 93 insertions(+) create mode 100644 bin/project/token/edit/examples.md create mode 100644 bin/project/token/edit/index.js diff --git a/bin/project/token/edit/examples.md b/bin/project/token/edit/examples.md new file mode 100644 index 000000000..e69de29bb diff --git a/bin/project/token/edit/index.js b/bin/project/token/edit/index.js new file mode 100644 index 000000000..ce8b0339c --- /dev/null +++ b/bin/project/token/edit/index.js @@ -0,0 +1,72 @@ +'use strict'; + +const Cli = require('lib/cli'); +const inquirer = require('inquirer'); +const yaml = require('js-yaml'); + + +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 questions = { + type: 'editor', + name: 'input', + message: 'Specify the name and access policy of the token.', + default: yaml.safeDump({ + name: result.name, + access: result.access.map(access => ({ + method: access.method, + path: access.path, + })), + }), + validate: function (text) { + try { + 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 true; + }, + }; + + const {input} = await inquirer.prompt(questions); + const data = yaml.safeLoad(input); + const requests = []; + if ('name' in data && data.name !== result.name) { + requests.push(args.helpers.api.patch(`${args.$node.parent.config.url(args)}/${args[resource.name]}`, { + name: data.name, + })); + } + if ('access' in data) { + // Add new access + requests.push(...data.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 => !data.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..5e270f7e4 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,25 @@ 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]``` +### 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 From b64532d64d7acc640908032c6dc272ff56e07fb7 Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Fri, 19 Oct 2018 12:11:20 +0200 Subject: [PATCH 2/3] Extract prompt_yaml_editor --- bin/project/token/edit/index.js | 51 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/bin/project/token/edit/index.js b/bin/project/token/edit/index.js index ce8b0339c..71c29ea30 100644 --- a/bin/project/token/edit/index.js +++ b/bin/project/token/edit/index.js @@ -5,6 +5,25 @@ 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]: { @@ -22,44 +41,30 @@ module.exports = resource => { handler: async args => { const url = `${resource.url(args)}/${args[resource.name]}`; const result = await args.helpers.api.get(url); - - const questions = { - type: 'editor', - name: 'input', - message: 'Specify the name and access policy of the token.', - default: yaml.safeDump({ + const input = await prompt_yaml_editor({ + defaultValue: { name: result.name, access: result.access.map(access => ({ method: access.method, path: access.path, })), - }), - validate: function (text) { - try { - 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 true; }, - }; - - const {input} = await inquirer.prompt(questions); - const data = yaml.safeLoad(input); + message: 'Specify the name and access policy of the token.', + }); const requests = []; - if ('name' in data && data.name !== result.name) { + if ('name' in input && input.name !== result.name) { requests.push(args.helpers.api.patch(`${args.$node.parent.config.url(args)}/${args[resource.name]}`, { - name: data.name, + name: input.name, })); } - if ('access' in data) { + if ('access' in input) { // Add new access - requests.push(...data.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 => !data.access.find(acc => acc.path === access.path && acc.method === access.method)) + .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) { From 5d6e78c6d2ad0ba6c4db87cbb063738f711b332e Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Wed, 14 Nov 2018 15:52:34 +0100 Subject: [PATCH 3/3] Update docs --- bin/project/token/edit/examples.md | 3 +++ docs/project.md | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/bin/project/token/edit/examples.md b/bin/project/token/edit/examples.md index e69de29bb..e3355fb7b 100644 --- a/bin/project/token/edit/examples.md +++ 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/docs/project.md b/docs/project.md index 5e270f7e4..eb79232cc 100644 --- a/docs/project.md +++ b/docs/project.md @@ -558,6 +558,12 @@ Edit token ### Syntax ```h1 project token edit | --token TOKEN [--project PROJECT]``` +### Example + +```bash +h1 project token edit --token 5bec35a2680cffd11f0f1be5 +``` + ### Required arguments | Name | Default | Description |