-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·367 lines (323 loc) · 10.6 KB
/
cli.js
File metadata and controls
executable file
·367 lines (323 loc) · 10.6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env node
// Generated by CoffeeScript 1.6.1
/*
This is the CLI interface for using git-server.
*/
var CLI, EventEmitter, GITCLI, GitServer, Table, async, commander, certs, fs, getUserHomeDir, logging, mkdirp, path, repoDB, repoLocation, repoPort, repos, _c, _g,
_this = this,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
CLI = require('cli-listener');
GitServer = require('./lib/host.js');
mkdirp = require('mkdirp');
fs = require('fs');
async = require('async');
path = require('path');
Table = require('cli-table');
commander = require('commander');
commander.version('0.2.2')
.option('-p, --port [value]', 'Port to run Git on', parseInt)
.option('-d, --directory [value]', 'Directory of the repos')
.option('-l, --logging', 'Verbose logging on or off')
.option('-s, --ssl', 'Enable SSL support; requires key and cert options')
.option('-k, --key [value]', 'SSL key path (required for ssl)')
.option('-c, --cert [value]', 'SSL cert path (required for ssl)')
.option('-a, --certificate-authority [value]', 'SSL certificate authority path')
.parse(process.argv);
repoPort = commander.port || 7000;
logging = commander.logging || false;
if (commander.ssl && commander.key && commander.cert) {
var readFileSync = fs.readFileSync,
resolve = require('path').resolve,
readFile = function(path) {
path = resolve(path);
return readFileSync(path, {encoding:'utf8'}).toString();
};
certs = {
key: readFile(commander.key),
cert: readFile(commander.cert)
};
if(commander.certificateAuthority) {
// Properly concatinate the ca chain for node https
var caChain = function caChain(cc) {
var ca = [],
cert = [],
chain = cc.split('\n'),
_i, _len, line;
for (_i = 0, _len = chain.length; _i < _len; _i++) {
line = chain[_i];
if (!(line.length !== 0)) {
continue;
}
cert.push(line);
if (line.match(/-END CERTIFICATE-/)) {
ca.push(cert.join("\n"));
cert = [];
}
}
return ca;
};
certs.ca = caChain(readFile(commander.certificateAuthority));
}
}
getUserHomeDir = function() {
var dir;
if (process.platform === 'win32') {
dir = 'USERPROFILE';
} else {
dir = 'HOME';
}
return process.env[dir];
};
repoLocation = commander.directory || path.join(getUserHomeDir(), './git-server/repos');
if (commander.directory !== void 0) {
repoDB = commander.directory + '.db';
} else {
repoDB = path.join(getUserHomeDir(), './git-server/repos.db');
}
mkdirp.sync(repoLocation);
if (fs.existsSync(repoDB)) {
repos = JSON.parse(fs.readFileSync(repoDB));
} else {
repos = {
repos: [],
users: []
};
}
GITCLI = (function(_super) {
__extends(GITCLI, _super);
/*
Constructor for the CLI interface
@param {Object} gitServer Git-Server object instance
@param {Array} users Users we are managing
*/
function GITCLI(gitServer, users) {
var availableCalls, welcomeMessage,
_this = this;
this.gitServer = gitServer;
this.users = users != null ? users : [];
this.saveConfig = function() {
return GITCLI.prototype.saveConfig.apply(_this, arguments);
};
this.listRepos = function(callback) {
return GITCLI.prototype.listRepos.apply(_this, arguments);
};
this.listUsers = function(callback) {
return GITCLI.prototype.listUsers.apply(_this, arguments);
};
this.columnPercentage = function(percentage) {
return GITCLI.prototype.columnPercentage.apply(_this, arguments);
};
this.getUser = function(username) {
return GITCLI.prototype.getUser.apply(_this, arguments);
};
this.addUserToRepo = function(callback) {
return GITCLI.prototype.addUserToRepo.apply(_this, arguments);
};
this.createUser = function(callback) {
return GITCLI.prototype.createUser.apply(_this, arguments);
};
this.createRepo = function(callback) {
return GITCLI.prototype.createRepo.apply(_this, arguments);
};
availableCalls = {
'create repo': this.createRepo,
'create user': this.createUser,
'list repos': this.listRepos,
'list users': this.listUsers,
'add user to repo': this.addUserToRepo
};
welcomeMessage = "Welcome to Git Server - Powered by NodeJS\n - Repo Location: " + repoLocation + "\n - Listening Port: " + repoPort + "\n - Repo Count: " + this.gitServer.repos.length + "\n - User Count: " + this.users.length;
this.cli = new CLI('git-server', welcomeMessage, availableCalls);
this.on('changedData', this.saveConfig);
setTimeout(this.cli.resetInput, 100);
}
GITCLI.prototype.createRepo = function(callback) {
var _this = this;
return this.cli.ask({
name: 'Repo Name: ',
anonRead: 'Anonymous Access? [y,N] :: '
}, function(err, results) {
var anon, name;
if (err) {
throw err;
}
name = results.name.toLowerCase();
anon = results.anonRead.toLowerCase();
if (anon === 'y') {
anon = true;
} else {
anon = false;
}
_this.gitServer.createRepo({
name: name,
anonRead: anon,
users: []
});
_this.emit('changedData');
return callback();
});
};
GITCLI.prototype.createUser = function(callback) {
var _this = this;
return this.cli.ask({
username: 'Users username: ',
password: 'Users password: '
}, function(err, answers) {
var user, username;
if (err) {
throw err;
}
username = answers.username.toLowerCase();
user = _this.getUser(username);
if (user !== false) {
console.log('This username already exists');
return callback();
} else {
user = {
username: username,
password: answers.password
};
_this.users.push(user);
_this.emit('changedData');
return callback();
}
});
};
GITCLI.prototype.addUserToRepo = function(callback) {
var _this = this;
return this.cli.ask({
repoName: 'Repo Name: ',
username: 'Users username: ',
permissions: 'Permissions (comma seperated: R,W ): '
}, function(err, answers) {
var permissions, repo, repoName, user, username;
repoName = answers.repoName.toLowerCase();
username = answers.username.toLowerCase();
repo = _this.gitServer.getRepo(repoName + '.git');
user = _this.getUser(username);
permissions = answers.permissions.split(',');
if (permissions.length === 0) {
permissions = ['R'];
}
if (repo === false) {
return console.log('Repo doesnt exist.');
} else if (user === false) {
return console.log('User doesnt exist.');
} else {
repo.users.push({
user: user,
permissions: permissions
});
_this.emit('changedData');
return callback();
}
});
};
/*
Loop through and find this user
@param {String} username Username of the user we are looking for
*/
GITCLI.prototype.getUser = function(username) {
var user, _i, _len, _ref;
_ref = this.users;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
user = _ref[_i];
if (user.username === username) {
return user;
}
}
return false;
};
/*
Get the number of columns needed from a % width
@param {Int} percentage Percentage of the console width
*/
GITCLI.prototype.columnPercentage = function(percentage) {
return Math.floor(process.stdout.columns * (percentage / 100));
};
GITCLI.prototype.listUsers = function(callback) {
var repo, repoUser, table, user, users, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _ref3;
users = this.users;
_ref = this.users;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
user = _ref[_i];
user.repos = [];
_ref1 = this.gitServer.repos;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
repo = _ref1[_j];
_ref2 = repo.users;
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
repoUser = _ref2[_k];
if (repoUser.user.username === user.username) {
user.repos.push({
name: repo.name,
permissions: repoUser.permissions
});
}
}
}
}
table = new Table({
head: ['Username', 'Password', 'Repos'],
colWidths: [this.columnPercentage(40) - 1, this.columnPercentage(20) - 1, this.columnPercentage(40) - 1]
});
_ref3 = this.users;
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
user = _ref3[_l];
repos = (function() {
var _len4, _m, _ref4, _results;
_ref4 = user.repos;
_results = [];
for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) {
repo = _ref4[_m];
_results.push("" + repo.name + " (" + (repo.permissions.join(',')) + ")");
}
return _results;
})();
table.push([user.username, user.password, repos.join('\n')]);
}
console.log(table.toString());
return callback();
};
GITCLI.prototype.listRepos = function(callback) {
var repo, table, user, users, _i, _len;
repos = this.gitServer.repos;
table = new Table({
head: ['Repo Name', 'Anonymous Reads', 'Users'],
colWidths: [this.columnPercentage(40) - 1, this.columnPercentage(20) - 1, this.columnPercentage(40) - 1]
});
for (_i = 0, _len = repos.length; _i < _len; _i++) {
repo = repos[_i];
users = (function() {
var _j, _len1, _ref, _results;
_ref = repo.users;
_results = [];
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
user = _ref[_j];
_results.push("" + user.user.username + " (" + (user.permissions.join(',')) + ")");
}
return _results;
})();
table.push([repo.name, repo.anonRead, users.join('\n')]);
}
console.log(table.toString());
return callback();
};
GITCLI.prototype.saveConfig = function() {
var config;
config = JSON.stringify({
repos: this.gitServer.repos,
users: this.users
});
return fs.writeFileSync(repoDB, config);
};
return GITCLI;
})(EventEmitter);
if (!certs) {
_g = new GitServer(repos.repos, logging, repoLocation, repoPort);
} else {
_g = new GitServer(repos.repos, logging, repoLocation, repoPort, certs);
}
_c = new GITCLI(_g, repos.users);