aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/actions.js')
-rw-r--r--cli/actions.js37
1 files changed, 22 insertions, 15 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
21var API = '/api/files/'; 21var API = '/api/files/';
22 22
23var gServer = '';
23var gQuery = {}; 24var gQuery = {};
24 25
25function checkConfig() { 26function 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
36function collectFiles(filesOrFolders, options) { 43function collectFiles(filesOrFolders, options) {
@@ -100,7 +107,7 @@ function login(uri, options) {
100function logout() { 107function 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
117function put(filePath, otherFilePaths, options) { 124function 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
163function get(filePath) { 170function 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
207function del(filePath, options) { 214function 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);