From 8c3ae0719e1f7d266ee04d86e7e1c3756745d372 Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Sat, 27 Jun 2015 17:07:42 +0200 Subject: Support recursive put --- cli/actions.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cli/config.js | 62 +++++++++++++++++++++++++++++++++++++ cli/surfer.js | 39 ++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 cli/actions.js create mode 100644 cli/config.js create mode 100755 cli/surfer.js (limited to 'cli') diff --git a/cli/actions.js b/cli/actions.js new file mode 100644 index 0000000..592d809 --- /dev/null +++ b/cli/actions.js @@ -0,0 +1,96 @@ +'use strict'; + +exports.login = login; +exports.put = put; +exports.get = get; +exports.del = del; + +var superagent = require('superagent'), + config = require('./config'), + async = require('async'), + fs = require('fs'), + path = require('path'); + +require('colors'); + +var API = '/api/files/'; + +function checkConfig() { + if (!config.server()) { + console.log('You have run "login" first'); + process.exit(1); + } +} + +function collectFiles(filesOrFolders) { + var tmp = []; + + filesOrFolders.forEach(function (filePath) { + var stat = fs.statSync(filePath); + + if (stat.isFile()) { + tmp.push(filePath); + } else if (stat.isDirectory()) { + var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); }); + tmp = tmp.concat(collectFiles(files)); + } else { + console.log('Skipping %s', filePath.cyan); + } + }); + + return tmp; +} + +function login(server) { + console.log('Using server', server); + config.set('server', server); +} + +function put(filePath, otherFilePaths) { + checkConfig(); + + var files = collectFiles([ filePath ].concat(otherFilePaths)); + + async.eachSeries(files, function (file, callback) { + var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1); + + console.log('Uploading file %s', relativeFilePath.cyan); + + superagent.put(config.server() + API + relativeFilePath).attach('file', file).end(callback); + }, function (error) { + if (error) { + console.log('Failed to put file.', error); + process.exit(1); + } + + console.log('Done'); + }); +} + +function get(filePath) { + checkConfig(); + + var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); + superagent.get(config.server() + API + relativeFilePath).end(function (error, result) { + if (error) return console.log('Failed', result ? result.body : error); + + if (result.body && result.body.entries) { + console.log('Files:'); + result.body.entries.forEach(function (entry) { + console.log('\t %s', entry); + }); + } else { + console.log(result.text); + } + }); +} + +function del(filePath) { + checkConfig(); + + var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); + superagent.del(config.server() + API + relativeFilePath).end(function (error, result) { + if (error) return console.log('Failed', result ? result.body : error); + console.log('Success', result.body); + }); +} diff --git a/cli/config.js b/cli/config.js new file mode 100644 index 0000000..a3708b8 --- /dev/null +++ b/cli/config.js @@ -0,0 +1,62 @@ +/* jshint node:true */ + +'use strict'; + +var fs = require('fs'), + path = require('path'), + safe = require('safetydance'), + _ = require('underscore'); + +exports = module.exports = { + clear: clear, + set: set, + get: get, + unset: unset, + has: has, + + // convenience + server: function () { return get('server'); } +}; + +var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; +var CONFIG_FILE_PATH = path.join(HOME, '.surfer.json'); + +var gConfig = (function () { + return safe.JSON.parse(safe.fs.readFileSync(CONFIG_FILE_PATH)) || {}; +})(); + +function save() { + fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(gConfig, null, 4)); +} + +function clear() { + safe.fs.unlinkSync(CONFIG_FILE_PATH); +} + +function set(key, value) { + if (typeof key === 'object') { + _.extend(gConfig, key); + } else { + safe.set(gConfig, key, value); + } + save(); +} + +function get(key) { + return safe.query(gConfig, key); +} + +function unset(key /*, .... */) { + for (var i = 0; i < arguments.length; i++) { + gConfig = safe.unset(gConfig, arguments[i]); + } + + save(); +} + +function has(key /*, ... */) { + for (var i = 0; i < arguments.length; i++) { + if (!(arguments[i] in gConfig)) return false; + } + return true; +} diff --git a/cli/surfer.js b/cli/surfer.js new file mode 100755 index 0000000..d906d62 --- /dev/null +++ b/cli/surfer.js @@ -0,0 +1,39 @@ +#!/usr/bin/env node + +'use strict'; + +var program = require('commander'), + actions = require('./actions'); + +// Allow self signed certs! +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; + +program.version('0.1.0'); + +program.command('login') + .description('Login to server') + .action(actions.login); + +program.command('put [files...]') + .description('Put a file') + .action(actions.put); + +program.command('get') + .description('Get a file or directory') + .action(actions.get); + +program.command('del') + .description('Delete a file') + .action(actions.del); + +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); + } +} -- cgit v1.2.3