baidubce-cli
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "baidubce-cli", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "baidu cloud engine command line tools", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,39 +27,39 @@ /** | ||
var argv = require('yargs') | ||
.usage('Usage: $0 bos') | ||
.boolean('h', {alias: 'help', describe: 'Show this help message.'}) | ||
.options('c', {alias: 'config', describe: 'Manage the baidubce configuration', boolean: true}) | ||
.usage('Usage: $0 bos') | ||
.boolean('h', {alias: 'help', describe: 'Show this help message.'}) | ||
.options('c', {alias: 'config', describe: 'Manage the baidubce configuration', 'boolean': true}) | ||
.options('list-buckets', {describe: 'List all buckets.', boolean: true}) | ||
.example('baidubce bos --list-buckets', 'List all buckets.') | ||
.options('list-buckets', {describe: 'List all buckets.', 'boolean': true}) | ||
.example('baidubce bos --list-buckets', 'List all buckets.') | ||
.options('create-buckets', {describe: 'Create buckets.', boolean: true}) | ||
.example('baidubce bos --create-buckets my-bucket-1 my-bucket-2', 'Create one or more buckets.') | ||
.options('create-buckets', {describe: 'Create buckets.', 'boolean': true}) | ||
.example('baidubce bos --create-buckets my-bucket-1 my-bucket-2', 'Create one or more buckets.') | ||
.options('delete-buckets', {describe: 'Delete buckets.', boolean: true}) | ||
.example('baidubce bos --delete-buckets my-bucket-1 my-bucket-2', 'Delete buckets') | ||
.options('delete-buckets', {describe: 'Delete buckets.', 'boolean': true}) | ||
.example('baidubce bos --delete-buckets my-bucket-1 my-bucket-2', 'Delete buckets') | ||
.options('set-bucket-acl', {describe: 'Set bucket acl.', boolean: true}) | ||
.example('baidubce bos --set-bucket-acl bucket-1 bucket-2 public-read-write', 'Set buckets public-read-write acl') | ||
.example('baidubce bos --set-bucket-acl bucket-1 bucket-2 private', 'Set buckets private acl\n') | ||
.options('set-bucket-acl', {describe: 'Set bucket acl.', 'boolean': true}) | ||
.example('baidubce bos --set-bucket-acl bucket-1 bucket-2 public-read-write', 'Set buckets public-read-write acl') | ||
.example('baidubce bos --set-bucket-acl bucket-1 bucket-2 private', 'Set buckets private acl\n') | ||
.options('list-objects', {describe: 'List all objects.', requiresArg: true}) | ||
.example('baidubce bos --list-objects my-bucket-1', 'List all objects in the specific bucket') | ||
.options('list-objects', {describe: 'List all objects.', 'boolean': true}) | ||
.example('baidubce bos --list-objects my-bucket-1', 'List all objects in the specific bucket') | ||
.options('put-object', {describe: 'Upload a regular file', requiresArg: true}) | ||
.example('baidubce bos --put-object hello.js bos://my-bucket/world.js', 'Upload a regular file') | ||
.options('put-object', {describe: 'Upload a regular file', requiresArg: true}) | ||
.example('baidubce bos --put-object hello.js bos://my-bucket/world.js', 'Upload a regular file') | ||
.options('copy-object', {describe: 'Copy object from one bucket to another', boolean: true}) | ||
.example('baidubce bos --copy-object bos://source/sk.js bos://target/tk.js', | ||
.options('copy-object', {describe: 'Copy object from one bucket to another', 'boolean': true}) | ||
.example('baidubce bos --copy-object bos://source/sk.js bos://target/tk.js', | ||
'Copy object from one bucket to another') | ||
.options('put-superfile', {describe: 'Upload a super large file', requiresArg: true}) | ||
.example('baidubce bos --put-superfile hello.js bos://my-bucket/world.js', 'Upload a super large file') | ||
.options('put-superfile', {describe: 'Upload a super large file', requiresArg: true}) | ||
.example('baidubce bos --put-superfile hello.js bos://my-bucket/world.js', 'Upload a super large file') | ||
.options('delete-objects', {describe: 'Delete objects.', boolean: true}) | ||
.example('baidubce bos --delete-objects my-bucket-1 hello.js world.js', 'Delete objects') | ||
.options('delete-objects', {describe: 'Delete objects.', 'boolean': true}) | ||
.example('baidubce bos --delete-objects my-bucket-1 hello.js world.js', 'Delete objects') | ||
.check(function(args) { | ||
.check(function (args) { | ||
var x = ['p', 's']; | ||
var c = 0; | ||
u.each(x, function(key) { | ||
u.each(x, function (key) { | ||
if (u.has(args, key)) { | ||
@@ -69,3 +69,3 @@ if (!fs.existsSync(args[key])) { | ||
} | ||
c ++; | ||
c += 1; | ||
} | ||
@@ -89,3 +89,3 @@ }); | ||
exports.run = function() { | ||
exports.run = function () { | ||
var args = argv.argv; | ||
@@ -110,24 +110,19 @@ if (args.h === true || args.help === true) { | ||
} | ||
else if (args['create-buckets']) { | ||
return createBuckets(getClient(args), args._); | ||
var actionMap = { | ||
'create-buckets': createBuckets, | ||
'list-buckets': listBuckets, | ||
'set-bucket-acl': setBucketsAcl, | ||
'list-objects': listObjects, | ||
'delete-objects': deleteObjects, | ||
'delete-buckets': deleteBuckets, | ||
'copy-object': copyObject | ||
}; | ||
for (var ak in actionMap) { | ||
if (args[ak] === true) { | ||
return actionMap[ak](args._); | ||
} | ||
} | ||
else if (args['list-buckets']) { | ||
return listBuckets(getClient(args)); | ||
} | ||
else if (args['set-bucket-acl']) { | ||
return setBucketsAcl(getClient(args), args._); | ||
} | ||
else if (args['list-objects']) { | ||
return listObjects(getClient(args), args['list-objects']); | ||
} | ||
else if (args['delete-objects']) { | ||
return deleteObjects(getClient(args), args._); | ||
} | ||
else if (args['delete-buckets']) { | ||
return deleteBuckets(getClient(args), args._); | ||
} | ||
else if (args['copy-object']) { | ||
return copyObject(getClient(args), args._); | ||
} | ||
else if (args['put-object'] || args['put-superfile']) { | ||
if (args['put-object'] || args['put-superfile']) { | ||
var destination = args._[0]; | ||
@@ -154,8 +149,8 @@ var match = kPattern.exec(destination); | ||
function deleteBuckets(client, buckets) { | ||
return Q.all(u.map(buckets, function(bucket){ | ||
return client.deleteBucket(bucket); | ||
})).then(function(response) { | ||
function deleteBuckets(buckets) { | ||
return Q.all(u.map(buckets, function (bucket) { | ||
return getClient(bucket).deleteBucket(bucket); | ||
})).then(function (response) { | ||
console.log('DONE'); | ||
}).catch(function(error) { | ||
}).catch(function (error) { | ||
console.error(error); | ||
@@ -165,3 +160,3 @@ }); | ||
function copyObject(client, args) { | ||
function copyObject(args) { | ||
if (args.length !== 2) { | ||
@@ -184,6 +179,15 @@ console.error('Arguments insufficient'); | ||
return client.copyObject(source_bucket_name, source_key, | ||
target_bucket_name, target_key).then(function(response) { | ||
var c1 = getClient(source_bucket_name); | ||
var c2 = getClient(target_bucket_name); | ||
if (c1.config.endpoint !== c2.config.endpoint) { | ||
console.error(source_bucket_name + ' and ' + target_bucket_name + | ||
' belongs to different endpoint.'); | ||
return; | ||
} | ||
return c1.copyObject(source_bucket_name, source_key, | ||
target_bucket_name, target_key).then(function (response) { | ||
console.log('DONE'); | ||
}).catch(function(error) { | ||
}).catch(function (error) { | ||
console.error(error); | ||
@@ -193,3 +197,3 @@ }); | ||
function deleteObjects(client, args) { | ||
function deleteObjects(args) { | ||
if (args.length < 2) { | ||
@@ -202,8 +206,9 @@ console.error('Arguments insufficient'); | ||
var objects = args; | ||
var client = getClient(bucket); | ||
return Q.all(u.map(objects, function(object) { | ||
return Q.all(u.map(objects, function (object) { | ||
return client.deleteObject(bucket, object); | ||
})).then(function(response) { | ||
})).then(function (response) { | ||
console.log('DONE'); | ||
}).catch(function(error) { | ||
}).catch(function (error) { | ||
console.error(error); | ||
@@ -213,12 +218,18 @@ }); | ||
function listObjects(client, bucket_name) { | ||
return client.listObjects(bucket_name).then(function(response) { | ||
function listObjects(args) { | ||
if (!args || !args.length) { | ||
console.error('Arguments insufficient'); | ||
return; | ||
} | ||
var bucket_name = args[0]; | ||
return getClient(bucket_name).listObjects(bucket_name).then(function (response) { | ||
var table = require('text-table'); | ||
var rows = [['No.', 'Name', 'Size', 'Last Modified']]; | ||
u.each(response.body.contents, function(object, idx) { | ||
u.each(response.body.contents, function (object, idx) { | ||
rows.push([idx + 1, object.key, object.size, | ||
moment(object.lastModified).format('YYYY-MM-DD hh:mm:ss')]); | ||
}); | ||
console.log(table(rows)); | ||
}).catch(function(error) { | ||
console.log(table(rows, {stringLength: stringLength})); | ||
}).catch(function (error) { | ||
console.error(error); | ||
@@ -228,3 +239,3 @@ }); | ||
function setBucketsAcl(client, args) { | ||
function setBucketsAcl(args) { | ||
if (args.length < 2) { | ||
@@ -238,7 +249,7 @@ console.error('Arguments insufficient'); | ||
return Q.all(u.map(buckets, function(bucket) { | ||
return client.setBucketCannedAcl(bucket, acl); | ||
})).then(function(response) { | ||
return Q.all(u.map(buckets, function (bucket) { | ||
return getClient(bucket).setBucketCannedAcl(bucket, acl); | ||
})).then(function (response) { | ||
console.log('DONE'); | ||
}).catch(function(error) { | ||
}).catch(function (error) { | ||
console.log(error); | ||
@@ -248,13 +259,13 @@ }); | ||
function listBuckets(client) { | ||
client.listBuckets() | ||
.then(function(response) { | ||
function listBuckets() { | ||
getClient().listBuckets() | ||
.then(function (response) { | ||
var table = require('text-table'); | ||
var rows = [['No.', 'Name', 'Create Date']]; | ||
u.each(response.body.buckets, function(bucket, idx) { | ||
u.each(response.body.buckets, function (bucket, idx) { | ||
rows.push([idx + 1, bucket.name, moment(bucket.creationDate).format('YYYY-MM-DD hh:mm:ss')]); | ||
}); | ||
console.log(table(rows)); | ||
console.log(table(rows, {stringLength: stringLength})); | ||
}) | ||
.catch(function(error) { | ||
.catch(function (error) { | ||
console.error(error); | ||
@@ -264,3 +275,3 @@ }); | ||
function createBuckets(client, buckets) { | ||
function createBuckets(buckets) { | ||
if (!buckets || !buckets.length) { | ||
@@ -271,7 +282,7 @@ console.log('Please specify the buckets you want to create.'); | ||
return Q.all(u.map(buckets, function(bucket) { | ||
return client.createBucket(bucket); | ||
})).then(function(response) { | ||
return Q.all(u.map(buckets, function (bucket) { | ||
return getClient(bucket).createBucket(bucket); | ||
})).then(function (response) { | ||
console.log('DONE'); | ||
}).catch(function(error) { | ||
}).catch(function (error) { | ||
console.error(error); | ||
@@ -283,3 +294,2 @@ }); | ||
/** | ||
* @param {Object} args The command line arguments. | ||
* @param {string=} opt_bucket_name The bucket name. | ||
@@ -289,6 +299,8 @@ * | ||
*/ | ||
function getClient(args, opt_bucket_name) { | ||
function getClient(opt_bucket_name) { | ||
var bucket_name = opt_bucket_name || ''; | ||
var ak, sk, endpoint; | ||
var ak; | ||
var sk; | ||
var endpoint; | ||
if (bucket_name) { | ||
@@ -314,6 +326,2 @@ ak = cmdConfig.get('bos.' + bucket_name + '.ak'); | ||
/** | ||
* Get the object name from command line args. | ||
* @return {string} | ||
*/ | ||
function getKey(args) { | ||
@@ -346,2 +354,8 @@ // 1. check the destination first | ||
function stringLength(s) { | ||
var a = String(s).length; | ||
// Non-ASCII character will produce double width. | ||
var b = String(s).replace(/[\x00-\xff]/g, '').length; | ||
return a + b; | ||
} | ||
@@ -354,2 +368,3 @@ | ||
/* vim: set ts=4 sw=4 sts=4 tw=120: */ |
@@ -20,3 +20,3 @@ /* | ||
exports.dump = function() { | ||
exports.dump = function () { | ||
if (!fs.existsSync(kConfigFile)) { | ||
@@ -26,10 +26,8 @@ console.log('There is no any configuration.'); | ||
} | ||
else { | ||
console.log('@' + kConfigFile); | ||
console.log(); | ||
console.log(fs.readFileSync(kConfigFile, 'utf-8')); | ||
} | ||
console.log('@' + kConfigFile); | ||
console.log(); | ||
console.log(fs.readFileSync(kConfigFile, 'utf-8')); | ||
}; | ||
exports.set = function(args) { | ||
exports.set = function (args) { | ||
var currentConfig = {}; | ||
@@ -57,3 +55,3 @@ if (fs.existsSync(kConfigFile)) { | ||
exports.get = function(key) { | ||
exports.get = function (key) { | ||
if (!fs.existsSync(kConfigFile)) { | ||
@@ -60,0 +58,0 @@ throw new Error('There is no any configuration'); |
@@ -19,8 +19,8 @@ /** | ||
exports.putRegularFile = function(client, bucket_name, key, filename) { | ||
exports.putRegularFile = function (client, bucket_name, key, filename) { | ||
client.putObjectFromFile(bucket_name, key, filename) | ||
.then(function() { | ||
.then(function () { | ||
console.log(client.generatePresignedUrl(bucket_name, key)); | ||
}) | ||
.catch(function(error) { | ||
.catch(function (error) { | ||
console.error(error); | ||
@@ -30,6 +30,6 @@ }); | ||
exports.putSuperLargeFile = function(client, bucket_name, key, filename) { | ||
exports.putSuperLargeFile = function (client, bucket_name, key, filename) { | ||
var upload_id = null; | ||
client.initiateMultipartUpload(bucket_name, key) | ||
.then(function(response) { | ||
.then(function (response) { | ||
upload_id = response.body.uploadId; | ||
@@ -48,2 +48,8 @@ | ||
function displayProgress(response) { | ||
success_count += 1; | ||
require('./util').displayProgress(Math.floor(success_count * 100 / part_count)); | ||
return response; | ||
} | ||
while (left_size > 0) { | ||
@@ -53,7 +59,3 @@ var part_size = Math.min(left_size, min_part_size); | ||
defers.push(client.uploadPartFromFile(bucket_name, key, upload_id, | ||
part_number, part_size, filename, offset).then(function(response) { | ||
success_count += 1; | ||
require('./util').displayProgress(Math.floor(success_count * 100 / part_count)); | ||
return response; | ||
})); | ||
part_number, part_size, filename, offset).then(displayProgress)); | ||
@@ -67,9 +69,9 @@ left_size -= part_size; | ||
}) | ||
.then(function(all_response) { | ||
.then(function (all_response) { | ||
console.log(); | ||
var part_list = []; | ||
u.each(all_response, function(response, index) { | ||
u.each(all_response, function (response, index) { | ||
part_list.push({ | ||
partNumber: index + 1, | ||
eTag: response.http_headers['etag'], | ||
eTag: response.http_headers.etag | ||
}); | ||
@@ -80,6 +82,6 @@ }); | ||
}) | ||
.then(function() { | ||
.then(function () { | ||
console.log(client.generatePresignedUrl(bucket_name, key)); | ||
}) | ||
.catch(function(error) { | ||
.catch(function (error) { | ||
console.error(error); | ||
@@ -86,0 +88,0 @@ }); |
@@ -32,3 +32,3 @@ /** | ||
if (require.main === module) { | ||
(function(){ | ||
(function () { | ||
var i = 5; | ||
@@ -43,3 +43,3 @@ setTimeout(function () { | ||
})(); | ||
}; | ||
} | ||
@@ -46,0 +46,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20506
9
490