Skip to content

Commit 6a0cb42

Browse files
Fix readme and code formatting
1 parent 9a5065c commit 6a0cb42

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,14 @@ This server exposes the following REST API's:
188188

189189
- **[WIP] POST `/download`**
190190

191-
Allows you to download .tar archive with rules from HTTP endpoint. Archive will be downloaded,extracted and removed.
191+
Allows you to download a .tar archive with rules from a given HTTP endpoint. The archive will be downloaded, extracted and removed.
192192
Please note, body should contain URL pointing to tar archive, with tar extension.
193193

194194
Usage example:
195195

196-
```curl -X POST localhost:3030/download -d "url=https://artifactory.com:443/artifactory/raw/rules/rules.tar"```
197-
196+
```bash
197+
curl -X POST localhost:3030/download -d "url=https://artifactory.com:443/artifactory/raw/rules/rules.tar"
198+
```
198199

199200
## Contributing
200201
Want to contribute to this project? Great! Please read our [contributing guidelines](CONTRIBUTING.md) before submitting an issue or a pull request.

src/common/errors/rule_request_errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export class RulesRootFolderNotCreatableError extends RequestError {
4444
export class URLNotSentError extends RequestError {
4545
constructor() {
4646
super('URLNotSentError', 'URL is missing in the body', 400);
47-
}
47+
}
4848
}
4949
export class URLNotPointingTar extends RequestError {
5050
constructor() {
5151
super('URLNotPointingTar', 'URL is not pointing to .tar file', 400);
5252
}
53-
}
53+
}

src/controllers/rules/index.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {join as joinPath, normalize as normalizePath, extname as pathExtension} from 'path';
1+
import { join as joinPath, normalize as normalizePath, extname as pathExtension } from 'path';
22
import mkdirp from 'mkdirp';
33
import tar from 'tar';
44
import fs from 'fs-extra';
@@ -7,8 +7,10 @@ import FileSystem from '../../common/file_system';
77
import config from '../../common/config';
88
import Logger from '../../common/logger';
99
import path from 'path';
10-
import {RuleNotFoundError, RuleNotReadableError, RuleNotWritableError,
11-
RulesFolderNotFoundError, RulesRootFolderNotCreatableError} from '../../common/errors/rule_request_errors';
10+
import {
11+
RuleNotFoundError, RuleNotReadableError, RuleNotWritableError,
12+
RulesFolderNotFoundError, RulesRootFolderNotCreatableError
13+
} from '../../common/errors/rule_request_errors';
1214

1315
let logger = new Logger('RulesController');
1416

@@ -21,26 +23,26 @@ export default class RulesController {
2123
getRules(path) {
2224
const self = this;
2325
const fullPath = joinPath(self.rulesFolder, path);
24-
return new Promise(function (resolve, reject) {
26+
return new Promise(function(resolve, reject) {
2527
self._fileSystemController.readDirectory(fullPath)
26-
.then(function (directoryIndex) {
28+
.then(function(directoryIndex) {
2729

28-
directoryIndex.rules = directoryIndex.files.filter(function (fileName) {
30+
directoryIndex.rules = directoryIndex.files.filter(function(fileName) {
2931
return pathExtension(fileName).toLowerCase() === '.yaml';
30-
}).map(function (fileName) {
32+
}).map(function(fileName) {
3133
return fileName.slice(0, -5);
3234
});
3335

3436
delete directoryIndex.files;
3537
resolve(directoryIndex);
3638
})
37-
.catch(function (error) {
39+
.catch(function(error) {
3840

3941
// Check if the requested folder is the rules root folder
4042
if (normalizePath(self.rulesFolder) === fullPath) {
4143

4244
// Try to create the root folder
43-
mkdirp(fullPath, function (error) {
45+
mkdirp(fullPath, function(error) {
4446
if (error) {
4547
reject(new RulesRootFolderNotCreatableError());
4648
logger.warn(`The rules root folder (${fullPath}) couldn't be found nor could it be created by the file system.`);
@@ -58,29 +60,29 @@ export default class RulesController {
5860

5961
rule(id) {
6062
const self = this;
61-
return new Promise(function (resolve, reject) {
63+
return new Promise(function(resolve, reject) {
6264
self._findRule(id)
63-
.then(function (access) {
65+
.then(function(access) {
6466
console.log('rule resolved');
6567
resolve({
66-
get: function () {
68+
get: function() {
6769
if (access.read) {
6870
return self._getRule(id);
6971
}
7072
return self._getErrorPromise(new RuleNotReadableError(id));
7173
},
72-
edit: function (body) {
74+
edit: function(body) {
7375
if (access.write) {
7476
return self._editRule(id, body);
7577
}
7678
return self._getErrorPromise(new RuleNotWritableError(id));
7779
},
78-
delete: function () {
80+
delete: function() {
7981
return self._deleteRule(id);
8082
}
8183
});
8284
})
83-
.catch(function () {
85+
.catch(function() {
8486
console.log('catched');
8587
reject(new RuleNotFoundError(id));
8688
});
@@ -98,9 +100,9 @@ export default class RulesController {
98100
_findRule(id) {
99101
let fileName = id + '.yaml';
100102
const self = this;
101-
return new Promise(function (resolve, reject) {
103+
return new Promise(function(resolve, reject) {
102104
self._fileSystemController.fileExists(joinPath(self.rulesFolder, fileName))
103-
.then(function (exists) {
105+
.then(function(exists) {
104106
if (!exists) {
105107
reject();
106108
} else {
@@ -112,7 +114,7 @@ export default class RulesController {
112114
});
113115
}
114116
})
115-
.catch(function (error) {
117+
.catch(function(error) {
116118
reject(error);
117119
});
118120
});
@@ -133,21 +135,21 @@ export default class RulesController {
133135
return this._fileSystemController.deleteFile(path);
134136
}
135137

136-
_downloadRules(URL){
138+
_downloadRules(URL) {
137139
const options = {
138140
uri: URL,
139141
strictSSL: false
140142
};
141143
const filename = path.basename(URL);
142-
144+
143145
return rq.get(options)
144146
.then(buffer => fs.outputFile(filename, buffer)
145-
.then(() => this._untarFile(this.rulesFolder,filename))
147+
.then(() => this._untarFile(this.rulesFolder, filename))
146148
.then(() => fs.remove(filename)));
147149
}
148150

149151
_getErrorPromise(error) {
150-
return new Promise(function (resolve, reject) {
152+
return new Promise(function(resolve, reject) {
151153
reject(error);
152154
});
153155
}
@@ -162,12 +164,12 @@ export default class RulesController {
162164
}
163165
}
164166

165-
_untarFile(path_to_extract,archive){
167+
_untarFile(path_to_extract, archive) {
166168
return tar.extract(
167169
{
168170
cwd: path_to_extract,
169171
file: archive
170172
}
171173
);
172174
}
173-
}
175+
}

src/handlers/rules/id/download.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import RouteLogger from '../../../routes/route_logger';
2-
import {sendRequestError} from '../../../common/errors/utils';
3-
import {URLNotSentError,URLNotPointingTar} from '../../../common/errors/rule_request_errors';
2+
import { sendRequestError } from '../../../common/errors/utils';
3+
import { URLNotSentError, URLNotPointingTar } from '../../../common/errors/rule_request_errors';
44
import path from 'path';
55

66
let logger = new RouteLogger('/download');
@@ -12,21 +12,21 @@ export default function downloadRulesHandler(request, response) {
1212
*/
1313
let server = request.app.get('server');
1414
let body = request.body;
15-
1615

17-
function _test_body(body){
18-
if (!body||!body.url) {
16+
17+
function _test_body(body) {
18+
if (!body || !body.url) {
1919
return new URLNotSentError();
2020
}
2121
let filename = path.basename(body.url);
22-
if (!filename.endsWith('.tar')){
22+
if (!filename.endsWith('.tar')) {
2323
return new URLNotPointingTar();
2424
}
2525
return body;
2626
}
27-
28-
body=_test_body(body);
29-
if(body.error){
27+
28+
body = _test_body(body);
29+
if (body.error) {
3030
logger.sendFailed(body.error);
3131
sendRequestError(response, body.error);
3232
return;
@@ -46,5 +46,5 @@ export default function downloadRulesHandler(request, response) {
4646
logger.sendFailed(error);
4747
sendRequestError(response, error);
4848
});
49-
49+
5050
}

0 commit comments

Comments
 (0)