diff options
author | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 12:34:15 +0100 |
---|---|---|
committer | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 12:34:15 +0100 |
commit | c9d33e20bf7d74b0d8a7eb1e1c89e8a845098460 (patch) | |
tree | f19a3b514adbbf333e6dabff39d32e3ed7c77c37 /cli | |
parent | cc6510cf142f7ca9994a3e0ce37724a5884729fb (diff) | |
download | Surfer-c9d33e20bf7d74b0d8a7eb1e1c89e8a845098460.tar.gz Surfer-c9d33e20bf7d74b0d8a7eb1e1c89e8a845098460.tar.zst Surfer-c9d33e20bf7d74b0d8a7eb1e1c89e8a845098460.zip |
Support last argument as destination
Diffstat (limited to 'cli')
-rw-r--r-- | cli/actions.js | 28 | ||||
-rwxr-xr-x | cli/surfer.js | 3 |
2 files changed, 22 insertions, 9 deletions
diff --git a/cli/actions.js b/cli/actions.js index b49ce59..7a361b7 100644 --- a/cli/actions.js +++ b/cli/actions.js | |||
@@ -8,6 +8,7 @@ exports.del = del; | |||
8 | var superagent = require('superagent'), | 8 | var superagent = require('superagent'), |
9 | config = require('./config.js'), | 9 | config = require('./config.js'), |
10 | readlineSync = require('readline-sync'), | 10 | readlineSync = require('readline-sync'), |
11 | safe = require('safetydance'), | ||
11 | async = require('async'), | 12 | async = require('async'), |
12 | fs = require('fs'), | 13 | fs = require('fs'), |
13 | request = require('request'), | 14 | request = require('request'), |
@@ -28,7 +29,7 @@ function checkConfig() { | |||
28 | 29 | ||
29 | gQuery = { username: config.username(), password: config.password() }; | 30 | gQuery = { username: config.username(), password: config.password() }; |
30 | 31 | ||
31 | console.error('Using server %s', config.server().yellow); | 32 | console.error('Using server %s', config.server().cyan); |
32 | } | 33 | } |
33 | 34 | ||
34 | function collectFiles(filesOrFolders, options) { | 35 | function collectFiles(filesOrFolders, options) { |
@@ -93,6 +94,14 @@ function login(uri) { | |||
93 | function put(filePath, otherFilePaths, options) { | 94 | function put(filePath, otherFilePaths, options) { |
94 | checkConfig(); | 95 | checkConfig(); |
95 | 96 | ||
97 | var destination = ''; | ||
98 | |||
99 | // take the last argument as destination | ||
100 | if (otherFilePaths.length > 0) { | ||
101 | destination = otherFilePaths.pop(); | ||
102 | if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; | ||
103 | } | ||
104 | |||
96 | var files = collectFiles([ filePath ].concat(otherFilePaths), options); | 105 | var files = collectFiles([ filePath ].concat(otherFilePaths), options); |
97 | 106 | ||
98 | async.eachSeries(files, function (file, callback) { | 107 | async.eachSeries(files, function (file, callback) { |
@@ -106,7 +115,7 @@ function put(filePath, otherFilePaths, options) { | |||
106 | relativeFilePath = path.basename(file); | 115 | relativeFilePath = path.basename(file); |
107 | } | 116 | } |
108 | 117 | ||
109 | var destinationPath = (options.destination ? '/' + options.destination : '') + '/' + relativeFilePath; | 118 | var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; |
110 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); | 119 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); |
111 | 120 | ||
112 | superagent.put(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { | 121 | superagent.put(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { |
@@ -136,14 +145,19 @@ function get(filePath) { | |||
136 | request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) { | 145 | request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) { |
137 | if (error) return console.error(error); | 146 | if (error) return console.error(error); |
138 | if (result.statusCode === 401) return console.log('Login failed'); | 147 | if (result.statusCode === 401) return console.log('Login failed'); |
139 | if (result.statusCode === 404) return console.log('No such file or directory'); | 148 | if (result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); |
140 | 149 | ||
141 | // 222 indicates directory listing | 150 | // 222 indicates directory listing |
142 | if (result.statusCode === 222) { | 151 | if (result.statusCode === 222) { |
143 | console.log('Files:'); | 152 | var files = safe.JSON.parse(body); |
144 | JSON.parse(body).entries.forEach(function (entry) { | 153 | if (!files || files.entries.length === 0) { |
145 | console.log('\t %s', entry); | 154 | console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow); |
146 | }); | 155 | } else { |
156 | console.log('Files:'); | ||
157 | files.entries.forEach(function (entry) { | ||
158 | console.log('\t %s', entry); | ||
159 | }); | ||
160 | } | ||
147 | } else { | 161 | } else { |
148 | process.stdout.write(body); | 162 | process.stdout.write(body); |
149 | } | 163 | } |
diff --git a/cli/surfer.js b/cli/surfer.js index 8920641..b311658 100755 --- a/cli/surfer.js +++ b/cli/surfer.js | |||
@@ -15,9 +15,8 @@ program.command('login <url>') | |||
15 | .action(actions.login); | 15 | .action(actions.login); |
16 | 16 | ||
17 | program.command('put <file> [files...]') | 17 | program.command('put <file> [files...]') |
18 | .option('-d --destination <folder>', 'Destination folder. This is prepended to the relative <file> path') | ||
19 | .option('-a --all', 'Also include hidden files and folders.', false) | 18 | .option('-a --all', 'Also include hidden files and folders.', false) |
20 | .description('Put a file') | 19 | .description('Put a file, last argument is destination if provided') |
21 | .action(actions.put); | 20 | .action(actions.put); |
22 | 21 | ||
23 | program.command('get [file]') | 22 | program.command('get [file]') |