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

Commit 9c19e26

Browse files
authored
Flatten QnaMaker config settings (#264)
* Flatten config file * Refactor to key storing in config
1 parent 15ba053 commit 9c19e26

File tree

13 files changed

+48
-60
lines changed

13 files changed

+48
-60
lines changed

packages/config/src/commands/config/set/qnamaker.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ export default class ConfigSetQnamaker extends Command {
5454
}
5555

5656
setValue(key: string, value: string, userConfig: Config) {
57-
let qnamaker = userConfig.qnamaker
58-
if (!qnamaker) {
59-
userConfig.qnamaker = {}
60-
}
61-
62-
userConfig.qnamaker[key] = value
57+
userConfig['qnamaker__' + key] = value
6358
this.log(`${key} set to ${value}`)
6459
}
6560
}

packages/config/src/commands/config/show/qnamaker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export default class ConfigShowQnamaker extends Command {
1515

1616
async run() {
1717
const userConfig: Config = await getConfigFile(this.config.configDir)
18-
this.log(JSON.stringify(userConfig.qnamaker, null, 2))
18+
let qnaMaker: any = {}
19+
Object.keys(userConfig).forEach((key: string) => {
20+
if (key.startsWith('qnamaker__')) {
21+
qnaMaker[key] = userConfig[key]
22+
}
23+
})
24+
this.log(JSON.stringify(qnaMaker, null, 2))
1925
}
2026
}

packages/config/test/commands/config/set/qnamaker.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ describe('config:set:qnamaker', () => {
1616
.command(['config:set:qnamaker', '--kbId', 'aaaaaaaa'])
1717
.it('Sets kbid in config file', async ctx => {
1818
let config = await fs.readJSON(getConfigFile())
19-
expect(config.qnamaker.kbId).to.contain('aaaaaaaa')
19+
expect(config.qnamaker__kbId).to.contain('aaaaaaaa')
2020
})
2121

2222
test
2323
.stdout()
2424
.command(['config:set:qnamaker', '--subscriptionKey', 'aaaaaaaa'])
2525
.it('Sets subscriptionkey in config file', async ctx => {
2626
let config = await fs.readJSON(getConfigFile())
27-
expect(config.qnamaker.subscriptionKey).to.contain('aaaaaaaa')
27+
expect(config.qnamaker__subscriptionKey).to.contain('aaaaaaaa')
2828
})
2929

3030
test
3131
.stdout()
3232
.command(['config:set:qnamaker', '--endpointKey', 'aaaaaaaa'])
3333
.it('Sets endpointKey in config file', async ctx => {
3434
let config = await fs.readJSON(getConfigFile())
35-
expect(config.qnamaker.subscriptionKey).to.contain('aaaaaaaa')
35+
expect(config.qnamaker__endpointKey).to.contain('aaaaaaaa')
3636
})
3737

3838
test
3939
.stdout()
4040
.command(['config:set:qnamaker', '--hostname', 'aaaaaaaa'])
4141
.it('Sets hostname in config file', async ctx => {
4242
let config = await fs.readJSON(getConfigFile())
43-
expect(config.qnamaker.subscriptionKey).to.contain('aaaaaaaa')
43+
expect(config.qnamaker__hostname).to.contain('aaaaaaaa')
4444
})
4545

4646
test
@@ -62,7 +62,7 @@ describe('config:set:qnamaker empty config file', () => {
6262
.command(['config:set:qnamaker', '--kbId', 'aaaaaaaa'])
6363
.it('Sets kbid in config file', async ctx => {
6464
let config = await fs.readJSON(getConfigFile())
65-
expect(config.qnamaker.kbId).to.contain('aaaaaaaa')
65+
expect(config.qnamaker__kbId).to.contain('aaaaaaaa')
6666
})
6767
})
6868

packages/config/test/commands/config/show.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('config:show', () => {
1414
.stdout()
1515
.command(['config:show'])
1616
.it('Displays config file data', ctx => {
17-
expect(ctx.stdout).to.contain('"subscriptionKey": "222222cccccctttttth223kk3k33"')
17+
expect(ctx.stdout).to.contain('"qnamaker__subscriptionKey": "222222cccccctttttth223kk3k33"')
1818
})
1919
})
2020

packages/config/test/commands/config/show/qnamaker.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ describe('config:show:qnamaker', () => {
1414
.stdout()
1515
.command(['config:show:qnamaker'])
1616
.it('Displays config file qnamaker data', ctx => {
17-
expect(ctx.stdout).to.contain('"subscriptionKey": "222222cccccctttttth223kk3k33",\n "hostname": "https://somehost.net",\n ')
17+
expect(ctx.stdout).to.contain('"qnamaker__subscriptionKey": "222222cccccctttttth223kk3k33",\n "qnamaker__hostname": "https://somehost.net",\n ')
1818
})
1919
})

packages/config/test/configfilehelper.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ const configFile = path.join(pathToConfigJson, 'config.json')
2424
export async function initTestConfigFile() {
2525
const config = {
2626
telemetry:true,
27-
qnamaker: {
28-
subscriptionKey: "222222cccccctttttth223kk3k33",
29-
hostname: "https://somehost.net",
30-
endpointKey: "xxxxxxxxxxxxxxxxxxx",
31-
kbId: "xxxxxxxxxxxxxxxxxxxxxxx"
32-
}
27+
qnamaker__subscriptionKey: "222222cccccctttttth223kk3k33",
28+
qnamaker__hostname: "https://somehost.net",
29+
qnamaker__endpointKey: "xxxxxxxxxxxxxxxxxxx",
30+
qnamaker__kbId: "xxxxxxxxxxxxxxxxxxxxxxx"
3331
}
3432

3533
await fs.mkdirp(pathToConfigJson)

packages/qnamaker/src/commands/qnamaker/init.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
*/
55

66
import {Command, flags} from '@microsoft/bf-cli-command'
7+
import {updateQnAMakerConfig} from '../../utils/qnamakerbase'
78
import cli from 'cli-ux'
89

910
const Knowledgebase = require('./../../../utils/api/knowledgebase')
1011
const Endpointkeys = require('./../../../utils/api/endpointkeys')
1112
const chalk = require('chalk')
12-
const fs = require('fs-extra')
13-
const path = require('path')
1413

1514
export default class QnamakerInit extends Command {
1615
static description = 'Initializes the config file with settings.'
@@ -45,7 +44,7 @@ export default class QnamakerInit extends Command {
4544

4645
let [subscriptionKey, kbId] = answers
4746
/* tslint:disable: prefer-object-spread */
48-
const config = Object.assign({}, {subscriptionKey, kbId, endpoint})
47+
const config = Object.assign({}, {subscriptionKey, kbId, endpoint, endpointKey: '', hostname: ''})
4948

5049
if (subscriptionKey && kbId) {
5150
cli.action.start('Updating config file hostname and endpoint key')
@@ -63,15 +62,7 @@ export default class QnamakerInit extends Command {
6362
}
6463

6564
if (confirmation) {
66-
let userConfig: any = {}
67-
if (fs.existsSync(path.join(this.config.configDir, 'config.json'))) {
68-
userConfig = await fs.readJSON(path.join(this.config.configDir, 'config.json'))
69-
} else {
70-
await fs.mkdirp(this.config.configDir)
71-
}
72-
73-
userConfig.qnamaker = config
74-
await fs.writeJson(path.join(this.config.configDir, 'config.json'), userConfig, {spaces: 2})
65+
await updateQnAMakerConfig(config, this.config.configDir)
7566
}
7667
return confirmation
7768
}

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ const Knowledgebase = require('./../../../../utils/api/knowledgebase')
1111
const Endpointkeys = require('./../../../../utils/api/endpointkeys')
1212
const createKbJSON = require('./../../../../utils/payloads/createKb')
1313

14-
const fs = require('fs-extra')
15-
const path = require('path')
1614
const readlineSync = require('readline-sync')
1715

18-
import {Inputs, processInputs} from '../../../utils/qnamakerbase'
16+
import {Inputs, processInputs, updateQnAMakerConfig} from '../../../utils/qnamakerbase'
1917

2018
export default class QnamakerKbCreate extends Command {
2119
static description = 'Creates a new knowledgebase'
@@ -66,18 +64,7 @@ export default class QnamakerKbCreate extends Command {
6664
if (flags.save) {
6765
input.config.kbId = kbId
6866
await this.updateKbId(input.config)
69-
let userConfig: any = {}
70-
71-
if (fs.existsSync(path.join(this.config.configDir, 'config.json'))) {
72-
userConfig = await fs.readJSON(path.join(this.config.configDir, 'config.json'))
73-
} else {
74-
await fs.mkdirp(this.config.configDir)
75-
}
76-
77-
delete input.config.endpoint
78-
79-
userConfig.qnamaker = input.config
80-
await fs.writeJson(path.join(this.config.configDir, 'config.json'), userConfig, {spaces: 2})
67+
await updateQnAMakerConfig(input.config, this.config.configDir)
8168
} else {
8269
this.log(JSON.stringify({kbId}, null, 2))
8370
}

packages/qnamaker/src/utils/qnamakerbase.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ export async function getFileInput(file: string) {
4040
return body
4141
}
4242

43+
export async function updateQnAMakerConfig(config: any , configfile: string) {
44+
let userConfig: any = {}
45+
if (fs.existsSync(path.join(configfile, 'config.json'))) {
46+
userConfig = await fs.readJSON(path.join(configfile, 'config.json'))
47+
} else {
48+
await fs.mkdirp(configfile)
49+
}
50+
userConfig.qnamaker__subscriptionKey = config.subscriptionKey ? config.subscriptionKey : userConfig.qnamaker__subscriptionKey
51+
userConfig.qnamaker__kbId = config.kbId ? config.kbId : userConfig.qnamaker__kbId
52+
userConfig.qnamaker__endpointKey = config.endpointKey ? config.endpointKey : userConfig.qnamaker__endpointKey
53+
userConfig.qnamaker__hostname = config.hostname ? config.hostname : userConfig.qnamaker__hostname
54+
await fs.writeJson(path.join(configfile, 'config.json'), userConfig, {spaces: 2})
55+
}
56+
4357
export interface Inputs {
4458
[key: string]: any
4559
}

packages/qnamaker/test/commands/qnamaker/init.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('qnamaker:init', () => {
4848
.command(['qnamaker:init'])
4949
.do(async output => {
5050
let config = await fs.readJSON(getConfigFile())
51-
expect(config.qnamaker.subscriptionKey).to.contain('abcdefg')
51+
expect(config.qnamaker__subscriptionKey).to.contain('abcdefg')
5252
})
5353
.it('Creates .qna.rc file')
5454
})

0 commit comments

Comments
 (0)