aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli
diff options
context:
space:
mode:
authorJohannes Zellner <johannes@nebulon.de>2015-06-27 17:07:42 +0200
committerJohannes Zellner <johannes@nebulon.de>2015-06-27 17:07:42 +0200
commit8c3ae0719e1f7d266ee04d86e7e1c3756745d372 (patch)
treeb5c2c0e9d7d339490c3c8384dcffe264ae67e7de /cli
parent08b2ad7f716b3323ab8c168eb8a00a47fda03d66 (diff)
downloadSurfer-8c3ae0719e1f7d266ee04d86e7e1c3756745d372.tar.gz
Surfer-8c3ae0719e1f7d266ee04d86e7e1c3756745d372.tar.zst
Surfer-8c3ae0719e1f7d266ee04d86e7e1c3756745d372.zip
Support recursive put
Diffstat (limited to 'cli')
-rw-r--r--cli/actions.js96
-rw-r--r--cli/config.js62
-rwxr-xr-xcli/surfer.js39
3 files changed, 197 insertions, 0 deletions
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 @@
1'use strict';
2
3exports.login = login;
4exports.put = put;
5exports.get = get;
6exports.del = del;
7
8var superagent = require('superagent'),
9 config = require('./config'),
10 async = require('async'),
11 fs = require('fs'),
12 path = require('path');
13
14require('colors');
15
16var API = '/api/files/';
17
18function checkConfig() {
19 if (!config.server()) {
20 console.log('You have run "login" first');
21 process.exit(1);
22 }
23}
24
25function collectFiles(filesOrFolders) {
26 var tmp = [];
27
28 filesOrFolders.forEach(function (filePath) {
29 var stat = fs.statSync(filePath);
30
31 if (stat.isFile()) {
32 tmp.push(filePath);
33 } else if (stat.isDirectory()) {
34 var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
35 tmp = tmp.concat(collectFiles(files));
36 } else {
37 console.log('Skipping %s', filePath.cyan);
38 }
39 });
40
41 return tmp;
42}
43
44function login(server) {
45 console.log('Using server', server);
46 config.set('server', server);
47}
48
49function put(filePath, otherFilePaths) {
50 checkConfig();
51
52 var files = collectFiles([ filePath ].concat(otherFilePaths));
53
54 async.eachSeries(files, function (file, callback) {
55 var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
56
57 console.log('Uploading file %s', relativeFilePath.cyan);
58
59 superagent.put(config.server() + API + relativeFilePath).attach('file', file).end(callback);
60 }, function (error) {
61 if (error) {
62 console.log('Failed to put file.', error);
63 process.exit(1);
64 }
65
66 console.log('Done');
67 });
68}
69
70function get(filePath) {
71 checkConfig();
72
73 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
74 superagent.get(config.server() + API + relativeFilePath).end(function (error, result) {
75 if (error) return console.log('Failed', result ? result.body : error);
76
77 if (result.body && result.body.entries) {
78 console.log('Files:');
79 result.body.entries.forEach(function (entry) {
80 console.log('\t %s', entry);
81 });
82 } else {
83 console.log(result.text);
84 }
85 });
86}
87
88function del(filePath) {
89 checkConfig();
90
91 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
92 superagent.del(config.server() + API + relativeFilePath).end(function (error, result) {
93 if (error) return console.log('Failed', result ? result.body : error);
94 console.log('Success', result.body);
95 });
96}
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 @@
1/* jshint node:true */
2
3'use strict';
4
5var fs = require('fs'),
6 path = require('path'),
7 safe = require('safetydance'),
8 _ = require('underscore');
9
10exports = module.exports = {
11 clear: clear,
12 set: set,
13 get: get,
14 unset: unset,
15 has: has,
16
17 // convenience
18 server: function () { return get('server'); }
19};
20
21var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
22var CONFIG_FILE_PATH = path.join(HOME, '.surfer.json');
23
24var gConfig = (function () {
25 return safe.JSON.parse(safe.fs.readFileSync(CONFIG_FILE_PATH)) || {};
26})();
27
28function save() {
29 fs.writeFileSync(CONFIG_FILE_PATH, JSON.stringify(gConfig, null, 4));
30}
31
32function clear() {
33 safe.fs.unlinkSync(CONFIG_FILE_PATH);
34}
35
36function set(key, value) {
37 if (typeof key === 'object') {
38 _.extend(gConfig, key);
39 } else {
40 safe.set(gConfig, key, value);
41 }
42 save();
43}
44
45function get(key) {
46 return safe.query(gConfig, key);
47}
48
49function unset(key /*, .... */) {
50 for (var i = 0; i < arguments.length; i++) {
51 gConfig = safe.unset(gConfig, arguments[i]);
52 }
53
54 save();
55}
56
57function has(key /*, ... */) {
58 for (var i = 0; i < arguments.length; i++) {
59 if (!(arguments[i] in gConfig)) return false;
60 }
61 return true;
62}
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 @@
1#!/usr/bin/env node
2
3'use strict';
4
5var program = require('commander'),
6 actions = require('./actions');
7
8// Allow self signed certs!
9process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
10
11program.version('0.1.0');
12
13program.command('login')
14 .description('Login to server')
15 .action(actions.login);
16
17program.command('put <file> [files...]')
18 .description('Put a file')
19 .action(actions.put);
20
21program.command('get')
22 .description('Get a file or directory')
23 .action(actions.get);
24
25program.command('del')
26 .description('Delete a file')
27 .action(actions.del);
28
29program.parse(process.argv);
30
31if (!process.argv.slice(2).length) {
32 program.outputHelp();
33} else { // https://github.com/tj/commander.js/issues/338
34 var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; });
35 if (!knownCommand) {
36 console.error('Unknown command: ' + process.argv[2]);
37 process.exit(1);
38 }
39}