Skip to content

Commit fb3b3a6

Browse files
committed
POC for interactive service account editor
1 parent 0437c92 commit fb3b3a6

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

bin/project/token/edit/examples.md

Whitespace-only changes.

bin/project/token/edit/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
const Cli = require('lib/cli');
4+
const inquirer = require('inquirer');
5+
const yaml = require('js-yaml');
6+
7+
8+
module.exports = resource => {
9+
const options = {
10+
[resource.name]: {
11+
description: `${resource.title} ID`,
12+
type: 'string',
13+
required: true,
14+
},
15+
};
16+
17+
return Cli.createCommand('edit', {
18+
description: `Edit ${resource.title}`,
19+
resource: resource,
20+
dirname: __dirname,
21+
options: Object.assign({}, options, resource.options),
22+
handler: async args => {
23+
const url = `${resource.url(args)}/${args[resource.name]}`;
24+
const result = await args.helpers.api.get(url);
25+
26+
const questions = {
27+
type: 'editor',
28+
name: 'input',
29+
message: 'Specify the name and access policy of the token.',
30+
default: yaml.safeDump({
31+
name: result.name,
32+
access: result.access.map(access => ({
33+
method: access.method,
34+
path: access.path,
35+
})),
36+
}),
37+
validate: function (text) {
38+
try {
39+
yaml.safeLoad(text);
40+
} catch (e) {
41+
return `You can not change the data format (YAML). The data entered is not a valid YAML document.\n${e.message}`;
42+
}
43+
return true;
44+
},
45+
};
46+
47+
const {input} = await inquirer.prompt(questions);
48+
const data = yaml.safeLoad(input);
49+
const requests = [];
50+
if ('name' in data && data.name !== result.name) {
51+
requests.push(args.helpers.api.patch(`${args.$node.parent.config.url(args)}/${args[resource.name]}`, {
52+
name: data.name,
53+
}));
54+
}
55+
if ('access' in data) {
56+
// Add new access
57+
requests.push(...data.access
58+
.filter(access => !result.access.find(acc => acc.path === access.path && acc.method === access.method))
59+
.map(access => args.helpers.api.post(`${url}/access`, access)));
60+
// Remove missing access
61+
requests.push(...result.access
62+
.filter(access => !data.access.find(acc => acc.path === access.path && acc.method === access.method))
63+
.map(access => args.helpers.api.delete(`${url}/access/${access._id}`)));
64+
}
65+
for (const req in requests) {
66+
await req; // Perform asynchronously for now.
67+
}
68+
return args.helpers.api.get(url)
69+
.then(result => args.helpers.sendOutput(args, result));
70+
},
71+
});
72+
};

bin/project/token/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = parent => {
2525

2626
category.addChild(require('./add')(resource));
2727
category.addChild(require('./access')(resource));
28+
category.addChild(require('./edit')(resource));
2829

2930
return category;
3031
};

docs/project.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [h1 project token access show](#h1-project-token-access-show) - Show access rule
2525
* [h1 project token access delete](#h1-project-token-access-delete) - Delete access rule
2626
* [h1 project token access add](#h1-project-token-access-add) - Add access rule
27+
* [h1 project token edit](#h1-project-token-edit) - Edit token
2728
* [h1 project notification](#h1-project-notification) - Manage your notifications
2829
* [h1 project notification credits](#h1-project-notification-credits) - Manage your threshold of credit limits
2930
* [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
550551
| ---- | ------- | ----------- |
551552
| ```--project PROJECT``` | | Project ID or name. Active project by default |
552553

554+
## h1 project token edit
555+
556+
Edit token
557+
558+
### Syntax
559+
560+
```h1 project token edit | --token TOKEN [--project PROJECT]```
561+
### Required arguments
562+
563+
| Name | Default | Description |
564+
| ---- | ------- | ----------- |
565+
| ```--token TOKEN``` | | token ID |
566+
567+
### Optional arguments
568+
569+
| Name | Default | Description |
570+
| ---- | ------- | ----------- |
571+
| ```--project PROJECT``` | | Project ID or name. Active project by default |
572+
553573
## h1 project notification
554574

555575
Manage your notifications

0 commit comments

Comments
 (0)