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

Commit 878d880

Browse files
committed
Merge branch 'master' into emilio/luis
2 parents d3bd048 + 7c760dc commit 878d880

File tree

13 files changed

+100
-49
lines changed

13 files changed

+100
-49
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@ export default class QnamakerCreateKb extends Command {
1616
static description = 'Creates a new knowledgebase'
1717

1818
static flags: flags.Input<any> = {
19-
in: flags.string({description: 'The CreateKbDTO object to send in the body of the request.', required: true}),
19+
in: flags.string({description: 'File path to the CreateKbDTO object to send in the body of the request.'}),
2020
name: flags.string({description: 'Name of the kb you want to create. This will override the name of KB that might be present in the CreateKb DTO'}),
2121
save: flags.boolean({description: 'Save the kbId in config.'}),
22+
quiet: flags.boolean({description: 'Save the kbId in config.'}),
2223
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 config'}),
2324
help: flags.help({char: 'h', description: 'qnamaker:create:kb command help'}),
2425
}
2526

2627
async run() {
2728
const {flags} = this.parse(QnamakerCreateKb)
29+
const stdin = await this.readStdin()
30+
if (!stdin && !flags.in) {
31+
throw new CLIError('No input. Please set file path with --in or pipe required data to the command')
32+
}
2833

29-
let input: Inputs = await processInputs(flags, createKbJSON, this.config.configDir)
34+
let input: Inputs = await processInputs(flags, createKbJSON, this.config.configDir, stdin)
3035

3136
if (flags.name) {
3237
input.requestBody.name = flags.name
3338
}
3439

35-
if (!input.requestBody.name) {
36-
if (!(flags.q || flags.quiet)) {
37-
let answer = readlineSync.question('What would you like to name your new knowledgebase?')
38-
if (answer && answer.length > 0) {
39-
input.requestBody.name = answer.trim()
40-
}
40+
if (!input.requestBody.name && flags.quiet) {
41+
let answer = readlineSync.question('What would you like to name your new knowledgebase?')
42+
if (answer && answer.length > 0) {
43+
input.requestBody.name = answer.trim()
4144
}
45+
4246
}
4347
// hack to map incorrect export property from expected import. Export uses qnaDocuments, create/update/replace qnaList :(
4448
if (input.requestBody.qnaDocuments && !input.requestBody.qnaList) {

packages/qnamaker/src/commands/qnamaker/replace/alterations.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ export default class QnamakerReplaceAlterations extends Command {
77
static description = 'Replaces word alterations (synonyms) for the KB with the give records.'
88

99
static flags: flags.Input<any> = {
10-
in: flags.string({description: 'The WordAlterationsDTO object to send in the body of the request', required: true}),
10+
in: flags.string({description: 'File path to the WordAlterationsDTO object to send in the body of the request'}),
1111
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 config'}),
1212
help: flags.help({char: 'h', description: 'qnamaker:replace:alterations command help'}),
1313
}
1414

1515
async run() {
1616
const {flags} = this.parse(QnamakerReplaceAlterations)
17-
let input: Inputs = await processInputs(flags, replaceAlterationsJSON, this.config.configDir)
17+
const stdin = await this.readStdin()
18+
19+
if (!stdin && !flags.in) {
20+
throw new CLIError('No input. Please set file path with --in or pipe required data to the command')
21+
}
22+
23+
let input: Inputs = await processInputs(flags, replaceAlterationsJSON, this.config.configDir, stdin)
1824
// hack to map incorrect export property from expected import. Export uses qnaDocuments, create/update/replace qnaList :(
1925
if (input.requestBody.qnaDocuments && !input.requestBody.qnaList) {
2026
input.requestBody.qnaList = input.requestBody.qnaDocuments

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ export default class QnamakerReplaceKb extends Command {
88
static description = 'Replace a knowledgebase contents with new contents'
99

1010
static flags: flags.Input<any> = {
11-
in: flags.string({description: 'The ReplaceKbDTO object to send in the body of the request', required: true}),
11+
in: flags.string({description: 'File path to the ReplaceKbDTO object to send in the body of the request'}),
1212
kbId: flags.string({description: 'Knowledgebase id.'}),
1313
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 config'}),
1414
help: flags.help({char: 'h', description: 'qnamaker:replace:kb command help'}),
1515
}
1616

1717
async run() {
1818
const {flags} = this.parse(QnamakerReplaceKb)
19-
let input: Inputs = await processInputs(flags, replaceKbJSON, this.config.configDir)
19+
const stdin = await this.readStdin()
20+
if (!stdin && !flags.in) {
21+
throw new CLIError('No input. Please set file path with --in or pipe required data to the command')
22+
}
23+
24+
let input: Inputs = await processInputs(flags, replaceKbJSON, this.config.configDir, stdin)
2025
// hack to map incorrect export property from expected import. Export uses qnaDocuments, create/update/replace qnaList :(
2126
if (input.requestBody.qnaDocuments && !input.requestBody.qnaList) {
2227
input.requestBody.qnaList = input.requestBody.qnaDocuments

packages/qnamaker/src/commands/qnamaker/train.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class QnamakerTrain extends Command {
77
static description = 'Train call to add suggestions to the knowledgebase.'
88

99
static flags: flags.Input<any> = {
10-
in: flags.string({description: 'The FeedbackRecordDTO object to send in the body of the request.', required: true}),
10+
in: flags.string({description: 'File path to the FeedbackRecordDTO object to send in the body of the request.'}),
1111
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 config'}),
1212
hostname: flags.string({description: 'Specifies the url for your private QnA service. Overrides the value present in config.'}),
1313
endpointKey: flags.string({description: 'Specifies the endpoint key for your private QnA service.(from qnamaker.ai portal user settings page). Overrides the value present in config.'}),
@@ -17,7 +17,12 @@ export default class QnamakerTrain extends Command {
1717

1818
async run() {
1919
const {flags} = this.parse(QnamakerTrain)
20-
let input: Inputs = await processInputs(flags, trainJSON, this.config.configDir)
20+
const stdin = await this.readStdin()
21+
22+
if (!stdin && !flags.in) {
23+
throw new CLIError('No input. Please set file path with --in or pipe required data to the command')
24+
}
25+
let input: Inputs = await processInputs(flags, trainJSON, this.config.configDir, stdin)
2126
const result = await qnamaker(input.config, input.serviceManifest, flags, input.requestBody)
2227
if (result.error) {
2328
throw new CLIError(JSON.stringify(result.error, null, 4))

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class QnamakerUpdateKb extends Command {
99
static description = 'Add or delete QnA Pairs and / or URLs to an existing knowledge base'
1010

1111
static flags: flags.Input<any> = {
12-
in: flags.string({description: 'The UpdateKbOperationDTO object to send in the body of the request.', required: true}),
12+
in: flags.string({description: 'The file path to the UpdateKbOperationDTO object to send in the body of the request.'}),
1313
kbId: flags.string({description: 'Knowledgebase id.'}),
1414
wait: flags.boolean({description: 'Wait for the operation to complete.'}),
1515
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 config'}),
@@ -18,7 +18,11 @@ export default class QnamakerUpdateKb extends Command {
1818

1919
async run() {
2020
const {flags} = this.parse(QnamakerUpdateKb)
21-
let input: Inputs = await processInputs(flags, updateKbJSON, this.config.configDir)
21+
const stdin = await this.readStdin()
22+
if (!stdin && !flags.in) {
23+
throw new CLIError('No input. Please set file path with --in or pipe required data to the command')
24+
}
25+
let input: Inputs = await processInputs(flags, updateKbJSON, this.config.configDir, stdin)
2226
// hack to map incorrect export property from expected import. Export uses qnaDocuments, create/update/replace qnaList :(
2327
if (input.requestBody.qnaDocuments && !input.requestBody.qnaList) {
2428
input.requestBody.qnaList = input.requestBody.qnaDocuments
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import {CLIError} from '@microsoft/bf-cli-command'
2-
const stdin = require('get-stdin')
2+
const fs = require('fs-extra')
3+
const path = require('path')
34
const qnaconfig = require('./../../utils/qnaconfig')
45
const srvMan = require('./../../utils/servicemanifest')
56
const {ServiceBase} = require('./../../utils/api/serviceBase')
67

7-
export async function processInputs(flags: any, payload: any, configfile: string) {
8+
export async function processInputs(flags: any, payload: any, configfile: string, stdin = '') {
89
let result: Inputs = {}
910
const config = await qnaconfig.composeConfig(flags, configfile)
10-
let service = {}
1111

1212
try {
13-
if (flags.stdin) {
14-
let json = await stdin()
15-
service = await JSON.parse(json)
16-
}
17-
qnaconfig.buildConfig(flags, service, config)
13+
qnaconfig.buildConfig(flags, config)
1814
ServiceBase.config = flags
1915
await qnaconfig.validateConfig(flags)
2016
const serviceManifest = srvMan.getServiceManifest(payload)
17+
flags.in = stdin ? stdin : flags.in
2118
result.requestBody = await srvMan.validateArguments(serviceManifest, flags)
19+
if (stdin || flags.in) {
20+
result.requestBody = stdin ? JSON.parse(stdin) : await getFileInput(flags.in)
21+
}
2222
result.config = config
2323
result.serviceManifest = serviceManifest
2424
} catch (e) {
@@ -28,6 +28,12 @@ export async function processInputs(flags: any, payload: any, configfile: string
2828
return result
2929
}
3030

31+
async function getFileInput(file: string) {
32+
// Let any errors fall through to the runProgram() promise
33+
let body = await fs.readJSON(path.resolve(file))
34+
return body
35+
}
36+
3137
export interface Inputs {
3238
[key: string]: any
3339
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,13 @@ describe('qnamaker:create:kb --save', () => {
118118
let config = await fs.readJSON(getConfigFile())
119119
expect(config.qnamaker.kbId).to.contain('8600c573-2acf-4466-97e8-999ad4cecbc2')
120120
})
121+
})
122+
123+
describe('qnamaker:create:kb No input', () => {
124+
test
125+
.stderr()
126+
.command(['qnamaker:create:kb'])
127+
.it('Creates kb qnamaker:create:kb --in', ctx => {
128+
expect(ctx.stderr).to.contain('No input. Please set file path with --in or pipe required data to the command')
129+
})
121130
})

packages/qnamaker/test/commands/qnamaker/replace/alterations.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ describe('qnamaker:replace:alterations', () => {
2323
.it('runs qnamaker:replace:alterations', ctx => {
2424
expect(ctx.stdout).to.empty
2525
})
26+
27+
test
28+
.stderr()
29+
.command(['qnamaker:replace:alterations'])
30+
.it('runs qnamaker:replace:alterations', ctx => {
31+
expect(ctx.stderr).to.contain('No input. Please set file path with --in or pipe required data to the command')
32+
})
2633
})

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,13 @@ describe('qnamaker:replace:kb', () => {
2222
.it('runs qnamaker:replace:kb --kbId xxxxxxxxxx --in replace.json', ctx => {
2323
expect(ctx.stdout).to.equal('')
2424
})
25+
26+
test
27+
.stderr()
28+
.command(['qnamaker:replace:kb'])
29+
.it('runs qnamaker:replace:kb', ctx => {
30+
expect(ctx.stderr).to.contain('No input. Please set file path with --in or pipe required data to the command')
31+
})
32+
33+
2534
})
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {expect, test} from '@oclif/test'
22

33
describe('qnamaker:train', () => {
4-
4+
test
5+
.stderr()
6+
.command(['qnamaker:train'])
7+
.it('qnamaker:train no input passed', ctx => {
8+
expect(ctx.stderr).to.contain('No input. Please set file path with --in or pipe required data to the command')
9+
})
510
})

0 commit comments

Comments
 (0)