Skip to content

Commit

Permalink
added, ability to remove ignoreFields through CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
SkeloGH committed Jan 5, 2020
1 parent 2258108 commit 54f129d
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 17 deletions.
31 changes: 16 additions & 15 deletions bin/commands/remove.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
const Debug = require('debug');
const { client } = require('./remove/');
const { client, ignore } = require('./remove/');

const logging = Debug('Weaver:bin:commands:remove');

module.exports = {
name: 'remove',
description: 'Interactive removal of clients, queries or ignores',
description: 'Removal of clients, queries or ignores',
setup: (yargs) => {
const cmd = yargs.command(
client.commandName,
client.commandDesc,
client.commandSpec,
client.commandHandler,
)
.command('query', 'Interactive removal of one or more queries',
const cmd = yargs
.command(
client.commandName,
client.commandDesc,
client.commandSpec,
client.commandHandler,
)
.command('query', 'Removal of one or more queries',
(_yargs) => _yargs,
(params) => {
logging('query params', params);
return params;
})
.command('ignore', 'Interactive removal of one or more ignores',
(_yargs) => _yargs,
(params) => {
logging('ignore params', params);
return params;
});
.command(
ignore.commandName,
ignore.commandDesc,
ignore.commandSpec,
ignore.commandHandler,
);
return cmd;
},
parse: (_argv) => {
Expand Down
79 changes: 79 additions & 0 deletions bin/commands/remove/ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const ldColl = require('lodash/collection');
const ldObject = require('lodash/object');
const ldArray = require('lodash/array');
const shell = require('shelljs');
const logging = require('debug')('Weaver:bin:commands:add:ignore');
const { getConfig, setConfig, getClientIDs } = require('../../lib/config');

const clientIDs = getClientIDs();
let defaultMsg = `Usage:\n
weaver remove ignore -i [clientID] -n [<namespace> ...]
`;
if (clientIDs.length) {
defaultMsg += `\nAvailable clientIDs: ${clientIDs.join(' ')}\n`;
} else {
defaultMsg += '\nNo clients are configured yet, try adding a client first.\n';
}

const commandName = 'ignore';
const commandDesc = 'Remove an ignore collection/index namespace of a client.';
const commandSpec = (yargs) => {
shell.echo(defaultMsg);
return yargs
.options({
clientid: {
alias: 'i',
choices: clientIDs,
demandOption: true,
describe: 'The client id to remove the namespace from.',
type: 'string',
},
namespaces: {
alias: 'n',
demandOption: true,
describe: 'The namespaces to avoid querying upon.',
type: 'array',
},
});
};

const removeIgnores = (params = {}) => {
const CFG = { ...getConfig() };
const dataClient = ldColl.find(CFG.dataClients, (c) => c.clientId === params.clientid);
const clientIdx = ldArray.findIndex(CFG.dataClients, (c) => c.clientId === params.clientid);
const newClient = ldObject.assign({}, dataClient);
const clientOpts = newClient.client ? newClient.client : {};
let { ignoreFields = [] } = clientOpts;

if (clientIdx < 0) return CFG;
ignoreFields = ignoreFields.filter((f) => params.namespaces.indexOf(f) < 0);
clientOpts.ignoreFields = ldArray.uniq(ignoreFields);
newClient.client = ldObject.assign({}, clientOpts);
CFG.dataClients[clientIdx] = newClient;

return CFG;
};

const commandHandler = (params) => {
logging('ignorefield params', params);
const newClientConfig = removeIgnores(params);
if (newClientConfig) {
try {
logging('Saving ignoreFields');
setConfig(newClientConfig);
shell.echo('Saved new settings:', JSON.stringify(newClientConfig, null, 2));
} catch (error) {
shell.echo(error);
return false;
}
}
return params;
};

module.exports = {
commandName,
commandDesc,
commandSpec,
commandHandler,
removeIgnores,
};
1 change: 1 addition & 0 deletions bin/commands/remove/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
client: require('./client'),
ignore: require('./ignore'),
};
2 changes: 1 addition & 1 deletion dist/bin/commands/remove.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/bin/commands/remove/ignore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/bin/commands/remove/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"use strict";module.exports={client:require("./client")};
"use strict";module.exports={client:require("./client"),ignore:require("./ignore")};

0 comments on commit 54f129d

Please sign in to comment.