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

Override RC file with CLI params #14

Merged
merged 2 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .topcoderrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"challengeIds": [
"30095545"
],
"username": "TonyJ",
"password": "******"
}
7 changes: 6 additions & 1 deletion bin/tc-submission-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const logger = require('../src/common/logger')
const _ = require('lodash')
const fs = require('fs')

program
.option('-u, --username <uname>', 'Topcoder username')
.option('-p, --password <password>', 'Topcoder password')
.option('-c, --challengeIds <ids>', '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`)
Expand All @@ -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 => {
Expand Down
15 changes: 10 additions & 5 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ 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)
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'])))
}

/**
Expand Down Expand Up @@ -91,11 +96,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,
Expand All @@ -105,7 +110,7 @@ async function createSubmission (submissionName, submissionData, userId, userNam
data: submissionData
}
}
return await submissionApiClient.createSubmission(submission)
return submissionApiClient.createSubmission(submission)
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/services/uploadSubmissionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ 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)
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)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down