From ec4c48f288f465d66accb4fe4b2e872feff38715 Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Wed, 7 Aug 2019 23:27:22 +0200 Subject: Add --server and --token options to cli tool --- cli/actions.js | 37 ++++++++++++++++++++++--------------- cli/surfer.js | 14 +++++++++----- 2 files changed, 31 insertions(+), 20 deletions(-) (limited to 'cli') diff --git a/cli/actions.js b/cli/actions.js index 2440c2d..e5d1731 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -20,17 +20,24 @@ require('colors'); var API = '/api/files/'; +var gServer = ''; var gQuery = {}; -function checkConfig() { - if (!config.server() || !config.accessToken()) { - console.log('Run %s first', 'surfer login'.yellow); +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.token && !config.accessToken()) { + console.log('Run %s first or provide %s', 'surfer login'.bold, '--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) { @@ -100,7 +107,7 @@ function login(uri, options) { function logout() { 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 (error) console.log(error); @@ -115,7 +122,7 @@ function logout() { } function put(filePath, otherFilePaths, options) { - checkConfig(); + checkConfig(options); var destination = ''; @@ -141,12 +148,12 @@ function put(filePath, otherFilePaths, options) { 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); }); @@ -160,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); @@ -186,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'); @@ -205,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); diff --git a/cli/surfer.js b/cli/surfer.js index 65773e3..33b4db6 100755 --- a/cli/surfer.js +++ b/cli/surfer.js @@ -7,6 +7,10 @@ var program = require('commander'), program.version(require('../package.json').version); +// Those override the login settings if provided +program.option('-s, --server ', 'Server URL (optional)'); +program.option('-t, --token ', 'Server Access Token (optional)'); + program.command('login ') .description('Login to server') .option('--username [username]', 'Username (optional)') @@ -37,9 +41,9 @@ program.parse(process.argv); if (!process.argv.slice(2).length) { program.outputHelp(); } else { // https://github.com/tj/commander.js/issues/338 - var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; }); - if (!knownCommand) { - console.error('Unknown command: ' + process.argv[2]); - process.exit(1); - } + // var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; }); + // if (!knownCommand) { + // console.error('Unknown command: ' + process.argv[2]); + // process.exit(1); + // } } -- cgit v1.2.3