X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=cli%2Factions.js;h=e5d1731eb739eda50d04a80ac03c9dae7dbeca73;hb=ec4c48f288f465d66accb4fe4b2e872feff38715;hp=69ffa10329e225d7592791468e9c51e284369b13;hpb=aa88a75382d0f5ff2929768a412d8ec64dfc6296;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git diff --git a/cli/actions.js b/cli/actions.js index 69ffa10..e5d1731 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,24 @@ require('colors'); var API = '/api/files/'; +var gServer = ''; var gQuery = {}; -function checkConfig() { - if (!config.server() || !config.username() || !config.password()) { - 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 = { username: config.username(), password: config.password() }; + 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); + gServer = options.parent.server || config.server(); + gQuery = { access_token: options.parent.token || config.accessToken() }; + + console.error('Using server %s', gServer.cyan); } function collectFiles(filesOrFolders, options) { @@ -54,7 +62,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,10 +70,12 @@ function login(uri) { console.log('Using server', server.cyan); - var username = readlineSync.question('Username: '); - var password = readlineSync.question('Password: ', { noEchoBack: true }); + var username = options.username || readlineSync.question('Username: '); + var password = options.password || readlineSync.question('Password: ', { hideEchoBack: true, mask: '' }); + + if (!username || !password) process.exit(1); - superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) { + superagent.post(server + '/api/login').send({ username: username, password: password }).end(function (error, result) { if (error && error.code === 'ENOTFOUND') { console.log('Server %s not found.'.red, server.bold); process.exit(1); @@ -74,25 +84,45 @@ function login(uri) { console.log('Failed to connect to server %s'.red, server.bold, error.code); process.exit(1); } - if (result.status === 401) { - console.log('Login failed.'.red); - process.exit(1); + if (result.status !== 201) { + console.log('Login failed.\n'.red); + + // 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 + config.set('username', ''); + config.set('password', ''); + config.set('server', server); - config.set('username', username); + config.set('accessToken', result.body.accessToken); - // TODO this is clearly bad and needs fixing - config.set('password', password); + console.log('Login successful'.green); + }); +} - gQuery = { username: username, password: password }; +function logout() { + if (!config.accessToken()) return console.log('Done'.green); - console.log('Login successful'.green); + 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); + + // 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); }); } function put(filePath, otherFilePaths, options) { - checkConfig(); + checkConfig(options); var destination = ''; @@ -118,12 +148,12 @@ function put(filePath, otherFilePaths, options) { 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) { + 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); }); @@ -137,13 +167,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); @@ -163,7 +193,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'); @@ -182,14 +212,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);