diff --git a/lib/actions/push.ts b/lib/actions/push.ts index ee634d01..54ed1e61 100644 --- a/lib/actions/push.ts +++ b/lib/actions/push.ts @@ -138,7 +138,6 @@ export const push: CommandDefinition< $ balena push 10.0.0.1 --source $ balena push 10.0.0.1 -s `, - permission: 'user', options: [ { signature: 'source', @@ -172,7 +171,10 @@ export const push: CommandDefinition< const Bluebird = await import('bluebird'); const remote = await import('../utils/remote-build'); const deviceDeploy = await import('../utils/device/deploy'); - const { exitWithExpectedError } = await import('../utils/patterns'); + const { + exitIfNotLoggedIn, + exitWithExpectedError, + } = await import('../utils/patterns'); const { parseRegistrySecrets } = await import('../utils/compose_ts'); const { BuildError } = await import('../utils/device/errors'); @@ -194,6 +196,7 @@ export const push: CommandDefinition< switch (buildTarget) { case BuildTarget.Cloud: const app = appOrDevice; + await exitIfNotLoggedIn(); await Bluebird.join( sdk.auth.getToken(), sdk.settings.get('balenaUrl'), diff --git a/lib/app.coffee b/lib/app.coffee index 983e3daf..72a78c31 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -75,7 +75,7 @@ actions = require('./actions') errors = require('./errors') events = require('./events') update = require('./utils/update') -{ exitWithExpectedError } = require('./utils/patterns') +{ exitIfNotLoggedIn } = require('./utils/patterns') # Assign bluebird as the global promise library # stream-to-promise will produce native promises if not @@ -84,17 +84,8 @@ update = require('./utils/update') require('any-promise/register/bluebird') capitano.permission 'user', (done) -> - balena = BalenaSdk.fromSharedOptions() - balena.auth.isLoggedIn().then (isLoggedIn) -> - if not isLoggedIn - exitWithExpectedError(''' - You have to log in to continue - - Run the following command to go through the login wizard: - - $ balena login - ''') - .nodeify(done) + exitIfNotLoggedIn() + .then(done, done) capitano.command signature: '*' diff --git a/lib/utils/patterns.ts b/lib/utils/patterns.ts index 3258dadf..f999f30c 100644 --- a/lib/utils/patterns.ts +++ b/lib/utils/patterns.ts @@ -14,8 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ import BalenaSdk = require('balena-sdk'); -import Promise = require('bluebird'); +import Bluebird = require('bluebird'); import chalk from 'chalk'; +import { stripIndent } from 'common-tags'; import _ = require('lodash'); import _form = require('resin-cli-form'); import _visuals = require('resin-cli-visuals'); @@ -28,7 +29,7 @@ const getBalenaSdk = _.once(() => BalenaSdk.fromSharedOptions()); const getForm = _.once((): typeof _form => require('resin-cli-form')); const getVisuals = _.once((): typeof _visuals => require('resin-cli-visuals')); -export function authenticate(options: {}): Promise { +export function authenticate(options: {}): Bluebird { const balena = getBalenaSdk(); return getForm() .run( @@ -75,6 +76,16 @@ export function authenticate(options: {}): Promise { }); } +export async function exitIfNotLoggedIn(): Promise { + const balena = getBalenaSdk(); + if (!(await balena.auth.isLoggedIn())) { + exitWithExpectedError(stripIndent` + You have to log in to continue + Run the following command to go through the login wizard: + $ balena login`); + } +} + export function askLoginType() { return getForm().ask({ message: 'How would you like to login?', @@ -122,7 +133,7 @@ export function confirm( message: string, yesMessage?: string, ) { - return Promise.try(function() { + return Bluebird.try(function() { if (yesOption) { if (yesMessage) { console.log(yesMessage); @@ -219,7 +230,7 @@ export function awaitDevice(uuid: string) { `Waiting for ${deviceName} to come online`, ); - const poll = (): Promise => { + const poll = (): Bluebird => { return balena.models.device.isOnline(uuid).then(function(isOnline) { if (isOnline) { spinner.stop(); @@ -230,7 +241,7 @@ export function awaitDevice(uuid: string) { // not start again if it was already started spinner.start(); - return Promise.delay(3000).then(poll); + return Bluebird.delay(3000).then(poll); } }); }; @@ -274,7 +285,7 @@ export function inferOrSelectDevice(preferredUuid: string) { export function selectFromList( message: string, choices: Array, -): Promise { +): Bluebird { return getForm().ask({ message, type: 'list',