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

Commit 1253a5b

Browse files
authored
support output to file for kb:export command (#1065)
1 parent 853f44c commit 1253a5b

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

packages/cli/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,9 @@ USAGE
16171617
$ bf qnamaker:kb:export
16181618
16191619
OPTIONS
1620+
-f, --force [default: false] If --out flag is provided with the path to an existing file, overwrites that file.
16201621
-h, --help qnamaker:kb:export command help
1622+
-o, --out Output file path. If not specified stdout will be used as output.
16211623
--endpoint=endpoint Overrides public endpoint https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/
16221624
--environment=environment [default: Prod] Specifies whether environment is Test or Prod.
16231625

packages/qnamaker/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,9 @@ USAGE
400400
$ bf qnamaker:kb:export
401401
402402
OPTIONS
403+
-f, --force [default: false] If --out flag is provided with the path to an existing file, overwrites that file.
403404
-h, --help qnamaker:kb:export command help
405+
-o, --out Output file path. If not specified stdout will be used as output.
404406
--endpoint=endpoint Overrides public endpoint https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/
405407
--environment=environment [default: Prod] Specifies whether environment is Test or Prod.
406408

packages/qnamaker/src/commands/qnamaker/kb/export.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
* Licensed under the MIT License.
44
*/
55

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

1113
export default class QnamakerKbExport extends Command {
1214
static description = 'Echos a knowledgebase in json or qna format to stdout'
@@ -17,21 +19,44 @@ export default class QnamakerKbExport extends Command {
1719
environment: flags.string({description: 'Specifies whether environment is Test or Prod.', default: 'Prod'}),
1820
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'}),
1921
endpoint: flags.string({description: 'Overrides public endpoint https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/'}),
22+
out: flags.string({char: 'o', description: 'Output file path. If not specified stdout will be used as output.'}),
23+
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file.', default: false}),
2024
help: flags.help({char: 'h', description: 'qnamaker:kb:export command help'}),
2125
}
2226

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

27-
const result = await qnamaker(input.config, input.serviceManifest, flags, input.requestBody)
31+
let result = await qnamaker(input.config, input.serviceManifest, flags, input.requestBody)
2832
if (result.error) {
2933
throw new CLIError(JSON.stringify(result.error, null, 4))
3034
} else {
31-
if (typeof result === 'string')
35+
if (typeof result !== 'string') {
36+
result = JSON.stringify(result, null, 2)
37+
}
38+
39+
if (flags.out) {
40+
await this.writeOutput(result, flags)
41+
} else {
3242
this.log(result)
33-
else
34-
this.log(JSON.stringify(result, null, 2))
43+
}
44+
}
45+
}
46+
47+
async writeOutput(result: any, flags: any) {
48+
let fullPath = path.resolve(flags.out)
49+
let root = path.dirname(fullPath)
50+
if (!fs.existsSync(root)) {
51+
fs.mkdirSync(root)
52+
}
53+
54+
const validatedPath = utils.validatePath(fullPath, '', flags.force)
55+
56+
try {
57+
await fs.writeFile(validatedPath, result, 'utf-8')
58+
} catch (error) {
59+
throw new CLIError('Unable to write file - ' + validatedPath + ' Error: ' + error.message)
3560
}
3661
}
3762
}

packages/qnamaker/test/commands/qnamaker/kb/export.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import {expect, test} from '@oclif/test'
2+
const fs = require('fs-extra')
3+
const path = require('path')
24

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

8+
const compareQnaFiles = async function (file1: string, file2: string) {
9+
let result = await fs.readFile(path.join(__dirname, file1))
10+
let fixtureFile = await fs.readFile(path.join(__dirname, file2))
11+
result = result.toString().replace(/\r\n/g, "\n")
12+
fixtureFile = fixtureFile.toString().replace(/\r\n/g, "\n")
13+
return result === fixtureFile
14+
}
15+
616
describe('qnamaker:kb:export', () => {
717
before(async function () {
818
await initTestConfigFile()
@@ -82,3 +92,35 @@ describe('[qnaformat] qnamaker:kb:export', () => {
8292
})
8393
})
8494

95+
describe('[qnaformat] qnamaker:kb:export to qna file', () => {
96+
before(async function () {
97+
await initTestConfigFile()
98+
// runs before all tests in this block
99+
nock('https://westus.api.cognitive.microsoft.com/qnamaker/v4.0')
100+
.get('/knowledgebases/5690998c-4438-4ae1-900a-88a2aa3bfa68/Test/qna?qnaformat=true')
101+
.reply(200,
102+
`# ? Hello
103+
- Plus d'information sur la lettre reçu des éléctions?
104+
105+
\`\`\`
106+
Plus d'information sur la lettre reçu des éléctions
107+
\`\`\``)
108+
})
109+
110+
after(async function () {
111+
await deleteTestConfigFile()
112+
await fs.remove(path.join(__dirname, './../../../../exportSpecialChars.qna'))
113+
})
114+
115+
test
116+
.stdout()
117+
.command(['qnamaker:kb:export',
118+
'--kbId', '5690998c-4438-4ae1-900a-88a2aa3bfa68',
119+
'--environment', 'Test',
120+
'--qnaFormat',
121+
'--out', 'exportSpecialChars.qna'])
122+
.it('Exports kb to qna file', async () => {
123+
expect(await compareQnaFiles('./../../../../exportSpecialChars.qna', './../../../fixtures/verified/exportSpecialChars.qna')).to.be.true
124+
nock.cleanAll()
125+
})
126+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ? Hello
2+
- Plus d'information sur la lettre reçu des éléctions?
3+
4+
```
5+
Plus d'information sur la lettre reçu des éléctions
6+
```

0 commit comments

Comments
 (0)