Skip to content

Commit a73dbb8

Browse files
ziv-codefreshitai-codefresh
authored andcommitted
Add support for watch option for get operations (#41)
1 parent 36ca860 commit a73dbb8

File tree

6 files changed

+45
-11
lines changed

6 files changed

+45
-11
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Command = require('../../Command');
22
const { crudFilenameOption } = require('../../helpers/general');
3+
const DEFAULTS = require('../../defaults');
34

45

56
const get = new Command({
@@ -24,8 +25,21 @@ const get = new Command({
2425
describe: 'Output format',
2526
alias: 'o',
2627
choices: ['json', 'yaml', 'wide', 'name'],
28+
})
29+
.option('watch', {
30+
describe: 'Watching updates to a particular resource',
31+
alias: 'w',
32+
type: Boolean,
33+
default: false,
34+
})
35+
.option('watch_interval', {
36+
describe: 'Interval time at watching mode (in seconds)',
37+
})
38+
.coerce('watch_interval', (watchInterval) => {
39+
return Math.max(watchInterval * 1000 , DEFAULTS.WATCH_INTERVAL);
2740
});
2841

42+
2943
crudFilenameOption(yargs);
3044

3145
return yargs

lib/interface/cli/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const DEFAULTS = {
55
GET_LIMIT_RESULTS: 25,
66
GET_PAGINATED_PAGE: 1,
77
CODEFRESH_REGISTRIES: ['r.cfcr.io'],
8+
WATCH_INTERVAL: 3000,
89
};
910

1011
module.exports = DEFAULTS;

lib/interface/cli/helpers/general.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const printError = (error) => {
1717
const wrapHandler = (handler) => {
1818
return async (argv) => {
1919
try {
20-
await handler(argv);
20+
argv.watch ? setInterval(async () => { await handler(argv)}, argv.watch_interval) : await handler(argv);
2121
} catch (err) {
2222
printError(err);
2323
process.exit(1);

lib/interface/cli/helpers/get.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const _ = require('lodash');
33
const columnify = require('columnify');
44
const yaml = require('js-yaml');
5+
const logUpdate = require('log-update');
56

67
//i tried that this function will be dynamic (with the keys). it is also possible to add an array with all the fields if you think it better
78
const _printArrayTable = (data) => {
@@ -19,7 +20,7 @@ const _printArrayTable = (data) => {
1920
res.push(obj);
2021
});
2122
const columns = columnify(res);
22-
console.log(columns);
23+
logUpdate(columns);
2324
};
2425

2526
const _printSingleTable = (info) => {
@@ -31,20 +32,21 @@ const _printSingleTable = (info) => {
3132
});
3233
res.push(obj);
3334
const columns = columnify(res);
34-
console.log(columns);
35+
logUpdate(columns);
3536
};
3637

3738

3839
const specifyOutputForSingle = (type, enitity) => {
40+
logUpdate.clear();
3941
switch (type) {
4042
case 'json':
41-
console.log(enitity.toJson());
43+
logUpdate(enitity.toJson());
4244
break;
4345
case 'yaml':
44-
console.log(enitity.toYaml());
46+
logUpdate(enitity.toYaml());
4547
break;
4648
case 'name':
47-
console.log(enitity.toName());
49+
logUpdate(enitity.toName());
4850
break;
4951
case 'wide':
5052
_printSingleTable(enitity.toWide());
@@ -56,13 +58,14 @@ const specifyOutputForSingle = (type, enitity) => {
5658

5759

5860
const specifyOutputForArray = (type, enitities) => {
61+
logUpdate.clear();
5962
switch (type) {
6063
case 'json':
6164
const jsonArray = [];
6265
_.forEach(enitities, (entity) => {
6366
jsonArray.push(entity.info);
6467
});
65-
console.log(JSON.stringify(jsonArray, null, '\t'));
68+
logUpdate(JSON.stringify(jsonArray, null, '\t'));
6669
break;
6770
case 'yaml':
6871
let yamlArray = {
@@ -71,11 +74,11 @@ const specifyOutputForArray = (type, enitities) => {
7174
_.forEach(enitities, (entity) => {
7275
yamlArray.items.push(entity.info);
7376
});
74-
console.log(yaml.safeDump(yamlArray));
77+
logUpdate(yaml.safeDump(yamlArray));
7578
break;
7679
case 'name':
7780
_.forEach(enitities, (entity) => {
78-
console.log(entity.toName());
81+
logUpdate(entity.toName());
7982
});
8083
break;
8184
case 'wide':

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,
@@ -29,6 +29,7 @@
2929
"jsonwebtoken": "^8.1.0",
3030
"kefir": "^3.8.0",
3131
"lodash": "^4.17.4",
32+
"log-update": "^2.3.0",
3233
"mkdirp": "^0.5.1",
3334
"moment": "^2.19.4",
3435
"prettyjson": "^1.2.1",

yarn.lock

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ cli-cursor@^1.0.1:
625625
dependencies:
626626
restore-cursor "^1.0.1"
627627

628-
cli-cursor@^2.1.0:
628+
cli-cursor@^2.0.0, cli-cursor@^2.1.0:
629629
version "2.1.0"
630630
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-2.1.0.tgz#b35dac376479facc3e94747d41d0d0f5238ffcb5"
631631
dependencies:
@@ -2427,6 +2427,14 @@ lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0:
24272427
version "4.17.4"
24282428
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
24292429

2430+
log-update@^2.3.0:
2431+
version "2.3.0"
2432+
resolved "https://registry.yarnpkg.com/log-update/-/log-update-2.3.0.tgz#88328fd7d1ce7938b29283746f0b1bc126b24708"
2433+
dependencies:
2434+
ansi-escapes "^3.0.0"
2435+
cli-cursor "^2.0.0"
2436+
wrap-ansi "^3.0.1"
2437+
24302438
longest@^1.0.1:
24312439
version "1.0.1"
24322440
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
@@ -3871,6 +3879,13 @@ wrap-ansi@^2.0.0:
38713879
string-width "^1.0.1"
38723880
strip-ansi "^3.0.1"
38733881

3882+
wrap-ansi@^3.0.1:
3883+
version "3.0.1"
3884+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-3.0.1.tgz#288a04d87eda5c286e060dfe8f135ce8d007f8ba"
3885+
dependencies:
3886+
string-width "^2.1.1"
3887+
strip-ansi "^4.0.0"
3888+
38743889
wrappy@1:
38753890
version "1.0.2"
38763891
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

0 commit comments

Comments
 (0)