Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding luis:application:list cmd #371

Merged
merged 19 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/luis/src/commands/luis/application/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import {CLIError, Command, flags} from '@microsoft/bf-cli-command'

const utils = require('../../../utils/index')

export default class LuisApplicationList extends Command {
static description = 'Lists all of the user applications'

static examples = [`
$ bf luis:application:list --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --take 3
$ bf luis:application:list --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --out {PATH_TO_JSON_FILE}
`]

static flags = {
help: flags.help({char: 'h'}),
endpoint: flags.string({description: 'LUIS endpoint hostname'}),
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}),
out: flags.string({char: 'o', description: 'Path to the directory where the exported file will be placed.'}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec states: Output results to specified file in JSON format, otherwise prints to STDOUT (optional)
Which suggests a file name can also be set as output, in the other hand, for this command I don't think there is a lot of value printing this to a file. @scheyal please comment

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest we just move fwd since it's already coded...discuss removing --out (if desired) later

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set the description as the spec please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not matching yet

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry...check again now

force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file', default: false}),
skip: flags.string({description: 'The number of entries to skip. The default is 0 (no skips)'}),
take: flags.string({description: 'The number of etnries to return. The maximum page size is 500. The default is 100.'}),
}

async run() {
const {flags} = this.parse(LuisApplicationList)
const flagLabels = Object.keys(LuisApplicationList.flags)
const configDir = this.config.configDir
const options: any = {}

let {endpoint, subscriptionKey, force, out, skip, take} = await utils.processInputs(flags, flagLabels, configDir)

const requiredProps = {endpoint, subscriptionKey}
utils.validateRequiredProps(requiredProps)

const client = utils.getLUISClient(subscriptionKey, endpoint)

if (skip) options.skip = parseInt(skip, 10)
if (take) options.take = parseInt(take, 10)

try {
const appList = await client.apps.list(options, undefined)
if (out) {
const writtenFilePath: string = await utils.writeToFile(out, appList, force)
this.log(`\nList successfully written to file: ${writtenFilePath}`)
} else {
await utils.writeToConsole(appList)
this.log('\nList successfully output to console')
}
} catch (err) {
throw new CLIError(`Failed to export application list: ${err}`)
}
}

}
78 changes: 78 additions & 0 deletions packages/luis/test/commands/luis/application/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {expect, test} from '@oclif/test'
const sinon = require('sinon')
const uuidv1 = require('uuid/v1')
const utils = require('../../../../src/utils/index')
const fs = require('fs-extra')
import * as rimraf from 'rimraf'

describe('luis:application:list', () => {

before(() => {
fs.mkdirSync('./testout');
});

after(() => {
rimraf('./testout', (err) => {
if (err) console.log(err);
})
});

beforeEach(() => {
sinon.stub(utils, 'processInputs').returnsArg(0)
})

afterEach(() => {
sinon.restore();
});

test
.stdout()
.command(['luis:application:list', '--help'])
.it('should print the help contents when --help is passed as an argument', ctx => {
expect(ctx.stdout).to.contain('Lists all of the user applications')
})

test
.stdout()
.stderr()
.command(['luis:application:list', '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays an error if any required input parameters are missing', ctx => {
expect(ctx.stderr).to.contain(`Required input property 'subscriptionKey' missing.`)
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.get(uri => uri.includes('apps'))
.reply(200, {name: 'testapp'})
)
.stdout()
.command(['luis:application:list', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays a list of applications', ctx => {
expect(ctx.stdout).to.contain('List successfully output to console')
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.get(uri => uri.includes('apps'))
.reply(200, {name: 'testapp'})
)
.stdout()
.command(['luis:application:list', '--out', './testout/test.json', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('export a list of applications to the specified file', ctx => {
expect(ctx.stdout).to.contain('List successfully written to file')
expect(ctx.stdout).to.contain('test.json')
})

test
.nock('https://westus.api.cognitive.microsoft.com', api => api
.get(uri => uri.includes('apps'))
.reply(200, {name: 'testapp'})
)
.stdout()
.stderr()
.command(['luis:application:list', '--out', 'xyz', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com'])
.it('displays a list of applications and a success message in the console (since the target path provided is invalid)', ctx => {
expect(ctx.stderr).to.contain('Target directory path doesn\'t exist:')
})

})