This repository has been archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding luis:version:export cmd (#356)
* Adding luis:version:export cmd * Remove dependency * Update example * Only store certain values in config, per specs * Refactor / keep logging and flag parsing in cmd only * Refactor write file error handling
- Loading branch information
Showing
4 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/*! | ||
* 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 LuisVersionExport extends Command { | ||
static description = 'Exports a LUIS application to JSON format' | ||
|
||
static examples = [` | ||
$ bf luis:version:export --appId {APP_ID} --versionId {VERSION_ID} --out {FILENAME.json or PATH/FILENAME.json} --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} | ||
`] | ||
|
||
static flags = { | ||
help: flags.help({char: 'h'}), | ||
appId: flags.string({description: 'LUIS application Id'}), | ||
versionId: flags.string({description: 'LUIS application version Id'}), | ||
out: flags.string({char: 'o', description: 'Path to the directory where the exported file will be placed.'}), | ||
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file', default: false}), | ||
endpoint: flags.string({description: 'LUIS endpoint hostname'}), | ||
subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (aka Ocp-Apim-Subscription-Key)'}), | ||
} | ||
|
||
async run() { | ||
const {flags} = this.parse(LuisVersionExport) | ||
const flagLabels = Object.keys(LuisVersionExport.flags) | ||
const configDir = this.config.configDir | ||
|
||
let { | ||
appId, | ||
versionId, | ||
endpoint, | ||
force, | ||
out, | ||
subscriptionKey, | ||
} = await utils.processInputs(flags, flagLabels, configDir) | ||
|
||
const requiredProps = {appId, versionId, endpoint, subscriptionKey} | ||
utils.validateRequiredProps(requiredProps) | ||
|
||
const client = utils.getLUISClient(subscriptionKey, endpoint) | ||
|
||
try { | ||
const appJSON = await client.versions.exportMethod(appId, versionId) | ||
if (!appJSON) throw new CLIError('Failed to export file') | ||
if (out) { | ||
const writtenFilePath: string = await utils.writeToFile(out, appJSON, force) | ||
this.log(`File successfully written: ${writtenFilePath}`) | ||
} else { | ||
await utils.writeToConsole(appJSON) | ||
this.log('App successfully exported\n') | ||
} | ||
} catch (error) { | ||
throw new CLIError(error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
packages/luis/test/commands/luis/version/export.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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:version:export', () => { | ||
|
||
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:version:export', '--help']) | ||
.it('should print the help contents when --help is passed as an argument', ctx => { | ||
expect(ctx.stdout).to.contain('Exports a LUIS application to JSON format') | ||
}) | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.command(['luis:version:export', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--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 'appId' missing.`) | ||
}) | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.command(['luis:version:export', '--appId', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1()]) | ||
.it('displays an error if any required input parameters are missing', ctx => { | ||
expect(ctx.stderr).to.contain(`Required input property 'versionId' missing.`) | ||
}) | ||
|
||
test | ||
.nock('https://westus.api.cognitive.microsoft.com', api => api | ||
.get(uri => uri.includes('export')) | ||
.reply(200, {name: 'testname'}) | ||
) | ||
.stdout() | ||
.command(['luis:version:export', '--appId', uuidv1(), '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com']) | ||
.it('exports a luis app and displays a success message and the export contents in the console', ctx => { | ||
expect(ctx.stdout).to.contain('App successfully exported') | ||
}) | ||
|
||
test | ||
.nock('https://westus.api.cognitive.microsoft.com', api => api | ||
.get(uri => uri.includes('export')) | ||
.reply(200, {name: 'testname'}) | ||
) | ||
.stdout() | ||
.command(['luis:version:export', '--appId', uuidv1(), '--out', './testout/test.json', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com']) | ||
.it('exports a luis app, displays a success message in the console and the export contents to the specified file', ctx => { | ||
expect(ctx.stdout).to.contain('File successfully written:') | ||
}) | ||
|
||
test | ||
.nock('https://westus.api.cognitive.microsoft.com', api => api | ||
.get(uri => uri.includes('export')) | ||
.reply(200, {name: 'testname'}) | ||
) | ||
.stdout() | ||
.stderr() | ||
.command(['luis:version:export', '--appId', uuidv1(), '--out', 'xyz', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com']) | ||
.it('exports a luis app and displays a success message and the export contents in the console (since the target path provided is invalid)', ctx => { | ||
expect(ctx.stderr).to.contain('Target directory path doesn\'t exist:') | ||
}) | ||
|
||
test | ||
.nock('https://westus.api.cognitive.microsoft.com', api => api | ||
.get(uri => uri.includes('export')) | ||
.reply(200, {name: 'testname'}) | ||
) | ||
.stdout() | ||
.stderr() | ||
.command(['luis:version:export', '--appId', uuidv1(), '--out', './testout/test.json', '--force', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com']) | ||
.it('exports a luis app, displays a success message in the console and the export contents to the specified file, overwriting the existing file of the same name', ctx => { | ||
expect(ctx.stdout).to.contain('File successfully written') | ||
}) | ||
|
||
test | ||
.nock('https://westus.api.cognitive.microsoft.com', api => api | ||
.get(uri => uri.includes('export')) | ||
.reply(200, {name: 'testname'}) | ||
) | ||
.stdout() | ||
.command(['luis:version:export', '--appId', uuidv1(), '--out', './testout/test.json', '--versionId', '0.1', '--subscriptionKey', uuidv1(), '--endpoint', 'https://westus.api.cognitive.microsoft.com']) | ||
.it('exports a luis app, displays a success message in the console and the export contents to the specified file, incrementing the filename', ctx => { | ||
expect(ctx.stdout).to.contain('File successfully written') | ||
expect(ctx.stdout).to.contain('test(1).json') | ||
}) | ||
|
||
}) |