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

Commit

Permalink
Adding config:set command and standardizing set behavior (#542)
Browse files Browse the repository at this point in the history
* Adding command to add keys and values to config file

* Adding flag to specify a key to show

* Removing prompting from set:luis

* Fixing config README.md
  • Loading branch information
munozemilio authored Feb 5, 2020
1 parent 6207a61 commit 353ac80
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 49 deletions.
59 changes: 58 additions & 1 deletion packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
# Commands
<!-- commands -->
* [`bf config`](#bf-config)
* [`bf config:set`](#bf-configset)
* [`bf config:set:luis`](#bf-configsetluis)
* [`bf config:set:qnamaker`](#bf-configsetqnamaker)
* [`bf config:set:telemetry`](#bf-configsettelemetry)
* [`bf config:show`](#bf-configshow)
* [`bf config:show:luis`](#bf-configshowluis)
* [`bf config:show:qnamaker`](#bf-configshowqnamaker)
* [`bf config:show:telemetry`](#bf-configshowtelemetry)

Expand All @@ -29,6 +32,45 @@ OPTIONS

_See code: [src/commands/config/index.ts](https://github.com/microsoft/botframework-cli/tree/master/packages/config/src/commands/config/index.ts)_

## `bf config:set`

Adds the specified key and value to the config file

```
USAGE
$ bf config:set
OPTIONS
-h, --help config:set help
-k, --key=key (required) Name of the key to add or override
-v, --value=value (required) Value associated with the specified key
```

_See code: [src/commands/config/set.ts](https://github.com/microsoft/botframework-cli/tree/master/packages/config/src/commands/config/set.ts)_

## `bf config:set:luis`

Stores default LUIS application values in global config.

```
USAGE
$ bf config:set:luis
OPTIONS
-h, --help show CLI help
--appId=appId LUIS application Id
--endpoint=endpoint LUIS application endpoint hostname, ex: <region>.api.cognitive.microsoft.com
--subscriptionKey=subscriptionKey LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)
--versionId=versionId LUIS version Id
EXAMPLE
$ bf config:set:luis --appId {APPLICATION_ID} --subscriptionKey {SUBSCRIPTION_KEY} --versionId {VERSION_ID}
--endpoint {ENDPOINT}
```

_See code: [src/commands/config/set/luis.ts](https://github.com/microsoft/botframework-cli/tree/master/packages/config/src/commands/config/set/luis.ts)_

## `bf config:set:qnamaker`

Set the QnAMaker config data
Expand Down Expand Up @@ -81,11 +123,26 @@ USAGE
$ bf config:show
OPTIONS
-h, --help config:show help
-h, --help config:show help
-k, --key=key Shows specific key value
```

_See code: [src/commands/config/show.ts](https://github.com/microsoft/botframework-cli/tree/master/packages/config/src/commands/config/show.ts)_

## `bf config:show:luis`

Display LUIS settings

```
USAGE
$ bf config:show:luis
OPTIONS
-h, --help config:show:luis help
```

_See code: [src/commands/config/show/luis.ts](https://github.com/microsoft/botframework-cli/tree/master/packages/config/src/commands/config/show/luis.ts)_

## `bf config:show:qnamaker`

Display QnAMaker settings
Expand Down
25 changes: 25 additions & 0 deletions packages/config/src/commands/config/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import {Command, flags} from '@microsoft/bf-cli-command'
import {getConfigFile, writeConfigFile, Config} from './../../utils/configfilehandler'

export default class ConfigSet extends Command {
static description = 'Adds the specified key and value to the config file'

static flags: flags.Input<any> = {
key: flags.string({char: 'k', description: 'Name of the key to add or override', required: true}),
value: flags.string({char: 'v', description: 'Value associated with the specified key', required: true}),
help: flags.help({char: 'h', description: 'config:set help'})
}

async run() {
const {flags} = this.parse(ConfigSet)
let userConfig: Config = await getConfigFile(this.config.configDir)
userConfig[flags.key] = flags.value
await writeConfigFile(this.config.configDir, userConfig)
this.log(`${flags.key} set to ${flags.value}`)
}
}
35 changes: 9 additions & 26 deletions packages/config/src/commands/config/set/luis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import {Command, flags} from '@microsoft/bf-cli-command'
const {cli} = require('cli-ux')
import {getConfigFile, writeConfigFile, Config} from '../../../utils/configfilehandler'

export default class ConfigSetLuis extends Command {
Expand All @@ -20,43 +19,27 @@ export default class ConfigSetLuis extends Command {
appId: flags.string({description: 'LUIS application Id'}),
versionId: flags.string({description: 'LUIS version Id'}),
endpoint: flags.string({description: 'LUIS application endpoint hostname, ex: <region>.api.cognitive.microsoft.com'}),
force: flags.boolean({description: 'Force save config without prompt'}),
}

async run() {
const {flags} = this.parse(ConfigSetLuis)
const configDir = this.config.configDir

if (Object.entries(flags).length === 0 && flags.constructor === Object) {
return this.log('No config settings specified')
}

try {
const response = await this.saveConfig(flags, configDir)
if (response) return this.log('Config settings saved')
return this.log('Unable to save config settings')
} catch (err) {
this.log(`Unable to save config settings: ${err}`)
this._help()
}
let userConfig: Config = await getConfigFile(this.config.configDir)
await this.saveConfig(flags, userConfig)
await writeConfigFile(this.config.configDir, userConfig)
}

async saveConfig(flags: any, configPath: string) {
async saveConfig(flags: any, userConfig: any) {
const configPrefix = 'luis__'
let saveConfigOptIn = false
let userConfig: Config = await getConfigFile(configPath)
if (!flags.force) {
saveConfigOptIn = await cli.confirm('Would you like to save the provided values to your global config file? (Y/N)')
}
if (saveConfigOptIn || flags.force) {
const flagLabels = Object.keys(flags)
flagLabels.map(label => {
if (label !== 'force') userConfig[`${configPrefix}${label}`] = flags[label]
})
await writeConfigFile(configPath, userConfig)
return true
}
return false
const flagLabels = Object.keys(flags)
flagLabels.map(label => {
userConfig[`${configPrefix}${label}`] = flags[label]
this.log(`${label} set to ${flags[label]}`)
})
}

}
10 changes: 9 additions & 1 deletion packages/config/src/commands/config/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ export default class ConfigShow extends Command {
static description = 'Displays the config file'

static flags: any = {
key: flags.string({char: 'k', description: 'Shows specific key value'}),
help: flags.help({char: 'h', description: 'config:show help'})
}

async run() {
const {flags} = this.parse(ConfigShow)
const userConfig: Config = await getConfigFile(this.config.configDir)
this.log(JSON.stringify(userConfig, null, 2))
let value
if (flags.key) {
value = userConfig[flags.key] ? userConfig[flags.key] : 'undefined'
} else {
value = userConfig
}
this.log(JSON.stringify(value, null, 2))
}
}
30 changes: 30 additions & 0 deletions packages/config/test/commands/config/set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {expect, test} from '@oclif/test'
import {initTestConfigFile, deleteTestConfigFile, getConfigFile} from './../../configfilehelper'

const fs = require('fs-extra')

describe('config:set', () => {
before(async function() {
await initTestConfigFile()
});

after(async function() {
await deleteTestConfigFile()
});

test
.stdout()
.command(['config:set', '--key', 'a', '--value', 'b'])
.it('Adds a value in config file', async ctx => {
let config = await fs.readJSON(getConfigFile())
expect(config.a).to.contain('b')
})

test
.stdout()
.command(['config:set', '--key', 'a', '--value', 'c'])
.it('Modifies a value in config file', async ctx => {
let config = await fs.readJSON(getConfigFile())
expect(config.a).to.contain('c')
})
})
35 changes: 14 additions & 21 deletions packages/config/test/commands/config/set/luis.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {expect, test} from '@oclif/test'
import ConfigSetLuis from '../../../../src/commands/config/set/luis'
const sinon = require('sinon')
import {initTestConfigFile, deleteTestConfigFile, getConfigFile} from './../../../configfilehelper'
const fs = require('fs-extra')

describe('config:set:luis', () => {

beforeEach(() => {
sinon.stub(ConfigSetLuis.prototype, 'saveConfig').returns(true)
})

afterEach(() => {
sinon.restore();
});
describe('config:set:luis', () => {
before(async function() {
await initTestConfigFile()
});

after(async function() {
await deleteTestConfigFile()
});

test
.stdout()
.command(['config:set:luis', '--help'])
Expand All @@ -31,16 +30,10 @@ describe('config:set:luis', () => {
.stdout()
.stderr()
.command(['config:set:luis', '--appId', '9999'])
.it('displays an message indication values saved successfully', ctx => {
expect(ctx.stdout).to.contain('Config settings saved')
})

test
.stdout()
.stderr()
.command(['config:set:luis', '--appId', '8888', '--force'])
.it('displays an message indication values saved successfully when force flag present', ctx => {
expect(ctx.stdout).to.contain('Config settings saved')
.it('displays an message indication values saved successfully', async ctx => {
let config = await fs.readJSON(getConfigFile())
expect(config.luis__appId).to.contain('9999')
expect(ctx.stdout).to.contain('appId set to 9999')
})

})
8 changes: 8 additions & 0 deletions packages/config/test/commands/config/show.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ describe('config:show', () => {
.it('Displays config file data', ctx => {
expect(ctx.stdout).to.contain('"qnamaker__subscriptionKey": "222222cccccctttttth223kk3k33"')
})

test
.stdout()
.command(['config:show', '--key', 'qnamaker__subscriptionKey'])
.it('Displays config file data', ctx => {
expect(ctx.stdout).to.contain('"222222cccccctttttth223kk3k33"')
})
})


0 comments on commit 353ac80

Please sign in to comment.