From a6299a4375c5fb3c2e0edf9006f041983b3d257c Mon Sep 17 00:00:00 2001 From: Sharathkumar Anbu Date: Sun, 18 Aug 2019 02:50:10 +0530 Subject: [PATCH 1/2] Override RC file with CLI params --- .topcoderrc | 7 +++++++ bin/tc-submission-cli.js | 7 ++++++- src/common/helper.js | 13 ++++++++----- src/services/uploadSubmissionService.js | 14 +++++++++----- test/unit.test.js | 2 +- 5 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 .topcoderrc diff --git a/.topcoderrc b/.topcoderrc new file mode 100644 index 0000000..0c41ca4 --- /dev/null +++ b/.topcoderrc @@ -0,0 +1,7 @@ +{ + "challengeIds": [ + "30095545" + ], + "username": "TonyJ", + "password": "******" +} \ No newline at end of file diff --git a/bin/tc-submission-cli.js b/bin/tc-submission-cli.js index 38a1fcb..de40ab0 100755 --- a/bin/tc-submission-cli.js +++ b/bin/tc-submission-cli.js @@ -7,6 +7,11 @@ const logger = require('../src/common/logger') const _ = require('lodash') const fs = require('fs') +program + .option('-u, --username ', 'Topcoder username') + .option('-p, --password ', 'Topcoder password') + .option('-c, --challengeIds ', 'Comma separated challenge IDs to which submission need to be done') + program.on('--help', () => { console.log(`\nTopcoder CLI to create a new submission in the challenge with contents from current directory\n`) @@ -24,7 +29,7 @@ program.on('--help', () => { program.parse(process.argv) -uploadSubmissionService.smart(process.cwd()) +uploadSubmissionService.smart(process.cwd(), program) .then(async (responses) => { const log = {} _.each(responses, response => { diff --git a/src/common/helper.js b/src/common/helper.js index 5e56672..97ae17b 100755 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -20,12 +20,15 @@ const schemaForRC = Joi.object({ * Read configuration from given topcoder rc file. * * @param {String} filename the name of the rc file + * @param {Object} cliParams CLI params passed to the program * @returns {Object} the rc object */ -function readFromRCFile (filename) { +function readFromRCFile (filename, cliParams) { logger.info('Reading from topcoder rc file...') const rcObject = JSON.parse(fs.readFileSync(filename).toString()) - return validateRCObject(rcObject) + cliParams.challengeIds = cliParams.challengeIds.split(',') + // Override values from RC file with CLI params + return validateRCObject(_.merge(rcObject, _.pick(cliParams, ['username', 'password', 'challengeIds']))) } /** @@ -91,11 +94,11 @@ async function createSubmission (submissionName, submissionData, userId, userNam logger.info(`Uploading submission on challenge ${challengeId}...`) const clientConfig = _.pick(config, ['TC_AUTHN_URL', 'TC_AUTHZ_URL', 'TC_CLIENT_ID', - 'TC_CLIENT_V2CONNECTION', 'SUBMISSION_API_URL']) + 'TC_CLIENT_V2CONNECTION', 'SUBMISSION_API_URL']) clientConfig['USERNAME'] = userName clientConfig['PASSWORD'] = password const submissionApiClient = submissionApi(clientConfig) - + const submission = { memberId: userId, challengeId: challengeId, @@ -105,7 +108,7 @@ async function createSubmission (submissionName, submissionData, userId, userNam data: submissionData } } - return await submissionApiClient.createSubmission(submission) + return submissionApiClient.createSubmission(submission) } /** diff --git a/src/services/uploadSubmissionService.js b/src/services/uploadSubmissionService.js index 43a4ddd..c4beb36 100755 --- a/src/services/uploadSubmissionService.js +++ b/src/services/uploadSubmissionService.js @@ -10,15 +10,19 @@ const logger = require('../common/logger') * Provide high-level functionality for upload a submission. * Under current working directory, it archives all files except rc file and * read rc configuration from the .topcoderrc file. - * + * @param {String} currDir Path to current directory + * @param {Object} cliParams CLI params passed to the program * @returns {Array} Uploaded submissions */ -async function smart (prefix) { - const { username, password, challengeIds } = helper.readFromRCFile(path.join(prefix, constants.rc.name)) +async function smart (currDir, cliParams) { + const { username, password, challengeIds } = helper.readFromRCFile(path.join(currDir, constants.rc.name), cliParams) + console.log(username) + console.log(password) + console.log(challengeIds) const userId = await helper.getUserId(username) const submissionName = helper.submissionNameFromUserId(userId) - const submissionData = helper.archiveCodebase(prefix) - return await basic(submissionName, submissionData, userId, username, password, challengeIds) + const submissionData = helper.archiveCodebase(currDir) + return basic(submissionName, submissionData, userId, username, password, challengeIds) } /** diff --git a/test/unit.test.js b/test/unit.test.js index 3f1201d..fb137a8 100755 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -35,7 +35,7 @@ describe('TC Submission CLI Test', async function () { testData.userId.admin, 'TonyJ', 'appirio123', - [ testData.challengeId.valid ] + [testData.challengeId.valid] ) }) it('topcoderrc - It should check if topcoder rc file exists', async function () { From 01025c6404b3fa4429f6371c807bcf420f8c1dc4 Mon Sep 17 00:00:00 2001 From: Sharathkumar Anbu Date: Sun, 18 Aug 2019 14:42:05 +0530 Subject: [PATCH 2/2] Review fixes --- src/common/helper.js | 4 +++- src/services/uploadSubmissionService.js | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/common/helper.js b/src/common/helper.js index 97ae17b..820071d 100755 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -26,7 +26,9 @@ const schemaForRC = Joi.object({ function readFromRCFile (filename, cliParams) { logger.info('Reading from topcoder rc file...') const rcObject = JSON.parse(fs.readFileSync(filename).toString()) - cliParams.challengeIds = cliParams.challengeIds.split(',') + if (cliParams.challengeIds) { + cliParams.challengeIds = cliParams.challengeIds.split(',') + } // Override values from RC file with CLI params return validateRCObject(_.merge(rcObject, _.pick(cliParams, ['username', 'password', 'challengeIds']))) } diff --git a/src/services/uploadSubmissionService.js b/src/services/uploadSubmissionService.js index c4beb36..e64940a 100755 --- a/src/services/uploadSubmissionService.js +++ b/src/services/uploadSubmissionService.js @@ -16,9 +16,6 @@ const logger = require('../common/logger') */ async function smart (currDir, cliParams) { const { username, password, challengeIds } = helper.readFromRCFile(path.join(currDir, constants.rc.name), cliParams) - console.log(username) - console.log(password) - console.log(challengeIds) const userId = await helper.getUserId(username) const submissionName = helper.submissionNameFromUserId(userId) const submissionData = helper.archiveCodebase(currDir)