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

support output to file for kb:export command #1065

Merged
merged 1 commit into from
Nov 30, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,9 @@ USAGE
$ bf qnamaker:kb:export

OPTIONS
-f, --force [default: false] If --out flag is provided with the path to an existing file, overwrites that file.
-h, --help qnamaker:kb:export command help
-o, --out Output file path. If not specified stdout will be used as output.
--endpoint=endpoint Overrides public endpoint https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/
--environment=environment [default: Prod] Specifies whether environment is Test or Prod.

Expand Down
2 changes: 2 additions & 0 deletions packages/qnamaker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ USAGE
$ bf qnamaker:kb:export

OPTIONS
-f, --force [default: false] If --out flag is provided with the path to an existing file, overwrites that file.
-h, --help qnamaker:kb:export command help
-o, --out Output file path. If not specified stdout will be used as output.
--endpoint=endpoint Overrides public endpoint https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/
--environment=environment [default: Prod] Specifies whether environment is Test or Prod.

Expand Down
37 changes: 31 additions & 6 deletions packages/qnamaker/src/commands/qnamaker/kb/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* Licensed under the MIT License.
*/

import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
import {CLIError, Command, flags, utils} from '@microsoft/bf-cli-command'
import {Inputs, processInputs} from '../../../utils/qnamakerbase'
const fs = require('fs-extra')
const path = require('path')
const qnamaker = require('./../../../../utils/index')
const exportKbJSON = require('./../../../../utils/payloads/exportkb')
import {Inputs, processInputs} from '../../../utils/qnamakerbase'

export default class QnamakerKbExport extends Command {
static description = 'Echos a knowledgebase in json or qna format to stdout'
Expand All @@ -17,21 +19,44 @@ export default class QnamakerKbExport extends Command {
environment: flags.string({description: 'Specifies whether environment is Test or Prod.', default: 'Prod'}),
subscriptionKey: flags.string({description: 'Specifies the qnamaker Ocp-Apim-Subscription Key (found in Keys under Resource Management section for your Qna Maker cognitive service). Overrides the subscriptionkey value present in the config'}),
endpoint: flags.string({description: 'Overrides public endpoint https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/'}),
out: flags.string({char: 'o', description: 'Output file path. If not specified stdout will be used as output.'}),
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file.', default: false}),
help: flags.help({char: 'h', description: 'qnamaker:kb:export command help'}),
}

async run() {
const {flags} = this.parse(QnamakerKbExport)
let input: Inputs = await processInputs(flags, exportKbJSON, this.config.configDir)

const result = await qnamaker(input.config, input.serviceManifest, flags, input.requestBody)
let result = await qnamaker(input.config, input.serviceManifest, flags, input.requestBody)
if (result.error) {
throw new CLIError(JSON.stringify(result.error, null, 4))
} else {
if (typeof result === 'string')
if (typeof result !== 'string') {
result = JSON.stringify(result, null, 2)
}

if (flags.out) {
await this.writeOutput(result, flags)
} else {
this.log(result)
else
this.log(JSON.stringify(result, null, 2))
}
}
}

async writeOutput(result: any, flags: any) {
let fullPath = path.resolve(flags.out)
let root = path.dirname(fullPath)
if (!fs.existsSync(root)) {
fs.mkdirSync(root)
}

const validatedPath = utils.validatePath(fullPath, '', flags.force)

try {
await fs.writeFile(validatedPath, result, 'utf-8')
} catch (error) {
throw new CLIError('Unable to write file - ' + validatedPath + ' Error: ' + error.message)
}
}
}
42 changes: 42 additions & 0 deletions packages/qnamaker/test/commands/qnamaker/kb/export.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import {expect, test} from '@oclif/test'
const fs = require('fs-extra')
const path = require('path')

import {deleteTestConfigFile, initTestConfigFile} from '../../../configfilehelper'
const nock = require('nock')

const compareQnaFiles = async function (file1: string, file2: string) {
let result = await fs.readFile(path.join(__dirname, file1))
let fixtureFile = await fs.readFile(path.join(__dirname, file2))
result = result.toString().replace(/\r\n/g, "\n")
fixtureFile = fixtureFile.toString().replace(/\r\n/g, "\n")
return result === fixtureFile
}

describe('qnamaker:kb:export', () => {
before(async function () {
await initTestConfigFile()
Expand Down Expand Up @@ -82,3 +92,35 @@ describe('[qnaformat] qnamaker:kb:export', () => {
})
})

describe('[qnaformat] qnamaker:kb:export to qna file', () => {
before(async function () {
await initTestConfigFile()
// runs before all tests in this block
nock('https://westus.api.cognitive.microsoft.com/qnamaker/v4.0')
.get('/knowledgebases/5690998c-4438-4ae1-900a-88a2aa3bfa68/Test/qna?qnaformat=true')
.reply(200,
`# ? Hello
- Plus d'information sur la lettre reçu des éléctions?

\`\`\`
Plus d'information sur la lettre reçu des éléctions
\`\`\``)
})

after(async function () {
await deleteTestConfigFile()
await fs.remove(path.join(__dirname, './../../../../exportSpecialChars.qna'))
})

test
.stdout()
.command(['qnamaker:kb:export',
'--kbId', '5690998c-4438-4ae1-900a-88a2aa3bfa68',
'--environment', 'Test',
'--qnaFormat',
'--out', 'exportSpecialChars.qna'])
.it('Exports kb to qna file', async () => {
expect(await compareQnaFiles('./../../../../exportSpecialChars.qna', './../../../fixtures/verified/exportSpecialChars.qna')).to.be.true
nock.cleanAll()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ? Hello
- Plus d'information sur la lettre reçu des éléctions?

```
Plus d'information sur la lettre reçu des éléctions
```