aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli/actions.js
blob: 6fb98937e73a57f8e26fd5400dfdc092fce04799 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';

exports.login = login;
exports.put = put;
exports.get = get;
exports.del = del;

var superagent = require('superagent'),
    config = require('./config.js'),
    readlineSync = require('readline-sync'),
    async = require('async'),
    fs = require('fs'),
    request = require('request'),
    url = require('url'),
    path = require('path');

require('colors');

var API = '/api/files/';

var gQuery = {};

function checkConfig() {
    if (!config.server() || !config.username() || !config.password()) {
        console.log('You have run "login" first');
        process.exit(1);
    }

    gQuery = { username: config.username(), password: config.password() };

    console.error('Using server %s', config.server().yellow);
}

function collectFiles(filesOrFolders) {
    var tmp = [];

    filesOrFolders.forEach(function (filePath) {
        var stat = fs.statSync(filePath);

        if (stat.isFile()) {
            tmp.push(filePath);
        } else if (stat.isDirectory()) {
            var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
            tmp = tmp.concat(collectFiles(files));
        } else {
            console.log('Skipping %s', filePath.cyan);
        }
    });

    return tmp;
}

function login(uri) {
    var tmp = url.parse(uri);
    if (!tmp.host) tmp = url.parse('https://' + uri);

    var server = tmp.protocol + '//' + tmp.host;

    console.log('Using server', server.bold);

    var username = readlineSync.question('Username: ', { hideEchoBack: false });
    var password = readlineSync.question('Password: ', { hideEchoBack: true });

    superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) {
        if (error && error.code === 'ENOTFOUND') {
            console.log('No such server %s'.red, server.bold);
            process.exit(1);
        }
        if (error && error.code) {
            console.log('Failed to connect to server %s'.red, server.bold, error.code);
            process.exit(1);
        }
        if (result.status === 401) {
            console.log('Login failed.'.red);
            process.exit(1);
        }

        config.set('server', server);
        config.set('username', username);

        // TODO this is clearly bad and needs fixing
        config.set('password', password);

        gQuery = { username: username, password: password };

        console.log('Ok'.green);
    });
}

function put(filePath, otherFilePaths, options) {
    checkConfig();

    var files = collectFiles([ filePath ].concat(otherFilePaths));

    async.eachSeries(files, function (file, callback) {
        var relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
        var destinationPath = (options.destination ? '/' + options.destination : '') + '/' + relativeFilePath;
        console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan);

        superagent.put(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
            if (error) return callback(error);
            if (result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode));

            console.log('Uploaded to ' + config.server() + destinationPath);
        });
    }, function (error) {
        if (error) {
            console.log('Failed to put file.', error);
            process.exit(1);
        }

        console.log('Done');
    });
}

function get(filePath) {
    checkConfig();

    // if no argument provided, fetch root
    filePath = filePath || '/';

    request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) {
        if (error) return console.error(error);
        if (result.statusCode === 401) return console.log('Login failed');
        if (result.statusCode === 404) return console.log('No such file or directory');

        // 222 indicates directory listing
        if (result.statusCode === 222) {
            console.log('Files:');
            JSON.parse(body).entries.forEach(function (entry) {
                console.log('\t %s', entry);
            });
        } else {
            console.log(body);
        }
    });
    // var req = superagent.get(config.server() + API + filePath);
    // req.query(gQuery);
    // req.end(function (error, result) {
    //     if (error && error.status === 401) return console.log('Login failed');
    //     if (error && error.status === 404) return console.log('No such file or directory');
    //     if (error) return console.log('Failed', result ? result.body : error);

    //     if (result.body && result.body.entries) {
    //         console.log('Files:');
    //         result.body.entries.forEach(function (entry) {
    //             console.log('\t %s', entry);
    //         });
    //     } else {
    //         req.pipe(process.stdout);
    //     }
    // });
}

function del(filePath) {
    checkConfig();

    var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
    superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) {
        if (error && error.status === 401) return console.log('Login failed');
        if (error && error.status === 404) return console.log('No such file or directory');
        if (error) return console.log('Failed', result ? result.body : error);
        console.log('Success', result.body);
    });
}