X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=cli%2Factions.js;h=3fd240302a3287cb214176021740cc0520f96f3d;hb=HEAD;hp=45656a6fb14fc593a5317e955e43332e9af4e928;hpb=9b7a26fc3708ac42d7d29c4329adbde465d29220;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git diff --git a/cli/actions.js b/cli/actions.js index 45656a6..3fd2403 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -1,6 +1,7 @@ 'use strict'; exports.login = login; +exports.logout = logout; exports.put = put; exports.get = get; exports.del = del; @@ -19,17 +20,31 @@ require('colors'); var API = '/api/files/'; +var gServer = ''; var gQuery = {}; -function checkConfig() { - if (!config.server() || !config.accessToken()) { - console.log('You have run "login" first'); +function checkConfig(options) { + if (!options.parent.server && !config.server()) { + console.log('Run %s first, or provide %s', 'surfer login'.bold, '--server '.bold); process.exit(1); } - gQuery = { access_token: config.accessToken() }; + if (options.parent.server) { + var tmp = url.parse(options.parent.server); + if (!tmp.slashes) tmp = url.parse('https://' + options.parent.server); + gServer = tmp.protocol + '//' + tmp.host; + } else { + gServer = config.server(); + } + + if (!options.parent.token && !config.accessToken()) { + console.log('Run %s first or provide %s', 'surfer login'.bold, '--token '.bold); + process.exit(1); + } - console.error('Using server %s', config.server().cyan); + gQuery = { access_token: options.parent.token || config.accessToken() }; + + console.error('Using server %s', gServer.cyan); } function collectFiles(filesOrFolders, options) { @@ -54,7 +69,7 @@ function collectFiles(filesOrFolders, options) { return tmp; } -function login(uri) { +function login(uri, options) { var tmp = url.parse(uri); if (!tmp.slashes) tmp = url.parse('https://' + uri); @@ -62,8 +77,10 @@ function login(uri) { console.log('Using server', server.cyan); - var username = readlineSync.question('Username: '); - var password = readlineSync.question('Password: ', { hideEchoBack: true, mask: '' }); + var username = options.username || readlineSync.question('Username: '); + var password = options.password || readlineSync.question('Password: ', { hideEchoBack: true, mask: '' }); + + if (!username || !password) process.exit(1); superagent.post(server + '/api/login').send({ username: username, password: password }).end(function (error, result) { if (error && error.code === 'ENOTFOUND') { @@ -76,7 +93,11 @@ function login(uri) { } if (result.status !== 201) { console.log('Login failed.\n'.red); - return login(uri); + + // remove the password to avoid a login loop + delete options.password; + + return login(uri, options); } // TODO remove at some point, this is just to clear the previous old version values @@ -86,49 +107,77 @@ function login(uri) { config.set('server', server); config.set('accessToken', result.body.accessToken); - gQuery = { access_token: result.body.accessToken }; - console.log('Login successful'.green); }); } -function put(filePath, otherFilePaths, options) { - checkConfig(); +function logout() { + if (!config.accessToken()) return console.log('Done'.green); - var destination = ''; + superagent.post(gServer + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) { + if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode); + if (error) console.log(error); - // take the last argument as destination - if (otherFilePaths.length > 0) { - destination = otherFilePaths.pop(); - if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; - } + // TODO remove at some point, this is just to clear the previous old version values + config.set('username', ''); + config.set('password', ''); + config.set('server', ''); + config.set('accessToken', ''); + + console.log('Done'.green); + }); +} - var files = collectFiles([ filePath ].concat(otherFilePaths), options); +function putOne(filePath, destination, options, callback) { + const absoluteFilePath = path.resolve(filePath); + const stat = safe.fs.statSync(absoluteFilePath); + if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`); + + let files, base; + + if (stat.isFile()) { + base = destination + path.basename(filePath); + files = [ absoluteFilePath ]; + } else if (stat.isDirectory()) { + base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/'); + files = collectFiles([ absoluteFilePath ], options); + } else { + return callback(); // ignore + } async.eachSeries(files, function (file, callback) { - 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); - } + let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file + let destinationPath = base + relativeFilePath; + console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan); - var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; - console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); - - superagent.post(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { + superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode)); if (error) return callback(error); - console.log('Uploaded to ' + config.server() + destinationPath); + console.log('Uploaded to ' + gServer + destinationPath); callback(null); }); - }, function (error) { + }, callback); +} + +function put(filePaths, options) { + checkConfig(options); + + if (filePaths.length < 2) { + console.log('target directory is required.'.red); + process.exit(1); + } + + let destination = filePaths.pop(); + if (!path.isAbsolute(destination)) { + console.log('target directory must be absolute'.red); + process.exit(1); + } + if (!destination.endsWith('/')) destination += '/'; + + async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) { if (error) { console.log('Failed to put file.', error.message.red); process.exit(1); @@ -138,13 +187,13 @@ function put(filePath, otherFilePaths, options) { }); } -function get(filePath) { - checkConfig(); +function get(filePath, options) { + checkConfig(options); // if no argument provided, fetch root filePath = filePath || '/'; - request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) { + request.get(gServer + API + filePath, { qs: gQuery }, function (error, result, body) { if (result && result.statusCode === 401) return console.log('Login failed'); if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); if (error) return console.error(error); @@ -164,7 +213,7 @@ function get(filePath) { process.stdout.write(body); } }); - // var req = superagent.get(config.server() + API + filePath); + // var req = superagent.get(gServer + API + filePath); // req.query(gQuery); // req.end(function (error, result) { // if (error && error.status === 401) return console.log('Login failed'); @@ -183,14 +232,14 @@ function get(filePath) { } function del(filePath, options) { - checkConfig(); + checkConfig(options); var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); query.recursive = options.recursive; query.dryRun = options.dryRun; var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); - superagent.del(config.server() + API + relativeFilePath).query(query).end(function (error, result) { + superagent.del(gServer + API + relativeFilePath).query(query).end(function (error, result) { if (error && error.status === 401) return console.log('Login failed'.red); if (error && error.status === 404) return console.log('No such file or directory'); if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow);