]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - cli/actions.js
Make listen port and ldap filter more flexible
[perso/Immae/Projets/Nodejs/Surfer.git] / cli / actions.js
1 'use strict';
2
3 exports.login = login;
4 exports.logout = logout;
5 exports.put = put;
6 exports.get = get;
7 exports.del = del;
8
9 var superagent = require('superagent'),
10 config = require('./config.js'),
11 readlineSync = require('readline-sync'),
12 safe = require('safetydance'),
13 async = require('async'),
14 fs = require('fs'),
15 request = require('request'),
16 url = require('url'),
17 path = require('path');
18
19 require('colors');
20
21 var API = '/api/files/';
22
23 var gServer = '';
24 var gQuery = {};
25
26 function checkConfig(options) {
27 if (!options.parent.server && !config.server()) {
28 console.log('Run %s first, or provide %s', 'surfer login'.bold, '--server <url>'.bold);
29 process.exit(1);
30 }
31
32 if (options.parent.server) {
33 var tmp = url.parse(options.parent.server);
34 if (!tmp.slashes) tmp = url.parse('https://' + options.parent.server);
35 gServer = tmp.protocol + '//' + tmp.host;
36 } else {
37 gServer = config.server();
38 }
39
40 if (!options.parent.token && !config.accessToken()) {
41 console.log('Run %s first or provide %s', 'surfer login'.bold, '--token <access token>'.bold);
42 process.exit(1);
43 }
44
45 gQuery = { access_token: options.parent.token || config.accessToken() };
46
47 console.error('Using server %s', gServer.cyan);
48 }
49
50 function collectFiles(filesOrFolders, options) {
51 var tmp = [];
52
53 filesOrFolders.forEach(function (filePath) {
54 var baseName = path.basename(filePath);
55 if (!options.all && baseName[0] === '.' && baseName.length > 1) return;
56
57 var stat = fs.statSync(filePath);
58
59 if (stat.isFile()) {
60 tmp.push(filePath);
61 } else if (stat.isDirectory()) {
62 var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
63 tmp = tmp.concat(collectFiles(files, options));
64 } else {
65 console.log('Skipping %s', filePath.cyan);
66 }
67 });
68
69 return tmp;
70 }
71
72 function login(uri, options) {
73 var tmp = url.parse(uri);
74 if (!tmp.slashes) tmp = url.parse('https://' + uri);
75
76 var server = tmp.protocol + '//' + tmp.host;
77
78 console.log('Using server', server.cyan);
79
80 var username = options.username || readlineSync.question('Username: ');
81 var password = options.password || readlineSync.question('Password: ', { hideEchoBack: true, mask: '' });
82
83 if (!username || !password) process.exit(1);
84
85 superagent.post(server + '/api/login').send({ username: username, password: password }).end(function (error, result) {
86 if (error && error.code === 'ENOTFOUND') {
87 console.log('Server %s not found.'.red, server.bold);
88 process.exit(1);
89 }
90 if (error && error.code) {
91 console.log('Failed to connect to server %s'.red, server.bold, error.code);
92 process.exit(1);
93 }
94 if (result.status !== 201) {
95 console.log('Login failed.\n'.red);
96
97 // remove the password to avoid a login loop
98 delete options.password;
99
100 return login(uri, options);
101 }
102
103 // TODO remove at some point, this is just to clear the previous old version values
104 config.set('username', '');
105 config.set('password', '');
106
107 config.set('server', server);
108 config.set('accessToken', result.body.accessToken);
109
110 console.log('Login successful'.green);
111 });
112 }
113
114 function logout() {
115 if (!config.accessToken()) return console.log('Done'.green);
116
117 superagent.post(gServer + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) {
118 if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode);
119 if (error) console.log(error);
120
121 // TODO remove at some point, this is just to clear the previous old version values
122 config.set('username', '');
123 config.set('password', '');
124 config.set('server', '');
125 config.set('accessToken', '');
126
127 console.log('Done'.green);
128 });
129 }
130
131 function putOne(filePath, destination, options, callback) {
132 const absoluteFilePath = path.resolve(filePath);
133 const stat = safe.fs.statSync(absoluteFilePath);
134 if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`);
135
136 let files, base;
137
138 if (stat.isFile()) {
139 base = destination + path.basename(filePath);
140 files = [ absoluteFilePath ];
141 } else if (stat.isDirectory()) {
142 base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/');
143 files = collectFiles([ absoluteFilePath ], options);
144 } else {
145 return callback(); // ignore
146 }
147
148 async.eachSeries(files, function (file, callback) {
149 let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file
150 let destinationPath = base + relativeFilePath;
151 console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan);
152
153 superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
154 if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed'));
155 if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode));
156 if (error) return callback(error);
157
158 console.log('Uploaded to ' + gServer + destinationPath);
159
160 callback(null);
161 });
162 }, callback);
163 }
164
165 function put(filePaths, options) {
166 checkConfig(options);
167
168 if (filePaths.length < 2) {
169 console.log('target directory is required.'.red);
170 process.exit(1);
171 }
172
173 let destination = filePaths.pop();
174 if (!path.isAbsolute(destination)) {
175 console.log('target directory must be absolute'.red);
176 process.exit(1);
177 }
178 if (!destination.endsWith('/')) destination += '/';
179
180 async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) {
181 if (error) {
182 console.log('Failed to put file.', error.message.red);
183 process.exit(1);
184 }
185
186 console.log('Done');
187 });
188 }
189
190 function get(filePath, options) {
191 checkConfig(options);
192
193 // if no argument provided, fetch root
194 filePath = filePath || '/';
195
196 request.get(gServer + API + filePath, { qs: gQuery }, function (error, result, body) {
197 if (result && result.statusCode === 401) return console.log('Login failed');
198 if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow);
199 if (error) return console.error(error);
200
201 // 222 indicates directory listing
202 if (result.statusCode === 222) {
203 var files = safe.JSON.parse(body);
204 if (!files || files.entries.length === 0) {
205 console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow);
206 } else {
207 console.log('Entries:');
208 files.entries.forEach(function (entry) {
209 console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath);
210 });
211 }
212 } else {
213 process.stdout.write(body);
214 }
215 });
216 // var req = superagent.get(gServer + API + filePath);
217 // req.query(gQuery);
218 // req.end(function (error, result) {
219 // if (error && error.status === 401) return console.log('Login failed');
220 // if (error && error.status === 404) return console.log('No such file or directory');
221 // if (error) return console.log('Failed', result ? result.body : error);
222
223 // if (result.body && result.body.entries) {
224 // console.log('Files:');
225 // result.body.entries.forEach(function (entry) {
226 // console.log('\t %s', entry);
227 // });
228 // } else {
229 // req.pipe(process.stdout);
230 // }
231 // });
232 }
233
234 function del(filePath, options) {
235 checkConfig(options);
236
237 var query = safe.JSON.parse(safe.JSON.stringify(gQuery));
238 query.recursive = options.recursive;
239 query.dryRun = options.dryRun;
240
241 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
242 superagent.del(gServer + API + relativeFilePath).query(query).end(function (error, result) {
243 if (error && error.status === 401) return console.log('Login failed'.red);
244 if (error && error.status === 404) return console.log('No such file or directory');
245 if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow);
246 if (error) return console.log('Failed', result ? result.body : error);
247
248 if (options.dryRun) {
249 console.log('This would remove %s files:', result.body.entries.length);
250 result.body.entries.forEach(function (entry) {
251 console.log('\t %s', entry);
252 });
253 } else {
254 console.log('Success. Removed %s files.', result.body.entries.length);
255 }
256 });
257 }