diff --git a/package-lock.json b/package-lock.json index bbf1c7f7..4825623f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -921,9 +921,9 @@ } }, "box-node-sdk": { - "version": "1.28.0", - "resolved": "https://registry.npmjs.org/box-node-sdk/-/box-node-sdk-1.28.0.tgz", - "integrity": "sha512-YsuSqKGjdnZQM6ZYmgKLja41ofNi6m2+Bwg/5Sl6JWcEBECdWHLcUh/CJZ6damV6aD/gNepKcZNjNtM8S00Q6Q==", + "version": "1.29.0", + "resolved": "https://registry.npmjs.org/box-node-sdk/-/box-node-sdk-1.29.0.tgz", + "integrity": "sha512-82ya27aRT8yQlL2WJtA4crHXIdxhelB2i+dcoSwF8Cu3bKRuAdlm75joV4WZ9nNcB6ZCh1eSBSNj+adpwKgGZA==", "requires": { "bluebird": "^3.5.0", "http-status": "^1.1.0", diff --git a/package.json b/package.json index 67b8e7b2..ab6c80bc 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@oclif/plugin-help": "^2.1.4", "@oclif/plugin-not-found": "^1.2.0", "archiver": "^3.0.0", - "box-node-sdk": "^1.28.0", + "box-node-sdk": "^1.29.0", "chalk": "^2.4.1", "cli-progress": "^2.1.0", "csv": "^3.1.0", diff --git a/src/commands/files/metadata/set.js b/src/commands/files/metadata/set.js new file mode 100644 index 00000000..815a33e0 --- /dev/null +++ b/src/commands/files/metadata/set.js @@ -0,0 +1,49 @@ +'use strict'; + +const BoxCommand = require('../../../box-command'); +const { flags } = require('@oclif/command'); +const utils = require('../../../util'); + +class FilesSetMetadataCommand extends BoxCommand { + async run() { + const { flags, args } = this.parse(FilesSetMetadataCommand); + + let metadataValues = Object.assign({}, ...flags.data); + let templateKey = flags['template-key']; + + let metadata = await this.client.files.setMetadata(args.id, flags.scope, templateKey, metadataValues); + await this.output(metadata); + } + +} + +FilesSetMetadataCommand.description = 'Set metadata on a file'; + +FilesSetMetadataCommand.flags = { + ...BoxCommand.flags, + data: flags.string({ + description: 'Metadata key and value, in the form "key=value". Note: For float type, use "f" on end of digits: key2=1234.50f', + required: true, + multiple: true, + parse: utils.parseMetadata, + }), + scope: flags.string({ + description: 'The scope of the metadata template to use', + default: 'enterprise', + }), + 'template-key': flags.string({ + description: 'The key of the metadata template to use', + required: true, + }), +}; + +FilesSetMetadataCommand.args = [ + { + name: 'id', + required: true, + hidden: false, + description: 'ID of the file to add metadata to', + } +]; + +module.exports = FilesSetMetadataCommand; diff --git a/src/commands/folders/metadata/set.js b/src/commands/folders/metadata/set.js new file mode 100644 index 00000000..2efde580 --- /dev/null +++ b/src/commands/folders/metadata/set.js @@ -0,0 +1,49 @@ +'use strict'; + +const BoxCommand = require('../../../box-command'); +const { flags } = require('@oclif/command'); +const utils = require('../../../util'); + +class FoldersSetMetadataCommand extends BoxCommand { + async run() { + const { flags, args } = this.parse(FoldersSetMetadataCommand); + + let metadataValues = Object.assign({}, ...flags.data); + let templateKey = flags['template-key']; + + let metadata = await this.client.folders.setMetadata(args.id, flags.scope, templateKey, metadataValues); + await this.output(metadata); + } + +} + +FoldersSetMetadataCommand.description = 'Set metadata on a folder'; + +FoldersSetMetadataCommand.flags = { + ...BoxCommand.flags, + data: flags.string({ + description: 'Metadata key and value, in the form "key=value". Note: For float type, use "f" on end of digits: key2=1234.50f', + required: true, + multiple: true, + parse: utils.parseMetadata, + }), + scope: flags.string({ + description: 'The scope of the metadata template to use', + default: 'enterprise', + }), + 'template-key': flags.string({ + description: 'The key of the metadata template to use', + required: true, + }), +}; + +FoldersSetMetadataCommand.args = [ + { + name: 'id', + required: true, + hidden: false, + description: 'ID of the folder to add metadata to', + } +]; + +module.exports = FoldersSetMetadataCommand; diff --git a/test/commands/files.test.js b/test/commands/files.test.js index 2ed7e872..60350b5a 100644 --- a/test/commands/files.test.js +++ b/test/commands/files.test.js @@ -983,6 +983,100 @@ describe('Files', () => { }); }); + describe('files:metadata:set', () => { + let fileID = '11111', + metadataScope = 'enterprise', + metadataTemplate = 'testTemplate', + addMetadataFixture = getFixture('files/post_files_id_metadata_scope_template'), + yamlOutput = getFixture('output/files_metadata_create_yaml.txt'); + + let createMetadataBody = { + test: 'test123', + number: 1.9, + arr: [ + 'foo', + 'bar' + ] + }; + + test + .nock(TEST_API_ROOT, api => api + .post(`/2.0/files/${fileID}/metadata/${metadataScope}/${metadataTemplate}`, createMetadataBody) + .reply(201, addMetadataFixture) + ) + .stdout() + .command([ + 'files:metadata:set', + fileID, + `--template-key=${metadataTemplate}`, + '--data=test=test123', + '--data=number=#1.9', + '--data=arr=[foo,bar]', + '--json', + '--token=test' + ]) + .it('should add metadata object with key/value pairs passed as a flag (JSON Output)', ctx => { + assert.equal(ctx.stdout, addMetadataFixture); + }); + + test + .nock(TEST_API_ROOT, api => api + .post(`/2.0/files/${fileID}/metadata/${metadataScope}/${metadataTemplate}`, createMetadataBody) + .reply(201, addMetadataFixture) + ) + .stdout() + .command([ + 'files:metadata:set', + fileID, + `--template-key=${metadataTemplate}`, + '--data=test=test123', + '--data=number=#1.9', + '--data=arr=[foo,bar]', + '--token=test' + ]) + .it('should add metadata object with key/value pairs passed as a flag (YAML Output)', ctx => { + assert.equal(ctx.stdout, yamlOutput); + }); + + test + .nock(TEST_API_ROOT, api => api + .post(`/2.0/files/${fileID}/metadata/${metadataScope}/${metadataTemplate}`, createMetadataBody) + .reply(409) + .put(`/2.0/files/${fileID}/metadata/${metadataScope}/${metadataTemplate}`, [ + { + op: 'add', + path: '/test', + value: 'test123', + }, + { + op: 'add', + path: '/number', + value: 1.9, + }, + { + op: 'add', + path: '/arr', + value: [ 'foo', 'bar' ], + } + ]) + .reply(200, addMetadataFixture) + ) + .stdout() + .command([ + 'files:metadata:set', + fileID, + `--template-key=${metadataTemplate}`, + '--data=test=test123', + '--data=number=#1.9', + '--data=arr=[foo,bar]', + '--json', + '--token=test' + ]) + .it('should update metadata object with key/value pairs passed as a flag when creation conflicts', ctx => { + assert.equal(ctx.stdout, addMetadataFixture); + }); + }); + leche.withData([ 'files:share', 'files:shared-links:create', diff --git a/test/commands/folders.test.js b/test/commands/folders.test.js index 12554d33..59d41d69 100644 --- a/test/commands/folders.test.js +++ b/test/commands/folders.test.js @@ -786,6 +786,102 @@ describe('Folders', () => { }); }); + describe('folders:metadata:set', () => { + let folderId = '0', + metadataScope = 'enterprise', + metadataTemplate = 'testTemplate', + addMetadataFixture = getFixture('folders/post_folders_id_metadata_scope_template'), + yamlOutput = getFixture('output/folders_metadata_create_yaml.txt'); + + let createMetadataBody = { + test: 'test123', + number: 1.9, + arr: [ + 'foo', + 'bar' + ] + }; + + test + .nock(TEST_API_ROOT, api => api + .post(`/2.0/folders/${folderId}/metadata/${metadataScope}/${metadataTemplate}`, createMetadataBody) + .reply(201, addMetadataFixture) + ) + .stdout() + .command([ + 'folders:metadata:set', + folderId, + `--template-key=${metadataTemplate}`, + '--data=test=test123', + '--data=number=#1.9', + '--data=arr=[foo,bar]', + '--json', + '--token=test' + ]) + .it('should add metadata object with key/value pairs passed as a flag (JSON Output)', ctx => { + assert.equal(ctx.stdout, addMetadataFixture); + }); + + test + .nock(TEST_API_ROOT, api => api + .post(`/2.0/folders/${folderId}/metadata/${metadataScope}/${metadataTemplate}`, createMetadataBody) + .reply(201, addMetadataFixture) + ) + .stdout() + .command([ + 'folders:metadata:set', + folderId, + `--template-key=${metadataTemplate}`, + '--data=test=test123', + '--data=number=#1.9', + '--data=arr=[foo,bar]', + '--token=test' + ]) + .it('should add metadata object with key/value pairs passed as a flag (YAML Output)', ctx => { + assert.equal(ctx.stdout, yamlOutput); + }); + + test + .nock(TEST_API_ROOT, api => api + .post(`/2.0/folders/${folderId}/metadata/${metadataScope}/${metadataTemplate}`, createMetadataBody) + .reply(409) + .put(`/2.0/folders/${folderId}/metadata/${metadataScope}/${metadataTemplate}`, [ + { + op: 'add', + path: '/test', + value: 'test123', + }, + { + op: 'add', + path: '/number', + value: 1.9, + }, + { + op: 'add', + path: '/arr', + value: [ 'foo', 'bar' ], + } + ]) + .reply(200, addMetadataFixture) + ) + .stdout() + .command([ + 'folders:metadata:set', + folderId, + `--template-key=${metadataTemplate}`, + '--data=test=test123', + '--data=number=#1.9', + '--data=arr=[foo,bar]', + '--json', + '--token=test' + ]) + .it('should update metadata object with key/value pairs passed as a flag when creation conflicts', ctx => { + assert.equal(ctx.stdout, addMetadataFixture); + }); + + + }); + leche.withData([ 'folders:share', 'folders:shared-links:create',