diff options
-rw-r--r-- | cli/actions.js | 37 | ||||
-rwxr-xr-x | cli/surfer.js | 14 |
2 files changed, 31 insertions, 20 deletions
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'); | |||
20 | 20 | ||
21 | var API = '/api/files/'; | 21 | var API = '/api/files/'; |
22 | 22 | ||
23 | var gServer = ''; | ||
23 | var gQuery = {}; | 24 | var gQuery = {}; |
24 | 25 | ||
25 | function checkConfig() { | 26 | function checkConfig(options) { |
26 | if (!config.server() || !config.accessToken()) { | 27 | if (!options.parent.server && !config.server()) { |
27 | console.log('Run %s first', 'surfer login'.yellow); | 28 | console.log('Run %s first, or provide %s', 'surfer login'.bold, '--server <url>'.bold); |
28 | process.exit(1); | 29 | process.exit(1); |
29 | } | 30 | } |
30 | 31 | ||
31 | gQuery = { access_token: config.accessToken() }; | 32 | if (!options.parent.token && !config.accessToken()) { |
33 | console.log('Run %s first or provide %s', 'surfer login'.bold, '--token <access token>'.bold); | ||
34 | process.exit(1); | ||
35 | } | ||
36 | |||
37 | gServer = options.parent.server || config.server(); | ||
38 | gQuery = { access_token: options.parent.token || config.accessToken() }; | ||
32 | 39 | ||
33 | console.error('Using server %s', config.server().cyan); | 40 | console.error('Using server %s', gServer.cyan); |
34 | } | 41 | } |
35 | 42 | ||
36 | function collectFiles(filesOrFolders, options) { | 43 | function collectFiles(filesOrFolders, options) { |
@@ -100,7 +107,7 @@ function login(uri, options) { | |||
100 | function logout() { | 107 | function logout() { |
101 | if (!config.accessToken()) return console.log('Done'.green); | 108 | if (!config.accessToken()) return console.log('Done'.green); |
102 | 109 | ||
103 | superagent.post(config.server() + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) { | 110 | superagent.post(gServer + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) { |
104 | if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode); | 111 | if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode); |
105 | if (error) console.log(error); | 112 | if (error) console.log(error); |
106 | 113 | ||
@@ -115,7 +122,7 @@ function logout() { | |||
115 | } | 122 | } |
116 | 123 | ||
117 | function put(filePath, otherFilePaths, options) { | 124 | function put(filePath, otherFilePaths, options) { |
118 | checkConfig(); | 125 | checkConfig(options); |
119 | 126 | ||
120 | var destination = ''; | 127 | var destination = ''; |
121 | 128 | ||
@@ -141,12 +148,12 @@ function put(filePath, otherFilePaths, options) { | |||
141 | var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; | 148 | var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; |
142 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); | 149 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); |
143 | 150 | ||
144 | superagent.post(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { | 151 | superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { |
145 | if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); | 152 | if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); |
146 | if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode)); | 153 | if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode)); |
147 | if (error) return callback(error); | 154 | if (error) return callback(error); |
148 | 155 | ||
149 | console.log('Uploaded to ' + config.server() + destinationPath); | 156 | console.log('Uploaded to ' + gServer + destinationPath); |
150 | 157 | ||
151 | callback(null); | 158 | callback(null); |
152 | }); | 159 | }); |
@@ -160,13 +167,13 @@ function put(filePath, otherFilePaths, options) { | |||
160 | }); | 167 | }); |
161 | } | 168 | } |
162 | 169 | ||
163 | function get(filePath) { | 170 | function get(filePath, options) { |
164 | checkConfig(); | 171 | checkConfig(options); |
165 | 172 | ||
166 | // if no argument provided, fetch root | 173 | // if no argument provided, fetch root |
167 | filePath = filePath || '/'; | 174 | filePath = filePath || '/'; |
168 | 175 | ||
169 | request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) { | 176 | request.get(gServer + API + filePath, { qs: gQuery }, function (error, result, body) { |
170 | if (result && result.statusCode === 401) return console.log('Login failed'); | 177 | if (result && result.statusCode === 401) return console.log('Login failed'); |
171 | if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); | 178 | if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); |
172 | if (error) return console.error(error); | 179 | if (error) return console.error(error); |
@@ -186,7 +193,7 @@ function get(filePath) { | |||
186 | process.stdout.write(body); | 193 | process.stdout.write(body); |
187 | } | 194 | } |
188 | }); | 195 | }); |
189 | // var req = superagent.get(config.server() + API + filePath); | 196 | // var req = superagent.get(gServer + API + filePath); |
190 | // req.query(gQuery); | 197 | // req.query(gQuery); |
191 | // req.end(function (error, result) { | 198 | // req.end(function (error, result) { |
192 | // if (error && error.status === 401) return console.log('Login failed'); | 199 | // if (error && error.status === 401) return console.log('Login failed'); |
@@ -205,14 +212,14 @@ function get(filePath) { | |||
205 | } | 212 | } |
206 | 213 | ||
207 | function del(filePath, options) { | 214 | function del(filePath, options) { |
208 | checkConfig(); | 215 | checkConfig(options); |
209 | 216 | ||
210 | var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); | 217 | var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); |
211 | query.recursive = options.recursive; | 218 | query.recursive = options.recursive; |
212 | query.dryRun = options.dryRun; | 219 | query.dryRun = options.dryRun; |
213 | 220 | ||
214 | var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); | 221 | var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); |
215 | superagent.del(config.server() + API + relativeFilePath).query(query).end(function (error, result) { | 222 | superagent.del(gServer + API + relativeFilePath).query(query).end(function (error, result) { |
216 | if (error && error.status === 401) return console.log('Login failed'.red); | 223 | if (error && error.status === 401) return console.log('Login failed'.red); |
217 | if (error && error.status === 404) return console.log('No such file or directory'); | 224 | if (error && error.status === 404) return console.log('No such file or directory'); |
218 | if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow); | 225 | 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'), | |||
7 | 7 | ||
8 | program.version(require('../package.json').version); | 8 | program.version(require('../package.json').version); |
9 | 9 | ||
10 | // Those override the login settings if provided | ||
11 | program.option('-s, --server <url>', 'Server URL (optional)'); | ||
12 | program.option('-t, --token <access token>', 'Server Access Token (optional)'); | ||
13 | |||
10 | program.command('login <url>') | 14 | program.command('login <url>') |
11 | .description('Login to server') | 15 | .description('Login to server') |
12 | .option('--username [username]', 'Username (optional)') | 16 | .option('--username [username]', 'Username (optional)') |
@@ -37,9 +41,9 @@ program.parse(process.argv); | |||
37 | if (!process.argv.slice(2).length) { | 41 | if (!process.argv.slice(2).length) { |
38 | program.outputHelp(); | 42 | program.outputHelp(); |
39 | } else { // https://github.com/tj/commander.js/issues/338 | 43 | } else { // https://github.com/tj/commander.js/issues/338 |
40 | var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; }); | 44 | // var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; }); |
41 | if (!knownCommand) { | 45 | // if (!knownCommand) { |
42 | console.error('Unknown command: ' + process.argv[2]); | 46 | // console.error('Unknown command: ' + process.argv[2]); |
43 | process.exit(1); | 47 | // process.exit(1); |
44 | } | 48 | // } |
45 | } | 49 | } |