|
|
@@ -2,39 +2,175 @@
|
|
2
|
2
|
'use strict';
|
|
3
|
3
|
|
|
4
|
4
|
const program = require('commander');
|
|
|
5
|
+const prompt = require('prompt-sync')();
|
|
|
6
|
+const utf8 = require('utf8');
|
|
|
7
|
+const crypto = require('crypto');
|
|
|
8
|
+const request = require('request');
|
|
|
9
|
+const stringify = require('json-stable-stringify')
|
|
|
10
|
+
|
|
|
11
|
+class Blih {
|
|
|
12
|
+ constructor(options) {
|
|
|
13
|
+ this.options = options;
|
|
|
14
|
+ this._baseUrl = options.baseurl || 'https://blih.epitech.eu/';
|
|
|
15
|
+ this._user = options.user || this.get_user();
|
|
|
16
|
+ this._token = options.token || this.token_calc();
|
|
|
17
|
+ this._verbose = options.verbose || false;
|
|
|
18
|
+ this._userAgent = options.useragent || 'blih-' + program.version;
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+ sign_data(data) {
|
|
|
22
|
+ const sign = crypto.createHmac('sha512', this._token);
|
|
|
23
|
+ return {
|
|
|
24
|
+ user: this._user,
|
|
|
25
|
+ signature: sign.update(this._user + stringify(data, {space: " "})).digest('hex'),
|
|
|
26
|
+ data: data
|
|
|
27
|
+ };
|
|
|
28
|
+ }
|
|
|
29
|
+
|
|
|
30
|
+ request(options) {
|
|
|
31
|
+ const signed_data = this.sign_data(options.data);
|
|
|
32
|
+ request({
|
|
|
33
|
+ url: options.path,
|
|
|
34
|
+ baseUrl: this._baseUrl,
|
|
|
35
|
+ method: options.method || 'GET',
|
|
|
36
|
+ body: JSON.stringify(signed_data),
|
|
|
37
|
+ headers: {
|
|
|
38
|
+ 'Content-Type': options.contentType || 'application/json',
|
|
|
39
|
+ 'User-Agent': this._userAgent
|
|
|
40
|
+ }
|
|
|
41
|
+ }, function (err, res, body) {
|
|
|
42
|
+ if (err)
|
|
|
43
|
+ return console.error('request failed:', err);
|
|
|
44
|
+ return console.log(body);
|
|
|
45
|
+ });
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ get_user() {
|
|
|
49
|
+ return this.options.user || process.env.LOGNAME || process.env.USER ||
|
|
|
50
|
+ process.env.LNAME || process.env.USERNAME;
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ token_calc() {
|
|
|
54
|
+ return crypto.createHash('sha512').update(prompt('Password: ', {echo: '*'})).digest('hex');
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ repo_create(name, type, description) {
|
|
|
58
|
+ var data = { name, type: type || 'git' };
|
|
|
59
|
+ if (description)
|
|
|
60
|
+ data['description'] = description;
|
|
|
61
|
+ return this.request({ path: '/repositories', method:'POST', data: data });
|
|
|
62
|
+ }
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+class subcommand {
|
|
|
66
|
+ constructor(options) {
|
|
|
67
|
+ this.options = options;
|
|
|
68
|
+ }
|
|
|
69
|
+}
|
|
|
70
|
+
|
|
|
71
|
+class Repository extends subcommand {
|
|
|
72
|
+ create(params) {
|
|
|
73
|
+ if (params.length < 1)
|
|
|
74
|
+ return this.usage()
|
|
|
75
|
+ const blih = new Blih(this.options)
|
|
|
76
|
+ blih.repo_create(params[0]);
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ list() {
|
|
|
80
|
+ console.log(process.argv);
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ info() {
|
|
|
84
|
+ console.log(process.argv);
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ delete() {
|
|
|
88
|
+ console.log(process.argv);
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ setacl() {
|
|
|
92
|
+ console.log(process.argv);
|
|
|
93
|
+ }
|
|
|
94
|
+
|
|
|
95
|
+ getacl() {
|
|
|
96
|
+ console.log(process.argv);
|
|
|
97
|
+ }
|
|
|
98
|
+
|
|
|
99
|
+ usage() {
|
|
|
100
|
+ console.log('Usage: ' + process.argv[1] + ' [options] repository command ...')
|
|
|
101
|
+ console.log()
|
|
|
102
|
+ console.log('Commands :')
|
|
|
103
|
+ console.log('\tcreate repo\t\t\t-- Create a repository named "repo"')
|
|
|
104
|
+ console.log('\tinfo repo\t\t\t-- Get the repository metadata')
|
|
|
105
|
+ console.log('\tgetacl repo\t\t\t-- Get the acls set for the repository')
|
|
|
106
|
+ console.log('\tlist\t\t\t\t-- List the repositories created')
|
|
|
107
|
+ console.log('\tsetacl repo user [acl]\t\t-- Set (or remove) an acl for "user" on "repo"')
|
|
|
108
|
+ console.log('\t\t\t\t\tACL format:')
|
|
|
109
|
+ console.log('\t\t\t\t\tr for read')
|
|
|
110
|
+ console.log('\t\t\t\t\tw for write')
|
|
|
111
|
+ console.log('\t\t\t\t\ta for admin')
|
|
|
112
|
+ }
|
|
|
113
|
+}
|
|
|
114
|
+
|
|
|
115
|
+class sshkey extends subcommand {
|
|
|
116
|
+ list() {
|
|
|
117
|
+
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ upload() {
|
|
|
121
|
+
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
|
124
|
+ delete() {
|
|
|
125
|
+
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ usage() {
|
|
|
129
|
+
|
|
|
130
|
+ }
|
|
|
131
|
+}
|
|
|
132
|
+
|
|
5
|
133
|
|
|
6
|
134
|
program
|
|
7
|
135
|
.version('1.7')
|
|
8
|
|
-.option('-v, --verbose', 'Enable verbose mode', false)
|
|
9
|
|
-.option('-u, --user <user>', 'Set user', undefined)
|
|
10
|
|
-.option('-b, --baseurl <url>', 'Set baseurl', 'https://blih.epitech.eu/')
|
|
11
|
|
-.option('-t, --token <token>', 'Set token', undefined)
|
|
12
|
|
-.option('-U, --useragent <agent>', 'Set useragent', 'blih-' + program.version);
|
|
|
136
|
+.option('-v, --verbose', 'Enable verbose mode')
|
|
|
137
|
+.option('-u, --user <user>', 'Set user')
|
|
|
138
|
+.option('-b, --baseurl <url>', 'Set baseurl')
|
|
|
139
|
+.option('-t, --token <token>', 'Set token')
|
|
|
140
|
+.option('-U, --useragent <agent>', 'Set useragent');
|
|
13
|
141
|
|
|
14
|
142
|
program
|
|
15
|
143
|
.command('repository [params...]')
|
|
16
|
144
|
.description('Manages repositories')
|
|
17
|
145
|
.action(function (params) {
|
|
18
|
|
- const repository = require('commander');
|
|
19
|
|
- console.log(process.argv);
|
|
20
|
|
- repository
|
|
21
|
|
- .version('1.7')
|
|
22
|
|
- .command('list')
|
|
23
|
|
- .description('List the repositories created')
|
|
24
|
|
- .action(function () {
|
|
25
|
|
- console.log('list');
|
|
26
|
|
- });
|
|
27
|
|
- repository.parse(params);
|
|
|
146
|
+ const action = params.shift();
|
|
|
147
|
+ var repo = new Repository(program);
|
|
|
148
|
+ if (typeof repo[action] === 'function')
|
|
|
149
|
+ repo[action](params);
|
|
|
150
|
+ else
|
|
|
151
|
+ repo.usage();
|
|
28
|
152
|
});
|
|
29
|
153
|
|
|
30
|
154
|
program
|
|
31
|
155
|
.command('sshkey')
|
|
32
|
156
|
.description('Manages SSH keys')
|
|
33
|
|
-.action();
|
|
|
157
|
+.action(function (params) {
|
|
|
158
|
+ const action = params.unshift();
|
|
|
159
|
+ var sshkeys= new sshkey(program);
|
|
|
160
|
+ if (typeof sshkeys[action] === 'function')
|
|
|
161
|
+ sshkeys[action]();
|
|
|
162
|
+ else
|
|
|
163
|
+ sshkeys.usage();
|
|
|
164
|
+});
|
|
34
|
165
|
|
|
35
|
166
|
program
|
|
36
|
167
|
.command('whoami')
|
|
37
|
168
|
.description('Ask who you are')
|
|
38
|
|
-.action();
|
|
|
169
|
+.action(function() {
|
|
|
170
|
+ console.log('whoami');
|
|
|
171
|
+});
|
|
39
|
172
|
|
|
40
|
173
|
program.parse(process.argv);
|
|
|
174
|
+
|
|
|
175
|
+if (program.args.length === 0)
|
|
|
176
|
+ program.help();
|