Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 00a8532

Browse files
Merge pull request #14 from sharathkumaranbu/Issue_12
Override RC file with CLI params
2 parents f8f2054 + 01025c6 commit 00a8532

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

.topcoderrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"challengeIds": [
3+
"30095545"
4+
],
5+
"username": "TonyJ",
6+
"password": "******"
7+
}

bin/tc-submission-cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const logger = require('../src/common/logger')
77
const _ = require('lodash')
88
const fs = require('fs')
99

10+
program
11+
.option('-u, --username <uname>', 'Topcoder username')
12+
.option('-p, --password <password>', 'Topcoder password')
13+
.option('-c, --challengeIds <ids>', 'Comma separated challenge IDs to which submission need to be done')
14+
1015
program.on('--help', () => {
1116
console.log(`\nTopcoder CLI to create a new submission in the challenge
1217
with contents from current directory\n`)
@@ -24,7 +29,7 @@ program.on('--help', () => {
2429

2530
program.parse(process.argv)
2631

27-
uploadSubmissionService.smart(process.cwd())
32+
uploadSubmissionService.smart(process.cwd(), program)
2833
.then(async (responses) => {
2934
const log = {}
3035
_.each(responses, response => {

src/common/helper.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ const schemaForRC = Joi.object({
2020
* Read configuration from given topcoder rc file.
2121
*
2222
* @param {String} filename the name of the rc file
23+
* @param {Object} cliParams CLI params passed to the program
2324
* @returns {Object} the rc object
2425
*/
25-
function readFromRCFile (filename) {
26+
function readFromRCFile (filename, cliParams) {
2627
logger.info('Reading from topcoder rc file...')
2728
const rcObject = JSON.parse(fs.readFileSync(filename).toString())
28-
return validateRCObject(rcObject)
29+
if (cliParams.challengeIds) {
30+
cliParams.challengeIds = cliParams.challengeIds.split(',')
31+
}
32+
// Override values from RC file with CLI params
33+
return validateRCObject(_.merge(rcObject, _.pick(cliParams, ['username', 'password', 'challengeIds'])))
2934
}
3035

3136
/**
@@ -91,11 +96,11 @@ async function createSubmission (submissionName, submissionData, userId, userNam
9196
logger.info(`Uploading submission on challenge ${challengeId}...`)
9297
const clientConfig = _.pick(config,
9398
['TC_AUTHN_URL', 'TC_AUTHZ_URL', 'TC_CLIENT_ID',
94-
'TC_CLIENT_V2CONNECTION', 'SUBMISSION_API_URL'])
99+
'TC_CLIENT_V2CONNECTION', 'SUBMISSION_API_URL'])
95100
clientConfig['USERNAME'] = userName
96101
clientConfig['PASSWORD'] = password
97102
const submissionApiClient = submissionApi(clientConfig)
98-
103+
99104
const submission = {
100105
memberId: userId,
101106
challengeId: challengeId,
@@ -105,7 +110,7 @@ async function createSubmission (submissionName, submissionData, userId, userNam
105110
data: submissionData
106111
}
107112
}
108-
return await submissionApiClient.createSubmission(submission)
113+
return submissionApiClient.createSubmission(submission)
109114
}
110115

111116
/**

src/services/uploadSubmissionService.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ const logger = require('../common/logger')
1010
* Provide high-level functionality for upload a submission.
1111
* Under current working directory, it archives all files except rc file and
1212
* read rc configuration from the .topcoderrc file.
13-
*
13+
* @param {String} currDir Path to current directory
14+
* @param {Object} cliParams CLI params passed to the program
1415
* @returns {Array} Uploaded submissions
1516
*/
16-
async function smart (prefix) {
17-
const { username, password, challengeIds } = helper.readFromRCFile(path.join(prefix, constants.rc.name))
17+
async function smart (currDir, cliParams) {
18+
const { username, password, challengeIds } = helper.readFromRCFile(path.join(currDir, constants.rc.name), cliParams)
1819
const userId = await helper.getUserId(username)
1920
const submissionName = helper.submissionNameFromUserId(userId)
20-
const submissionData = helper.archiveCodebase(prefix)
21-
return await basic(submissionName, submissionData, userId, username, password, challengeIds)
21+
const submissionData = helper.archiveCodebase(currDir)
22+
return basic(submissionName, submissionData, userId, username, password, challengeIds)
2223
}
2324

2425
/**

test/unit.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('TC Submission CLI Test', async function () {
3535
testData.userId.admin,
3636
'TonyJ',
3737
'appirio123',
38-
[ testData.challengeId.valid ]
38+
[testData.challengeId.valid]
3939
)
4040
})
4141
it('topcoderrc - It should check if topcoder rc file exists', async function () {

0 commit comments

Comments
 (0)