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

Commit 14b8ff8

Browse files
author
JSpru
authored
Adding luis:application:list cmd (#371)
* Adding luis:application:list cmd * Updating description * Update desc * Increase timeout * Update test * Add flags type
1 parent 8d90555 commit 14b8ff8

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*!
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
import {CLIError, Command, flags} from '@microsoft/bf-cli-command'
7+
8+
const utils = require('../../../utils/index')
9+
10+
export default class LuisApplicationList extends Command {
11+
static description = 'Lists all applications on LUIS service.'
12+
13+
static examples = [`
14+
$ bf luis:application:list --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --take 3
15+
$ bf luis:application:list --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --out {PATH_TO_JSON_FILE}
16+
`]
17+
18+
static flags: any = {
19+
help: flags.help({char: 'h'}),
20+
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
21+
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
22+
out: flags.string({char: 'o', description: 'Path to the directory where the exported file will be placed.'}),
23+
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file', default: false}),
24+
skip: flags.string({description: 'The number of entries to skip. The default is 0 (no skips)'}),
25+
take: flags.string({description: 'The number of etnries to return. The maximum page size is 500. The default is 100.'}),
26+
}
27+
28+
async run() {
29+
const {flags} = this.parse(LuisApplicationList)
30+
const flagLabels = Object.keys(LuisApplicationList.flags)
31+
const configDir = this.config.configDir
32+
const options: any = {}
33+
34+
let {endpoint, subscriptionKey, force, out, skip, take} = await utils.processInputs(flags, flagLabels, configDir)
35+
36+
const requiredProps = {endpoint, subscriptionKey}
37+
utils.validateRequiredProps(requiredProps)
38+
39+
const client = utils.getLUISClient(subscriptionKey, endpoint)
40+
41+
if (skip) options.skip = parseInt(skip, 10)
42+
if (take) options.take = parseInt(take, 10)
43+
44+
try {
45+
const appList = await client.apps.list(options, undefined)
46+
if (out) {
47+
const writtenFilePath: string = await utils.writeToFile(out, appList, force)
48+
this.log(`\nList successfully written to file: ${writtenFilePath}`)
49+
} else {
50+
await utils.writeToConsole(appList)
51+
this.log('\nList successfully output to console')
52+
}
53+
} catch (err) {
54+
throw new CLIError(`Failed to export application list: ${err}`)
55+
}
56+
}
57+
58+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {expect, test} from '@oclif/test'
2+
const sinon = require('sinon')
3+
const uuidv1 = require('uuid/v1')
4+
const utils = require('../../../../src/utils/index')
5+
const fs = require('fs-extra')
6+
import * as rimraf from 'rimraf'
7+
8+
describe('luis:application:list', () => {
9+
10+
before(() => {
11+
fs.mkdirSync('./testout');
12+
});
13+
14+
after(() => {
15+
rimraf('./testout', (err) => {
16+
if (err) console.log(err);
17+
})
18+
});
19+
20+
beforeEach(() => {
21+
sinon.stub(utils, 'processInputs').returnsArg(0)
22+
})
23+
24+
afterEach(() => {
25+
sinon.restore();
26+
});
27+
28+
test
29+
.stdout()
30+
.command(['luis:application:list', '--help'])
31+
.it('should print the help contents when --help is passed as an argument', ctx => {
32+
expect(ctx.stdout).to.contain('Lists all applications on LUIS service.')
33+
})
34+
35+
test
36+
.stdout()
37+
.stderr()
38+
.command(['luis:application:list', '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
39+
.it('displays an error if any required input parameters are missing', ctx => {
40+
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
41+
})
42+
43+
test
44+
.nock('https://westus.api.cognitive.microsoft.com', api => api
45+
.get(uri => uri.includes('apps'))
46+
.reply(200, {name: 'testapp'})
47+
)
48+
.stdout()
49+
.command(['luis:application:list', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
50+
.it('displays a list of applications', ctx => {
51+
expect(ctx.stdout).to.contain('List successfully output to console')
52+
})
53+
54+
test
55+
.nock('https://westus.api.cognitive.microsoft.com', api => api
56+
.get(uri => uri.includes('apps'))
57+
.reply(200, {name: 'testapp'})
58+
)
59+
.stdout()
60+
.command(['luis:application:list', '--out', './testout/test.json', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
61+
.it('export a list of applications to the specified file', ctx => {
62+
expect(ctx.stdout).to.contain('List successfully written to file')
63+
expect(ctx.stdout).to.contain('test.json')
64+
})
65+
66+
test
67+
.nock('https://westus.api.cognitive.microsoft.com', api => api
68+
.get(uri => uri.includes('apps'))
69+
.reply(200, {name: 'testapp'})
70+
)
71+
.stdout()
72+
.stderr()
73+
.command(['luis:application:list', '--out', 'xyz', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
74+
.it('displays a list of applications and a success message in the console (since the target path provided is invalid)', ctx => {
75+
expect(ctx.stderr).to.contain('Target directory path doesn\'t exist:')
76+
})
77+
78+
})

0 commit comments

Comments
 (0)