heroku-cli-util
Advanced tools
Comparing version 3.1.0 to 3.2.0
'use strict'; | ||
let co = require('co'); | ||
let Heroku = require('heroku-client'); | ||
let errors = require('./errors'); | ||
let co = require('co'); | ||
let Heroku = require('heroku-client'); | ||
let h = require('./'); | ||
module.exports = function command (fn) { | ||
function heroku (context) { | ||
if (!context.auth.password) { | ||
return 'set `needsApp: true` on the command'; | ||
} | ||
let opts = { | ||
log: context.debug, | ||
token: context.auth.password, | ||
}; | ||
if (context.secondFactor) { | ||
opts.headers = {'Heroku-Two-Factor-Code': context.secondFactor}; | ||
} | ||
return new Heroku(opts); | ||
} | ||
module.exports = function command (options, fn) { | ||
return function (context) { | ||
if (typeof options === 'function') { | ||
fn = options; | ||
options = {}; | ||
} | ||
process.chdir(context.cwd); | ||
co(function *() { | ||
let heroku; | ||
if (context.auth.password) { | ||
heroku = new Heroku({token: context.auth.password}); | ||
} | ||
yield fn(context, heroku); | ||
}).catch(errors.errorHandler({ | ||
let handleErr = h.errorHandler({ | ||
logPath: context.herokuDir + '/error.log', | ||
stackTrace: true, | ||
})); | ||
}); | ||
let run = function () { | ||
co.wrap(fn)(context, heroku(context)) | ||
.catch(function (err) { | ||
if (err && err.body && err.body.id === 'two_factor') { | ||
h.prompt('Two-factor code', {mask: true}) | ||
.then(function (secondFactor) { | ||
if (options.preauth) { | ||
return h.preauth(context.app, heroku(context), secondFactor); | ||
} else { | ||
context.secondFactor = secondFactor; | ||
} | ||
}) | ||
.then(run) | ||
.catch(handleErr); | ||
return; | ||
} | ||
throw err; | ||
}).catch(handleErr); | ||
}; | ||
run(); | ||
}; | ||
}; |
@@ -10,3 +10,3 @@ 'use strict'; | ||
skipScheme: 'ansi-color' | ||
})(msg); | ||
})(msg || ''); | ||
} | ||
@@ -32,5 +32,2 @@ | ||
function getErrorMessage (err) { | ||
if (!err) { | ||
return "unspecified error"; | ||
} | ||
if (err.body) { | ||
@@ -70,3 +67,3 @@ // API error | ||
error(getErrorMessage(err)); | ||
if (options.stackTrace && err.stack) { | ||
if (!err.body && options.stackTrace && err.stack) { | ||
if (options.logPath) { | ||
@@ -82,4 +79,4 @@ logErr(err, options.logPath); | ||
module.exports.error = error; | ||
module.exports.warn = warn; | ||
module.exports.error = error; | ||
module.exports.warn = warn; | ||
module.exports.errorHandler = errorHandler; |
{ | ||
"name": "heroku-cli-util", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Set of helpful CLI utilities", | ||
"main": "cli.js", | ||
"main": "index.js", | ||
"repository": { | ||
@@ -7,0 +7,0 @@ "type": "git", |
'use strict'; | ||
let prompt = require('./prompt'); | ||
let util = require('./util'); | ||
function preauth (app, heroku) { | ||
return new Promise(function (fulfill, reject) { | ||
prompt.prompt('Two-factor code', {mask: true}).then(function (second_factor) { | ||
fulfill(heroku.request({ | ||
method: 'PUT', | ||
path: `/apps/${app}/pre-authorizations`, | ||
headers: { | ||
'Heroku-Two-Factor-Code': second_factor | ||
} | ||
})); | ||
}).catch(reject); | ||
function preauth (app, heroku, secondFactor) { | ||
return heroku.request({ | ||
method: 'PUT', | ||
path: `/apps/${app}/pre-authorizations`, | ||
headers: { 'Heroku-Two-Factor-Code': secondFactor } | ||
}); | ||
@@ -17,0 +10,0 @@ } |
@@ -33,3 +33,4 @@ 'use strict'; | ||
// Ctrl-c | ||
reject(); | ||
console.error(); | ||
reject('canceled'); | ||
return; | ||
@@ -36,0 +37,0 @@ default: |
@@ -153,9 +153,24 @@ # heroku-cli-util | ||
## Preauth | ||
With options: | ||
```js | ||
var h = require('heroku-cli-util'); | ||
yield h.preauth("APPNAME", heroku); | ||
let h = require('heroku-cli-util'); | ||
module.exports.commands = [ | ||
{ | ||
topic: 'apps', | ||
command: 'info', | ||
needsAuth: true, | ||
needsApp: true, | ||
run: h.command({preauth: true}, | ||
function* (context, heroku) { | ||
let app = yield heroku.apps(context.app).info(); | ||
console.dir(app); | ||
}) | ||
} | ||
]; | ||
``` | ||
If the command has a `two_factor` API error, it will ask the user for a 2fa code and retry. | ||
If you set `preauth: true` it will preauth against the current app instead of just setting the header on an app. (This is necessary if you need to do more than 1 API call that will require 2fa) | ||
## Tests | ||
@@ -162,0 +177,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20223
502
185