X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=cli%2Factions.js;h=7a361b7fda3b050005bf65157556d4320bd32778;hb=c9d33e20bf7d74b0d8a7eb1e1c89e8a845098460;hp=6fb98937e73a57f8e26fd5400dfdc092fce04799;hpb=802c67734b461e68d271434291c22a2305fc01a7;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git diff --git a/cli/actions.js b/cli/actions.js index 6fb9893..7a361b7 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -8,6 +8,7 @@ exports.del = del; var superagent = require('superagent'), config = require('./config.js'), readlineSync = require('readline-sync'), + safe = require('safetydance'), async = require('async'), fs = require('fs'), request = require('request'), @@ -28,20 +29,23 @@ function checkConfig() { gQuery = { username: config.username(), password: config.password() }; - console.error('Using server %s', config.server().yellow); + console.error('Using server %s', config.server().cyan); } -function collectFiles(filesOrFolders) { +function collectFiles(filesOrFolders, options) { var tmp = []; filesOrFolders.forEach(function (filePath) { + var baseName = path.basename(filePath); + if (!options.all && baseName[0] === '.' && baseName.length > 1) return; + var stat = fs.statSync(filePath); if (stat.isFile()) { tmp.push(filePath); } else if (stat.isDirectory()) { var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); }); - tmp = tmp.concat(collectFiles(files)); + tmp = tmp.concat(collectFiles(files, options)); } else { console.log('Skipping %s', filePath.cyan); } @@ -52,18 +56,18 @@ function collectFiles(filesOrFolders) { function login(uri) { var tmp = url.parse(uri); - if (!tmp.host) tmp = url.parse('https://' + uri); + if (!tmp.slashes) tmp = url.parse('https://' + uri); var server = tmp.protocol + '//' + tmp.host; - console.log('Using server', server.bold); + console.log('Using server', server.cyan); - var username = readlineSync.question('Username: ', { hideEchoBack: false }); - var password = readlineSync.question('Password: ', { hideEchoBack: true }); + var username = readlineSync.question('Username: '); + var password = readlineSync.question('Password: ', { noEchoBack: true }); superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) { if (error && error.code === 'ENOTFOUND') { - console.log('No such server %s'.red, server.bold); + console.log('Server %s not found.'.red, server.bold); process.exit(1); } if (error && error.code) { @@ -83,18 +87,35 @@ function login(uri) { gQuery = { username: username, password: password }; - console.log('Ok'.green); + console.log('Login successful'.green); }); } function put(filePath, otherFilePaths, options) { checkConfig(); - var files = collectFiles([ filePath ].concat(otherFilePaths)); + var destination = ''; + + // take the last argument as destination + if (otherFilePaths.length > 0) { + destination = otherFilePaths.pop(); + if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; + } + + var files = collectFiles([ filePath ].concat(otherFilePaths), options); async.eachSeries(files, function (file, callback) { - var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1); - var destinationPath = (options.destination ? '/' + options.destination : '') + '/' + relativeFilePath; + var relativeFilePath; + + if (path.isAbsolute(file)) { + relativeFilePath = path.basename(file); + } else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir + relativeFilePath = path.resolve(file).slice(process.cwd().length + 1); + } else { // relative but somewhere else + relativeFilePath = path.basename(file); + } + + var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); superagent.put(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { @@ -102,6 +123,8 @@ function put(filePath, otherFilePaths, options) { if (result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode)); console.log('Uploaded to ' + config.server() + destinationPath); + + callback(null); }); }, function (error) { if (error) { @@ -122,16 +145,21 @@ function get(filePath) { request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) { if (error) return console.error(error); if (result.statusCode === 401) return console.log('Login failed'); - if (result.statusCode === 404) return console.log('No such file or directory'); + if (result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); // 222 indicates directory listing if (result.statusCode === 222) { - console.log('Files:'); - JSON.parse(body).entries.forEach(function (entry) { - console.log('\t %s', entry); - }); + var files = safe.JSON.parse(body); + if (!files || files.entries.length === 0) { + console.log('No files on the server. Use %s to upload some.', 'surfer put '.yellow); + } else { + console.log('Files:'); + files.entries.forEach(function (entry) { + console.log('\t %s', entry); + }); + } } else { - console.log(body); + process.stdout.write(body); } }); // var req = superagent.get(config.server() + API + filePath);