]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - cli/actions.js
4 exports
.logout
= logout
;
9 var superagent
= require('superagent'),
10 config
= require('./config.js'),
11 readlineSync
= require('readline-sync'),
12 safe
= require('safetydance'),
13 async
= require('async'),
15 request
= require('request'),
17 path
= require('path');
21 var API
= '/api/files/';
25 function checkConfig() {
26 if (!config
.server() || !config
.accessToken()) {
27 console
.log('Run %s first', 'surfer login'.yellow
);
31 gQuery
= { access_token: config
.accessToken() };
33 console
.error('Using server %s', config
.server().cyan
);
36 function collectFiles(filesOrFolders
, options
) {
39 filesOrFolders
.forEach(function (filePath
) {
40 var baseName
= path
.basename(filePath
);
41 if (!options
.all
&& baseName
[0] === '.' && baseName
.length
> 1) return;
43 var stat
= fs
.statSync(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
));
51 console
.log('Skipping %s', filePath
.cyan
);
58 function login(uri
, options
) {
59 var tmp
= url
.parse(uri
);
60 if (!tmp
.slashes
) tmp
= url
.parse('https://' + uri
);
62 var server
= tmp
.protocol
+ '//' + tmp
.host
;
64 console
.log('Using server', server
.cyan
);
66 var username
= options
.username
|| readlineSync
.question('Username: ');
67 var password
= options
.password
|| readlineSync
.question('Password: ', { hideEchoBack: true, mask: '' });
69 if (!username
|| !password
) process
.exit(1);
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
);
76 if (error
&& error
.code
) {
77 console
.log('Failed to connect to server %s'.red
, server
.bold
, error
.code
);
80 if (result
.status
!== 201) {
81 console
.log('Login failed.\n'.red
);
83 // remove the password to avoid a login loop
84 delete options
.password
;
86 return login(uri
, options
);
89 // TODO remove at some point, this is just to clear the previous old version values
90 config
.set('username', '');
91 config
.set('password', '');
93 config
.set('server', server
);
94 config
.set('accessToken', result
.body
.accessToken
);
96 console
.log('Login successful'.green
);
101 if (!config
.accessToken()) return console
.log('Done'.green
);
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
);
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', '');
113 console
.log('Done'.green
);
117 function put(filePath
, otherFilePaths
, options
) {
120 var destination
= '';
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
+= '/';
128 var files
= collectFiles([ filePath
].concat(otherFilePaths
), options
);
130 async
.eachSeries(files
, function (file
, callback
) {
131 var relativeFilePath
;
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
);
141 var destinationPath
= (destination
? '/' + destination : '') + '/' + relativeFilePath
;
142 console
.log('Uploading file %s -> %s', relativeFilePath
.cyan
, destinationPath
.cyan
);
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
);
149 console
.log('Uploaded to ' + config
.server() + destinationPath
);
153 }, function (error
) {
155 console
.log('Failed to put file.', error
.message
.red
);
163 function get(filePath
) {
166 // if no argument provided, fetch root
167 filePath
= filePath
|| '/';
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
);
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
);
180 console
.log('Entries:');
181 files
.entries
.forEach(function (entry
) {
182 console
.log('\t %s', entry
.isDirectory
? entry
.filePath
+ '/' : entry
.filePath
);
186 process
.stdout
.write(body
);
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);
196 // if (result.body && result.body.entries) {
197 // console.log('Files:');
198 // result.body.entries.forEach(function (entry) {
199 // console.log('\t %s', entry);
202 // req.pipe(process.stdout);
207 function del(filePath
, options
) {
210 var query
= safe
.JSON
.parse(safe
.JSON
.stringify(gQuery
));
211 query
.recursive
= options
.recursive
;
212 query
.dryRun
= options
.dryRun
;
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
);
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
);
227 console
.log('Success. Removed %s files.', result
.body
.entries
.length
);