Skip to content

Commit

Permalink
feat(builder): display nicer error message if openwhisk creds are mis…
Browse files Browse the repository at this point in the history
…sing

fixes #103
  • Loading branch information
tripodsan committed Feb 7, 2020
1 parent 999af39 commit 2a2ef39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/action_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,19 @@ module.exports = class ActionBuilder {
this.log.info(chalk`{green ok:} bundle can be loaded and has a {gray main()} function.\n`);
}

async deploy() {
const openwhisk = ow({
getOpenwhiskClient() {
if (!this._wskApiHost || !this._wskAuth || !this._wskNamespace) {
throw Error(chalk`\nMissing OpenWhisk credentials. Make sure you have a {grey .wskprops} in your home directory.\nYou can also set {grey WSK_NAMESPACE}, {gray WSK_AUTH} and {gray WSK_API_HOST} environment variables.`);
}
return ow({
apihost: this._wskApiHost,
api_key: this._wskAuth,
namespace: this._wskNamespace,
});
}

async deploy() {
const openwhisk = this.getOpenwhiskClient();
const relZip = path.relative(process.cwd(), this._zipFile);
this.log.debug(`Deploying ${relZip} as ${this._actionName} to OpenWhisk`);
const actionoptions = {
Expand Down Expand Up @@ -587,11 +593,7 @@ module.exports = class ActionBuilder {
}

async updatePackage() {
const openwhisk = ow({
apihost: this._wskApiHost,
api_key: this._wskAuth,
namespace: this._wskNamespace,
});
const openwhisk = this.getOpenwhiskClient();
let fn = openwhisk.packages.update.bind(openwhisk.packages);
let verb = 'updated';
try {
Expand Down Expand Up @@ -660,11 +662,7 @@ module.exports = class ActionBuilder {
}

async testInvoke() {
const openwhisk = ow({
apihost: this._wskApiHost,
api_key: this._wskAuth,
namespace: this._wskNamespace,
});
const openwhisk = this.getOpenwhiskClient();

this.log.info(`--: invoking: ${chalk.blueBright(this._actionName)} ...`);
try {
Expand Down Expand Up @@ -719,11 +717,7 @@ module.exports = class ActionBuilder {
}
});

const openwhisk = ow({
apihost: this._wskApiHost,
api_key: this._wskAuth,
namespace: this._wskNamespace,
});
const openwhisk = this.getOpenwhiskClient();

const annotations = [{
key: 'exec',
Expand Down
16 changes: 16 additions & 0 deletions test/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ describe('Deploy Test', () => {
await fse.remove(testRoot);
});

it('reports nice error if no wsk props are set', async () => {
await fse.copy(path.resolve(__dirname, 'fixtures', 'web-action'), testRoot);
process.chdir(testRoot); // need to change .cwd() for yargs to pickup `wsk` in package.json
const builder = new CLI()
.prepare([
'--verbose',
'--deploy',
'--directory', testRoot,
]);
builder._logger = new TestLogger();
// hack to invalidate the wsk props, if any
builder.initWskProps = () => {};

await assert.rejects(builder.run(), /Missing OpenWhisk credentials./);
});

it('deploys a web action', async () => {
await fse.copy(path.resolve(__dirname, 'fixtures', 'web-action'), testRoot);

Expand Down

0 comments on commit 2a2ef39

Please sign in to comment.