|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const kleur = require("kleur"); |
| 4 | +const _ = require("lodash"); |
| 5 | +const { table, getBorderCharacters } = require("table"); |
| 6 | +const { match } = require("../utils"); |
| 7 | + |
| 8 | +module.exports = function(vorpal, broker) { |
| 9 | + // Register cache commands |
| 10 | + vorpal |
| 11 | + .removeIfExist("cache keys") |
| 12 | + .command("cache keys", "List keys of cache entries") |
| 13 | + .option("-f, --filter <match>", "filter keys") |
| 14 | + .action((args, done) => { |
| 15 | + if (!broker.cacher) { |
| 16 | + console.log(kleur.red().bold("No cacher.")); |
| 17 | + return done(); |
| 18 | + } |
| 19 | + |
| 20 | + if (!_.isFunction(broker.cacher.getCacheKeys)) { |
| 21 | + console.log(kleur.yellow().bold("Cacher is not compatible. Missing 'getCacheKeys' method.")); |
| 22 | + return done(); |
| 23 | + } |
| 24 | + |
| 25 | + broker.cacher.getCacheKeys().then((entries) => { |
| 26 | + const data = [ |
| 27 | + [ |
| 28 | + kleur.bold("Key"), |
| 29 | + //kleur.bold("Expires at"), |
| 30 | + ] |
| 31 | + ]; |
| 32 | + |
| 33 | + entries.sort((a, b) => a.key.localeCompare(b.key)); |
| 34 | + |
| 35 | + //let hLines = []; |
| 36 | + |
| 37 | + entries.forEach(item => { |
| 38 | + |
| 39 | + if (args.options.filter && !match(item.key, args.options.filter)) |
| 40 | + return; |
| 41 | + |
| 42 | + data.push([ |
| 43 | + item.key, |
| 44 | + //item.expiresAt |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + const tableConf = { |
| 49 | + border: _.mapValues(getBorderCharacters("honeywell"), char => kleur.gray(char)), |
| 50 | + columns: {}, |
| 51 | + //drawHorizontalLine: (index, count) => index == 0 || index == 1 || index == count || hLines.indexOf(index) !== -1 |
| 52 | + }; |
| 53 | + |
| 54 | + console.log(table(data, tableConf)); |
| 55 | + |
| 56 | + done(); |
| 57 | + }).catch(err => { |
| 58 | + console.error(err); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + |
| 64 | + // Clear cache |
| 65 | + vorpal |
| 66 | + .removeIfExist("cache clear") |
| 67 | + .command("cache clear [pattern]", "Clear cache entries") |
| 68 | + .action((args, done) => { |
| 69 | + if (broker.cacher) { |
| 70 | + broker.cacher.clean(args.pattern).then(() => { |
| 71 | + console.log(kleur.yellow().bold(args.pattern ? "Cacher cleared entries by pattern." : "Cacher cleared all entries.")); |
| 72 | + done(); |
| 73 | + }).catch(err => { |
| 74 | + console.error(err); |
| 75 | + done(); |
| 76 | + }); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + console.log(kleur.red().bold("No cacher.")); |
| 81 | + done(); |
| 82 | + }); |
| 83 | +}; |
0 commit comments