Skip to content

Commit 029a248

Browse files
Fix watch flag (#48)
1 parent a73dbb8 commit 029a248

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

lib/interface/cli/commands/root/get.cmd.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ const get = new Command({
3434
})
3535
.option('watch_interval', {
3636
describe: 'Interval time at watching mode (in seconds)',
37+
default: DEFAULTS.WATCH_INTERVAL_MS / 1000,
3738
})
3839
.coerce('watch_interval', (watchInterval) => {
39-
return Math.max(watchInterval * 1000 , DEFAULTS.WATCH_INTERVAL);
40+
return Math.max(watchInterval * 1000, DEFAULTS.WATCH_INTERVAL_MS);
4041
});
4142

4243

lib/interface/cli/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const DEFAULTS = {
55
GET_LIMIT_RESULTS: 25,
66
GET_PAGINATED_PAGE: 1,
77
CODEFRESH_REGISTRIES: ['r.cfcr.io'],
8-
WATCH_INTERVAL: 3000,
8+
WATCH_INTERVAL_MS: 3000,
9+
MAX_CONSECUTIVE_ERRORS_LIMIT: 10,
910
};
1011

1112
module.exports = DEFAULTS;

lib/interface/cli/helpers/general.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const _ = require('lodash');
2-
const fs = require('fs');
3-
const yaml = require('js-yaml');
1+
const Promise = require('bluebird');
2+
const _ = require('lodash');
3+
const fs = require('fs');
4+
const yaml = require('js-yaml');
45
const defaults = require('../defaults');
5-
const CFError = require('cf-errors');
6-
const path = require('path');
6+
const CFError = require('cf-errors');
7+
const path = require('path');
78

89

910
const printError = (error) => {
@@ -17,7 +18,27 @@ const printError = (error) => {
1718
const wrapHandler = (handler) => {
1819
return async (argv) => {
1920
try {
20-
argv.watch ? setInterval(async () => { await handler(argv)}, argv.watch_interval) : await handler(argv);
21+
if (!argv.watch) {
22+
return handler(argv);
23+
}
24+
25+
let consecutiveErrors = 0;
26+
let initial = true;
27+
while (true) {
28+
try {
29+
await handler(argv, initial);
30+
consecutiveErrors = 0;
31+
initial = false;
32+
}
33+
catch (err) {
34+
consecutiveErrors++;
35+
if (initial || consecutiveErrors > defaults.MAX_CONSECUTIVE_ERRORS_LIMIT) {
36+
throw err;
37+
}
38+
}
39+
await Promise.delay(argv.watch_interval);
40+
}
41+
2142
} catch (err) {
2243
printError(err);
2344
process.exit(1);
@@ -32,12 +53,12 @@ const wrapHandler = (handler) => {
3253
*/
3354
const prepareKeyValueFromCLIEnvOption = (environmentVariables) => {
3455
let variables = {};
35-
let envArray = [];
56+
let envArray = [];
3657
environmentVariables.constructor !== Array ? envArray.push(environmentVariables) : envArray = environmentVariables;
3758
envArray.forEach(function (vars) {
38-
let fields = vars.split("=");
39-
let key = fields[0];
40-
let val = fields[1];
59+
let fields = vars.split('=');
60+
let key = fields[0];
61+
let val = fields[1];
4162
if (_.isUndefined(key) || _.isUndefined(val)) {
4263
throw new CFError('Invalid environment variable format. please enter [key]=[value]');
4364
}
@@ -48,12 +69,12 @@ const prepareKeyValueFromCLIEnvOption = (environmentVariables) => {
4869

4970
const prepareKeyValueCompostionFromCLIEnvOption = (environmentVariables) => {
5071
const variables = [];
51-
let envArray = [];
72+
let envArray = [];
5273
environmentVariables.constructor !== Array ? envArray.push(environmentVariables) : envArray = environmentVariables;
5374
envArray.forEach(function (vars) {
54-
let fields = vars.split("=");
55-
let key = fields[0];
56-
let val = fields[1];
75+
let fields = vars.split('=');
76+
let key = fields[0];
77+
let val = fields[1];
5778
if (_.isUndefined(key) || _.isUndefined(val)) {
5879
throw new CFError('Invalid environment variable format. please enter [key]=[value]');
5980
}

0 commit comments

Comments
 (0)