Commit ec4c48f2 by Johannes Zellner

Add --server and --token options to cli tool

1 parent c2e5d204
Showing with 31 additions and 20 deletions
...@@ -20,17 +20,24 @@ require('colors'); ...@@ -20,17 +20,24 @@ require('colors');
var API = '/api/files/'; var API = '/api/files/';
var gServer = '';
var gQuery = {}; var gQuery = {};
function checkConfig() { function checkConfig(options) {
if (!config.server() || !config.accessToken()) { if (!options.parent.server && !config.server()) {
console.log('Run %s first', 'surfer login'.yellow); console.log('Run %s first, or provide %s', 'surfer login'.bold, '--server <url>'.bold);
process.exit(1); process.exit(1);
} }
gQuery = { access_token: config.accessToken() }; if (!options.parent.token && !config.accessToken()) {
console.log('Run %s first or provide %s', 'surfer login'.bold, '--token <access token>'.bold);
process.exit(1);
}
gServer = options.parent.server || config.server();
gQuery = { access_token: options.parent.token || config.accessToken() };
console.error('Using server %s', config.server().cyan); console.error('Using server %s', gServer.cyan);
} }
function collectFiles(filesOrFolders, options) { function collectFiles(filesOrFolders, options) {
...@@ -100,7 +107,7 @@ function login(uri, options) { ...@@ -100,7 +107,7 @@ function login(uri, options) {
function logout() { function logout() {
if (!config.accessToken()) return console.log('Done'.green); if (!config.accessToken()) return console.log('Done'.green);
superagent.post(config.server() + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) { 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 (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode);
if (error) console.log(error); if (error) console.log(error);
...@@ -115,7 +122,7 @@ function logout() { ...@@ -115,7 +122,7 @@ function logout() {
} }
function put(filePath, otherFilePaths, options) { function put(filePath, otherFilePaths, options) {
checkConfig(); checkConfig(options);
var destination = ''; var destination = '';
...@@ -141,12 +148,12 @@ function put(filePath, otherFilePaths, options) { ...@@ -141,12 +148,12 @@ function put(filePath, otherFilePaths, options) {
var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath;
console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); 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 === 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 (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode));
if (error) return callback(error); if (error) return callback(error);
console.log('Uploaded to ' + config.server() + destinationPath); console.log('Uploaded to ' + gServer + destinationPath);
callback(null); callback(null);
}); });
...@@ -160,13 +167,13 @@ function put(filePath, otherFilePaths, options) { ...@@ -160,13 +167,13 @@ function put(filePath, otherFilePaths, options) {
}); });
} }
function get(filePath) { function get(filePath, options) {
checkConfig(); checkConfig(options);
// if no argument provided, fetch root // if no argument provided, fetch root
filePath = filePath || '/'; 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 === 401) return console.log('Login failed');
if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow);
if (error) return console.error(error); if (error) return console.error(error);
...@@ -186,7 +193,7 @@ function get(filePath) { ...@@ -186,7 +193,7 @@ function get(filePath) {
process.stdout.write(body); process.stdout.write(body);
} }
}); });
// var req = superagent.get(config.server() + API + filePath); // var req = superagent.get(gServer + API + filePath);
// req.query(gQuery); // req.query(gQuery);
// req.end(function (error, result) { // req.end(function (error, result) {
// if (error && error.status === 401) return console.log('Login failed'); // if (error && error.status === 401) return console.log('Login failed');
...@@ -205,14 +212,14 @@ function get(filePath) { ...@@ -205,14 +212,14 @@ function get(filePath) {
} }
function del(filePath, options) { function del(filePath, options) {
checkConfig(); checkConfig(options);
var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); var query = safe.JSON.parse(safe.JSON.stringify(gQuery));
query.recursive = options.recursive; query.recursive = options.recursive;
query.dryRun = options.dryRun; query.dryRun = options.dryRun;
var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); 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 === 401) return console.log('Login failed'.red);
if (error && error.status === 404) return console.log('No such file or directory'); 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); if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow);
......
...@@ -7,6 +7,10 @@ var program = require('commander'), ...@@ -7,6 +7,10 @@ var program = require('commander'),
program.version(require('../package.json').version); program.version(require('../package.json').version);
// Those override the login settings if provided
program.option('-s, --server <url>', 'Server URL (optional)');
program.option('-t, --token <access token>', 'Server Access Token (optional)');
program.command('login <url>') program.command('login <url>')
.description('Login to server') .description('Login to server')
.option('--username [username]', 'Username (optional)') .option('--username [username]', 'Username (optional)')
...@@ -37,9 +41,9 @@ program.parse(process.argv); ...@@ -37,9 +41,9 @@ program.parse(process.argv);
if (!process.argv.slice(2).length) { if (!process.argv.slice(2).length) {
program.outputHelp(); program.outputHelp();
} else { // https://github.com/tj/commander.js/issues/338 } else { // https://github.com/tj/commander.js/issues/338
var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; }); // var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; });
if (!knownCommand) { // if (!knownCommand) {
console.error('Unknown command: ' + process.argv[2]); // console.error('Unknown command: ' + process.argv[2]);
process.exit(1); // process.exit(1);
} // }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!