Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commander not parsing arguments #66

Merged
merged 5 commits into from
Feb 13, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions dynamoDBtoCSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,33 @@ program
.option("-s, --size [size]", "Number of lines to read before writing.", 5000)
.parse(process.argv);

if (!program.table) {
const options = program.opts();

if (!options.table) {
console.log("You must specify a table");
program.outputHelp();
options.outputHelp();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's correct. options is doesn't have an outputHelp method. it's a program.OptionValue

process.exit(1);
}

if (program.region && AWS.config.credentials) {
AWS.config.update({ region: program.region });
if (options.region && AWS.config.credentials) {
AWS.config.update({ region: options.region });
} else {
AWS.config.loadFromPath(__dirname + "/config.json");
}

if (program.endpoint) {
AWS.config.update({ endpoint: program.endpoint });
if (options.endpoint) {
AWS.config.update({ endpoint: options.endpoint });
}

if (program.profile) {
let newCreds = new AWS.SharedIniFileCredentials({ profile: program.profile });
newCreds.profile = program.profile;
if (options.profile) {
let newCreds = new AWS.SharedIniFileCredentials({ profile: options.profile });
newCreds.profile = options.profile;
AWS.config.update({ credentials: newCreds });
}

if (program.envcreds) {
if (options.envcreds) {
let newCreds = AWS.config.credentials;
newCreds.profile = program.profile;
newCreds.profile = options.profile;
AWS.config.update({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
Expand All @@ -66,49 +68,49 @@ if (program.envcreds) {
});
}

if (program.mfa && program.profile) {
if (options.mfa && options.profile) {
const creds = new AWS.SharedIniFileCredentials({
tokenCodeFn: (serial, cb) => {cb(null, program.mfa)},
profile: program.profile
tokenCodeFn: (serial, cb) => {cb(null, options.mfa)},
profile: options.profile
});

// Update config to include MFA
AWS.config.update({ credentials: creds });
} else if (program.mfa && !program.profile) {
} else if (options.mfa && !options.profile) {
console.log('error: MFA requires a profile(-p [profile]) to work');
process.exit(1);
}

const dynamoDB = new AWS.DynamoDB();

const query = {
TableName: program.table,
IndexName: program.index,
Select: program.count ? "COUNT" : (program.select ? "SPECIFIC_ATTRIBUTES" : (program.index ? "ALL_PROJECTED_ATTRIBUTES" : "ALL_ATTRIBUTES")),
KeyConditionExpression: program.keyExpression,
ExpressionAttributeValues: JSON.parse(program.keyExpressionValues),
ProjectionExpression: program.select,
TableName: options.table,
IndexName: options.index,
Select: options.count ? "COUNT" : (options.select ? "SPECIFIC_ATTRIBUTES" : (options.index ? "ALL_PROJECTED_ATTRIBUTES" : "ALL_ATTRIBUTES")),
KeyConditionExpression: options.keyExpression,
ExpressionAttributeValues: JSON.parse(options.keyExpressionValues),
ProjectionExpression: options.select,
Limit: 1000
};

const scanQuery = {
TableName: program.table,
IndexName: program.index,
TableName: options.table,
IndexName: options.index,
Limit: 1000
};

// if there is a target file, open a write stream
if (!program.describe && program.file) {
var stream = fs.createWriteStream(program.file, { flags: 'a' });
if (!options.describe && options.file) {
var stream = fs.createWriteStream(options.file, { flags: 'a' });
}
let rowCount = 0;
let writeCount = 0;
let writeChunk = program.size;
let writeChunk = options.size;

const describeTable = () => {
dynamoDB.describeTable(
{
TableName: program.table
TableName: options.table
},
function (err, data) {
if (!err) {
Expand Down Expand Up @@ -142,7 +144,7 @@ const describeTable = () => {
const appendStats = (params, items) => {
for (let i = 0; i < items.length; i++) {
let item = items[i];
let key = item[program.stats].S;
let key = item[options.stats].S;

if (params.stats[key]) {
params.stats[key]++;
Expand Down Expand Up @@ -199,7 +201,7 @@ const queryDynamoDB = (params) => {
let query = params.query;
dynamoDB.query(query, function (err, data) {
if (!err) {
if (program.stats) {
if (options.stats) {
processStats(params, data);
} else {
processRows(params, data);
Expand All @@ -219,7 +221,7 @@ const unparseData = (lastEvaluatedKey) => {
// remove column names after first write chunk.
endData = endData.replace(/(.*\r\n)/, "");;
}
if (program.file) {
if (options.file) {
writeData(endData);
} else {
console.log(endData);
Expand Down Expand Up @@ -265,6 +267,7 @@ const unMarshalIntoArray = (items) => {
});
}

if (program.describe) describeTable(scanQuery);
if (program.keyExpression) queryDynamoDB({ "query": query, stats: {} });
if (options.describe) describeTable(scanQuery);
if (options.keyExpression) queryDynamoDB({ "query": query, stats: {} });
else scanDynamoDB(scanQuery);