]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - cli/actions.js
Bump version
[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 gQuery = {};
24
25 function checkConfig() {
26 if (!config.server() || !config.accessToken()) {
27 console.log('Run %s first', 'surfer login'.yellow);
28 process.exit(1);
29 }
30
31 gQuery = { access_token: config.accessToken() };
32
33 console.error('Using server %s', config.server().cyan);
34 }
35
36 function collectFiles(filesOrFolders, options) {
37 var tmp = [];
38
39 filesOrFolders.forEach(function (filePath) {
40 var baseName = path.basename(filePath);
41 if (!options.all && baseName[0] === '.' && baseName.length > 1) return;
42
43 var stat = fs.statSync(filePath);
44
45 if (stat.isFile()) {
46 tmp.push(filePath);
47 } else if (stat.isDirectory()) {
48 var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
49 tmp = tmp.concat(collectFiles(files, options));
50 } else {
51 console.log('Skipping %s', filePath.cyan);
52 }
53 });
54
55 return tmp;
56 }
57
58 function login(uri, options) {
59 var tmp = url.parse(uri);
60 if (!tmp.slashes) tmp = url.parse('https://' + uri);
61
62 var server = tmp.protocol + '//' + tmp.host;
63
64 console.log('Using server', server.cyan);
65
66 var username = options.username || readlineSync.question('Username: ');
67 var password = options.password || readlineSync.question('Password: ', { hideEchoBack: true, mask: '' });
68
69 if (!username || !password) process.exit(1);
70
71 superagent.post(server + '/api/login').send({ username: username, password: password }).end(function (error, result) {
72 if (error && error.code === 'ENOTFOUND') {
73 console.log('Server %s not found.'.red, server.bold);
74 process.exit(1);
75 }
76 if (error && error.code) {
77 console.log('Failed to connect to server %s'.red, server.bold, error.code);
78 process.exit(1);
79 }
80 if (result.status !== 201) {
81 console.log('Login failed.\n'.red);
82
83 // remove the password to avoid a login loop
84 delete options.password;
85
86 return login(uri, options);
87 }
88
89 // TODO remove at some point, this is just to clear the previous old version values
90 config.set('username', '');
91 config.set('password', '');
92
93 config.set('server', server);
94 config.set('accessToken', result.body.accessToken);
95
96 console.log('Login successful'.green);
97 });
98 }
99
100 function logout() {
101 if (!config.accessToken()) return console.log('Done'.green);
102
103 superagent.post(config.server() + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) {
104 if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode);
105 if (error) console.log(error);
106
107 // TODO remove at some point, this is just to clear the previous old version values
108 config.set('username', '');
109 config.set('password', '');
110 config.set('server', '');
111 config.set('accessToken', '');
112
113 console.log('Done'.green);
114 });
115 }
116
117 function put(filePath, otherFilePaths, options) {
118 checkConfig();
119
120 var destination = '';
121
122 // take the last argument as destination
123 if (otherFilePaths.length > 0) {
124 destination = otherFilePaths.pop();
125 if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/';
126 }
127
128 var files = collectFiles([ filePath ].concat(otherFilePaths), options);
129
130 async.eachSeries(files, function (file, callback) {
131 var relativeFilePath;
132
133 if (path.isAbsolute(file)) {
134 relativeFilePath = path.basename(file);
135 } else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir
136 relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
137 } else { // relative but somewhere else
138 relativeFilePath = path.basename(file);
139 }
140
141 var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath;
142 console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan);
143
144 superagent.post(config.server() + 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'));
146 if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode));
147 if (error) return callback(error);
148
149 console.log('Uploaded to ' + config.server() + destinationPath);
150
151 callback(null);
152 });
153 }, function (error) {
154 if (error) {
155 console.log('Failed to put file.', error.message.red);
156 process.exit(1);
157 }
158
159 console.log('Done');
160 });
161 }
162
163 function get(filePath) {
164 checkConfig();
165
166 // if no argument provided, fetch root
167 filePath = filePath || '/';
168
169 request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) {
170 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);
172 if (error) return console.error(error);
173
174 // 222 indicates directory listing
175 if (result.statusCode === 222) {
176 var files = safe.JSON.parse(body);
177 if (!files || files.entries.length === 0) {
178 console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow);
179 } else {
180 console.log('Entries:');
181 files.entries.forEach(function (entry) {
182 console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath);
183 });
184 }
185 } else {
186 process.stdout.write(body);
187 }
188 });
189 // var req = superagent.get(config.server() + API + filePath);
190 // req.query(gQuery);
191 // req.end(function (error, result) {
192 // if (error && error.status === 401) return console.log('Login failed');
193 // if (error && error.status === 404) return console.log('No such file or directory');
194 // if (error) return console.log('Failed', result ? result.body : error);
195
196 // if (result.body && result.body.entries) {
197 // console.log('Files:');
198 // result.body.entries.forEach(function (entry) {
199 // console.log('\t %s', entry);
200 // });
201 // } else {
202 // req.pipe(process.stdout);
203 // }
204 // });
205 }
206
207 function del(filePath, options) {
208 checkConfig();
209
210 var query = safe.JSON.parse(safe.JSON.stringify(gQuery));
211 query.recursive = options.recursive;
212 query.dryRun = options.dryRun;
213
214 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
215 superagent.del(config.server() + API + relativeFilePath).query(query).end(function (error, result) {
216 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');
218 if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow);
219 if (error) return console.log('Failed', result ? result.body : error);
220
221 if (options.dryRun) {
222 console.log('This would remove %s files:', result.body.entries.length);
223 result.body.entries.forEach(function (entry) {
224 console.log('\t %s', entry);
225 });
226 } else {
227 console.log('Success. Removed %s files.', result.body.entries.length);
228 }
229 });
230 }