Skip to content

Commit

Permalink
added, logic to add ignorefields, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SkeloGH committed Jan 2, 2020
1 parent 108b062 commit 50c070e
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 35 deletions.
3 changes: 3 additions & 0 deletions bin/__tests__/commands/add/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ describe('weaver add command tests', () => {
test('Client logic is exported', () => {
expect(importedModule.client).not.toBe(undefined);
});
test('Ignores logic is exported', () => {
expect(importedModule.ignore).not.toBe(undefined);
});
});
35 changes: 18 additions & 17 deletions bin/commands/add.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
const Debug = require('debug');
const { client } = require('./add/');
const { client, ignore } = require('./add/');

const logging = Debug('Weaver:bin:commands:add');
const defaultMsg = `
You need at least one command before moving on
weaver add [client|query|ignoreField]`;

module.exports = {
name: 'add',
description: 'Interactive creation of client, query or ignoreField',
description: 'Creation of client, query or ignore',
setup: (yargs) => {
const cmd = yargs.command(
client.commandName,
client.commandDesc,
client.commandSpec,
client.commandHandler,
)
const defaultMsg = `
You need at least one command before moving on
weaver add [${client.commandName}|query|${ignore.commandName}]`;
const cmd = yargs
.command(
client.commandName,
client.commandDesc,
client.commandSpec,
client.commandHandler,
)
.command('query', 'Interactive creation of a new query',
(_yargs) => _yargs,
(params) => {
logging('query params', params);
return params;
})
.command('ignoreField', 'Interactive creation of a new ignoreField',
(_yargs) => _yargs,
(params) => {
logging('ignoreField params', params);
return params;
})
.command(
ignore.commandName,
ignore.commandDesc,
ignore.commandSpec,
ignore.commandHandler,
)
.demandCommand(1, defaultMsg);
return cmd;
},
Expand Down
2 changes: 1 addition & 1 deletion bin/commands/add/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const defaultMsg = `Usage
weaver add client -f [mongodb] -t [source|target] -o [<source.name>] -n my_source_db -u mongodb://localhost:27017
`;
const commandName = 'client';
const commandDesc = 'Interactive creation of a new client';
const commandDesc = 'Creation of a new client';

const isSameFamily = (c, _c) => c.family === _c.family;
const isSameType = (c, _c) => c.type === _c.type;
Expand Down
77 changes: 77 additions & 0 deletions bin/commands/add/ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 = 'No clients are configured yet, try adding a client first.';
if (clientIDs.length) {
defaultMsg = `
Usage
weaver add ignore -i [clientID] -ns [<namespace> ...]
Available clientIDs: ${clientIDs.join(' ')}
`;
}
const commandName = 'ignore';
const commandDesc = 'Add a collection/index namespace to ignore when performing queries';
const commandSpec = (yargs) => {
shell.echo(defaultMsg);
return yargs
.options({
clientid: {
alias: 'i',
choices: clientIDs,
demandOption: true,
describe: 'The client id that will skip reading from the provided namespace.',
type: 'string',
},
namespace: {
alias: 'n',
demandOption: true,
describe: 'The namespaces to avoid querying upon.',
type: 'array',
},
});
};

const addIgnores = (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;

ignoreFields = ignoreFields.concat(params.namespace);
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 = addIgnores(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,
};
1 change: 1 addition & 0 deletions bin/commands/add/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
client: require('./client'),
ignore: require('./ignore'),
};
6 changes: 3 additions & 3 deletions bin/commands/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logging = Debug('Weaver:bin:commands:remove');

module.exports = {
name: 'remove',
description: 'Interactive removal of clients, queries or ignoreFields',
description: 'Interactive removal of clients, queries or ignores',
setup: (yargs) => {
const cmd = yargs.command(
client.commandName,
Expand All @@ -19,10 +19,10 @@ module.exports = {
logging('query params', params);
return params;
})
.command('ignoreField', 'Interactive removal of one or more ignoreFields',
.command('ignore', 'Interactive removal of one or more ignores',
(_yargs) => _yargs,
(params) => {
logging('ignoreField params', params);
logging('ignore params', params);
return params;
});
return cmd;
Expand Down
2 changes: 1 addition & 1 deletion bin/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {

if (!hasDataClients) {
message = `Error: dataClients not set, try:
weaver add [client|query|ignoreField]
weaver add [client|query|ignore]
`;
}
if (!hasQueries) {
Expand Down
12 changes: 7 additions & 5 deletions bin/options/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const ld = require('lodash');
const ldLang = require('lodash/lang');
const ldArray = require('lodash/array');
const ldObject = require('lodash/object');
const fs = require('fs');
const Debug = require('debug');
const shell = require('shelljs');
Expand Down Expand Up @@ -28,9 +30,9 @@ const isJSONFile = (filePath) => {
};

const isValidConfigObject = (cfg) => {
const isPlainObject = ld.isPlainObject(cfg);
const keys = ld.keys(cfg);
const matchingKeys = ld.intersection(REQUIRED_CONFIG_KEYS, keys);
const isPlainObject = ldLang.isPlainObject(cfg);
const keys = ldObject.keys(cfg);
const matchingKeys = ldArray.intersection(REQUIRED_CONFIG_KEYS, keys);
return isPlainObject && matchingKeys.length === REQUIRED_CONFIG_KEYS.length;
};

Expand Down Expand Up @@ -68,7 +70,7 @@ const validationFeedback = (validation, filePath) => {

const saveConfigPath = (configFilePath) => {
const CFG = getCLIJSONContent();
const configContent = ld.assign({}, CFG, { config: { filePath: configFilePath } });
const configContent = ldObject.assign({}, CFG, { config: { filePath: configFilePath } });
try {
fs.writeFileSync(cfgAbsPath, JSON.stringify(configContent, null, 2));
if (!TEST_NODE_ENV) shell.echo('Updated config file:\n\t', configContent.config);
Expand Down
6 changes: 3 additions & 3 deletions dist/bin/commands/add.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/add/client.js

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

6 changes: 6 additions & 0 deletions dist/bin/commands/add/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/add/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")};
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.

2 changes: 1 addition & 1 deletion dist/bin/commands/run.js

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

Loading

0 comments on commit 50c070e

Please sign in to comment.