Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata set commands #136

Merged
merged 4 commits into from
Apr 30, 2019
Merged
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions src/commands/files/metadata/set.js
Original file line number Diff line number Diff line change
@@ -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;
49 changes: 49 additions & 0 deletions src/commands/folders/metadata/set.js
Original file line number Diff line number Diff line change
@@ -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;
94 changes: 94 additions & 0 deletions test/commands/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
96 changes: 96 additions & 0 deletions test/commands/folders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down