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 <url>'.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 <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 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);
}
function put(filePath, otherFilePaths, options) {
- checkConfig();
+ checkConfig(options);
var destination = '';
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 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);
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');
}
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);
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>')
.description('Login to server')
.option('--username [username]', 'Username (optional)')
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);
+ // }
}