]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blame - cli/actions.js
Use proper data directory
[perso/Immae/Projets/Nodejs/Surfer.git] / cli / actions.js
CommitLineData
8c3ae071
JZ
1'use strict';
2
3exports.login = login;
4exports.put = put;
5exports.get = get;
6exports.del = del;
7
8var superagent = require('superagent'),
a90a633f
JZ
9 config = require('./config.js'),
10 readlineSync = require('readline-sync'),
8c3ae071
JZ
11 async = require('async'),
12 fs = require('fs'),
13 path = require('path');
14
15require('colors');
16
17var API = '/api/files/';
18
a90a633f
JZ
19var gQuery = {};
20
8c3ae071 21function checkConfig() {
a90a633f 22 if (!config.server() || !config.username() || !config.password()) {
8c3ae071
JZ
23 console.log('You have run "login" first');
24 process.exit(1);
25 }
c10e69bf 26
a90a633f
JZ
27 gQuery = { username: config.username(), password: config.password() };
28
c10e69bf 29 console.log('Using server %s', config.server().yellow);
8c3ae071
JZ
30}
31
32function collectFiles(filesOrFolders) {
33 var tmp = [];
34
35 filesOrFolders.forEach(function (filePath) {
36 var stat = fs.statSync(filePath);
37
38 if (stat.isFile()) {
39 tmp.push(filePath);
40 } else if (stat.isDirectory()) {
41 var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
42 tmp = tmp.concat(collectFiles(files));
43 } else {
44 console.log('Skipping %s', filePath.cyan);
45 }
46 });
47
48 return tmp;
49}
50
a90a633f
JZ
51function checkResponse(error, result) {
52 if (error && error.status === 401) {
53 console.log('Login failed');
54 process.exit(1);
55 } else if (error) {
56 console.log('Error', result ? result.text : error);
57 process.exit(1);
58 }
59}
60
8c3ae071 61function login(server) {
591ad40c
JZ
62 if (server[server.length-1] === '/') server = server.slice(0, -1);
63
8c3ae071 64 console.log('Using server', server);
a90a633f
JZ
65
66 var username = readlineSync.question('Username: ', { hideEchoBack: false });
67 var password = readlineSync.question('Password: ', { hideEchoBack: true });
68
69 superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) {
70 console.log(result.status);
71
72 if (result.status === 401) {
73 console.log('Login failed.');
74 process.exit(1);
75 }
76
77 config.set('server', server);
78 config.set('username', username);
79
80 // TODO this is clearly bad and needs fixing
81 config.set('password', password);
82
83 gQuery = { username: username, password: password };
84 });
8c3ae071
JZ
85}
86
eb83e4da 87function put(filePath, otherFilePaths, options) {
8c3ae071
JZ
88 checkConfig();
89
90 var files = collectFiles([ filePath ].concat(otherFilePaths));
91
92 async.eachSeries(files, function (file, callback) {
93 var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
94
eb83e4da 95 console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan);
8c3ae071 96
a90a633f 97 superagent.put(config.server() + API + relativeFilePath).query(gQuery).attach('file', file).end(callback);
8c3ae071
JZ
98 }, function (error) {
99 if (error) {
100 console.log('Failed to put file.', error);
101 process.exit(1);
102 }
103
104 console.log('Done');
105 });
106}
107
108function get(filePath) {
109 checkConfig();
110
a90a633f
JZ
111 superagent.get(config.server() + API + filePath).query(gQuery).end(function (error, result) {
112 if (error && error.status === 401) return console.log('Login failed');
113 if (error && error.status === 404) return console.log('No such file or directory');
8c3ae071
JZ
114 if (error) return console.log('Failed', result ? result.body : error);
115
116 if (result.body && result.body.entries) {
117 console.log('Files:');
118 result.body.entries.forEach(function (entry) {
119 console.log('\t %s', entry);
120 });
121 } else {
122 console.log(result.text);
123 }
124 });
125}
126
127function del(filePath) {
128 checkConfig();
129
130 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
a90a633f
JZ
131 superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) {
132 if (error && error.status === 401) return console.log('Login failed');
133 if (error && error.status === 404) return console.log('No such file or directory');
8c3ae071
JZ
134 if (error) return console.log('Failed', result ? result.body : error);
135 console.log('Success', result.body);
136 });
137}