Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/project/token/edit/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```bash
{{command_name}} --token 5bec35a2680cffd11f0f1be5 --project MyProject
```
77 changes: 77 additions & 0 deletions bin/project/token/edit/index.js
Original file line number Diff line number Diff line change
@@ -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));
},
});
};
1 change: 1 addition & 0 deletions bin/project/token/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = parent => {

category.addChild(require('./add')(resource));
category.addChild(require('./access')(resource));
category.addChild(require('./edit')(resource));

return category;
};
Expand Down
26 changes: 26 additions & 0 deletions docs/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down