Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit c0e555d

Browse files
committed
fix for -h in headless mode; was always returning top-level help
Fixes #764
1 parent 687f686 commit c0e555d

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

app/content/js/usage-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const format = (message, options={}) => {
136136
// the return value will be `result`; we will populate it with
137137
// those fields now; `body` is the flex-wrap portion of the
138138
// content
139-
const resultWrapper = div(undefined, 'fade-in'),
139+
const resultWrapper = div(undefined, 'fade-in usage-error-wrapper'),
140140
result = div(undefined, options.noHide ? '' : 'hideable'),
141141
body = div(),
142142
left = div(), // usage and detailedExample

app/headless.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
const debug = require('debug')('headless')
1818
debug('starting')
1919

20+
// by default, we'll exit with an exit code of 0 when success is
21+
// called; this bit is necessary, as process.exit doesn't seem to
22+
// really exit the first time
23+
let exitCode = 0
24+
2025
// electron pops up a window by default, for uncaught exceptions
2126
process.on('uncaughtException', err => {
2227
console.error(err.red)
@@ -63,6 +68,8 @@ let noAuth = false
6368
function mimicDom(app, { createWindow }, localStorage) {
6469
debug('mimicDom')
6570

71+
const { quit } = app
72+
6673
try {
6774
global.localStorage = Store(app)
6875
} catch (err) {
@@ -152,7 +159,7 @@ function mimicDom(app, { createWindow }, localStorage) {
152159
showEntity: entity => print(entity),
153160
maybeHideEntity: () => false,
154161
ok: () => dom0(),
155-
oops: () => failure,
162+
oops: () => failure(quit),
156163
oopsMessage: err => {
157164
return (err && err.error && err.error.response && err.error.response.result && err.error.response.result.error && err.error.response.result.error.error) // feed creation error. nice
158165
|| (err && err.error && err.error.response && err.error.response.result && err.error.response.result.error)
@@ -230,11 +237,11 @@ function mimicDom(app, { createWindow }, localStorage) {
230237
.then(resolve)
231238
.catch(reject);
232239
} catch (err) {
233-
failure(err); reject(err)
240+
failure(quit)(err); reject(err)
234241
}
235242
})
236243
} catch (err) {
237-
failure(err); reject(err)
244+
failure(quit)(err); reject(err)
238245
}
239246
})
240247

@@ -424,12 +431,20 @@ const print = (msg, logger=log, stream=process.stdout, color='reset', ok='ok') =
424431

425432
if (msg._isFakeDom) {
426433
// msg is a DOM facade
434+
435+
if (msg.className.indexOf('usage-error-wrapper') >= 0) {
436+
// print usage errors to stdout
437+
stream = process.stdout
438+
}
439+
427440
prettyDom(msg, logger, stream, color)
428441
logger()
429442

430443
} else if (msg.then) {
431444
// msg is a promise; resolve it and try again
432-
msg.then(msg => print(msg, logger, stream, color, ok))
445+
return msg.then(msg => {
446+
return print(msg, logger, stream, color, ok)
447+
})
433448

434449
} else if (msg.message && msg.message._isFakeDom) {
435450
// msg.message is a DOM facade
@@ -505,14 +520,17 @@ const success = quit => out => {
505520

506521
if (!graphicalShellIsOpen) {
507522
quit()
508-
process.exit(0)
523+
process.exit(exitCode)
509524
} else {
510525
//log('The graphical shell should now be open. This process will stay alive till you close the window.'.red)
511526
//log('You may background this process, but do not kill it, unless you also want to kill the graphical shell.'.red)
512527
}
513528
}
514-
const failure = err => {
529+
const failure = quit => err => {
515530
debug('failure', err)
531+
532+
let completion = Promise.resolve()
533+
516534
if (!noAuth) {
517535
// we're not in a corner case of having no openwhisk auth, so
518536
// print the error
@@ -521,18 +539,22 @@ const failure = err => {
521539
if (typeof msg === 'string') {
522540
error(msg.red)
523541
} else {
524-
print(msg, error, process.stderr, 'red', 'error')
542+
completion = print(msg, error, process.stderr, 'red', 'error') || Promise.resolve()
525543
}
526544
} else {
527545
error(`No wskprops file was found. Consider trying again with "fsh help" command.`)
528546
}
529547

530-
if (!graphicalShellIsOpen) {
531-
// if the graphical shell isn't open, then we're done here
532-
process.exit(1)
533-
}
548+
return completion.then(() => {
549+
if (!graphicalShellIsOpen) {
550+
// if the graphical shell isn't open, then we're done here
551+
exitCode = 1
552+
process.exit(1)
553+
if (quit) quit()
554+
}
534555

535-
return false
556+
return false
557+
})
536558
}
537559

538560
/**
@@ -545,13 +567,7 @@ const onlyOpts = argv => !argv.find(_ => _.charAt(0) !== '-')
545567
* Insufficient arguments provided?
546568
*
547569
*/
548-
const insufficientArgs = () => argv.length === 0 || onlyOpts(argv) || argv.find(_ => _ === '--help' || _ === '-h')
549-
550-
/**
551-
* Print usage information, if the command line arguments are insufficient
552-
*
553-
*/
554-
const usage = () => repl.qexec('help')
570+
const insufficientArgs = () => argv.length === 0
555571

556572
/**
557573
* Initialize headless mode
@@ -610,9 +626,9 @@ const main = (app, mainFunctions) => {
610626
debug('delayed namespace loading')
611627
return namespace.init()
612628
.then(() => eval(cmd))
613-
.catch(failure)
629+
.catch(failure(quit))
614630
} else {
615-
return failure(err)
631+
return failure(quit)(err)
616632
}
617633
}
618634

@@ -628,7 +644,7 @@ const main = (app, mainFunctions) => {
628644
debug('exiting, no command')
629645
process.exit(0)
630646
}
631-
}).catch(failure)
647+
}).then(success(quit)).catch(failure(quit))
632648
}
633649

634650
exports.main = main

tests/tests/passes/02/headless.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const cli = {
2828

2929
exec(command, (err, stdout, stderr) => {
3030
if (err) {
31-
resolve({ code: err.code, output: stderr })
31+
resolve({ code: err.code, output: stdout.trim().concat(stderr) })
3232
} else {
33-
resolve({ code: 0, output: stdout })
33+
resolve({ code: 0, output: stdout, stderr })
3434
}
3535
})
3636
}),
@@ -101,6 +101,10 @@ describe('Headless mode', function() {
101101
.then(cli.expectError(1, 'Shell Docs / OpenWhisk'))
102102
.catch(common.oops(this)))
103103

104+
it('should show help for app ls -h', () => cli.do('app ls -h')
105+
.then(cli.expectError(1, 'Shell Docs / Composer / CRUD Operations / List compositions'))
106+
.catch(common.oops(this)))
107+
104108
const listers = ['action list', 'wsk action list', 'ls']
105109
listers.forEach(ls => {
106110
it(`should show empty ${ls}`, () => cli.do(ls)

0 commit comments

Comments
 (0)