Allow 'balena push <deviceIpAddress>' when not logged in to balenaCloud.

Change-type: patch
Signed-off-by: Paulo Castro <paulo@balena.io>
This commit is contained in:
Paulo Castro 2019-03-25 15:16:23 +00:00
parent 0fac8d8d3b
commit 6b21f5aa5a
3 changed files with 25 additions and 20 deletions

View File

@ -138,7 +138,6 @@ export const push: CommandDefinition<
$ balena push 10.0.0.1 --source <source directory> $ balena push 10.0.0.1 --source <source directory>
$ balena push 10.0.0.1 -s <source directory> $ balena push 10.0.0.1 -s <source directory>
`, `,
permission: 'user',
options: [ options: [
{ {
signature: 'source', signature: 'source',
@ -172,7 +171,10 @@ export const push: CommandDefinition<
const Bluebird = await import('bluebird'); const Bluebird = await import('bluebird');
const remote = await import('../utils/remote-build'); const remote = await import('../utils/remote-build');
const deviceDeploy = await import('../utils/device/deploy'); 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 { parseRegistrySecrets } = await import('../utils/compose_ts');
const { BuildError } = await import('../utils/device/errors'); const { BuildError } = await import('../utils/device/errors');
@ -194,6 +196,7 @@ export const push: CommandDefinition<
switch (buildTarget) { switch (buildTarget) {
case BuildTarget.Cloud: case BuildTarget.Cloud:
const app = appOrDevice; const app = appOrDevice;
await exitIfNotLoggedIn();
await Bluebird.join( await Bluebird.join(
sdk.auth.getToken(), sdk.auth.getToken(),
sdk.settings.get('balenaUrl'), sdk.settings.get('balenaUrl'),

View File

@ -75,7 +75,7 @@ actions = require('./actions')
errors = require('./errors') errors = require('./errors')
events = require('./events') events = require('./events')
update = require('./utils/update') update = require('./utils/update')
{ exitWithExpectedError } = require('./utils/patterns') { exitIfNotLoggedIn } = require('./utils/patterns')
# Assign bluebird as the global promise library # Assign bluebird as the global promise library
# stream-to-promise will produce native promises if not # stream-to-promise will produce native promises if not
@ -84,17 +84,8 @@ update = require('./utils/update')
require('any-promise/register/bluebird') require('any-promise/register/bluebird')
capitano.permission 'user', (done) -> capitano.permission 'user', (done) ->
balena = BalenaSdk.fromSharedOptions() exitIfNotLoggedIn()
balena.auth.isLoggedIn().then (isLoggedIn) -> .then(done, done)
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)
capitano.command capitano.command
signature: '*' signature: '*'

View File

@ -14,8 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import BalenaSdk = require('balena-sdk'); import BalenaSdk = require('balena-sdk');
import Promise = require('bluebird'); import Bluebird = require('bluebird');
import chalk from 'chalk'; import chalk from 'chalk';
import { stripIndent } from 'common-tags';
import _ = require('lodash'); import _ = require('lodash');
import _form = require('resin-cli-form'); import _form = require('resin-cli-form');
import _visuals = require('resin-cli-visuals'); 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 getForm = _.once((): typeof _form => require('resin-cli-form'));
const getVisuals = _.once((): typeof _visuals => require('resin-cli-visuals')); const getVisuals = _.once((): typeof _visuals => require('resin-cli-visuals'));
export function authenticate(options: {}): Promise<void> { export function authenticate(options: {}): Bluebird<void> {
const balena = getBalenaSdk(); const balena = getBalenaSdk();
return getForm() return getForm()
.run( .run(
@ -75,6 +76,16 @@ export function authenticate(options: {}): Promise<void> {
}); });
} }
export async function exitIfNotLoggedIn(): Promise<void> {
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() { export function askLoginType() {
return getForm().ask({ return getForm().ask({
message: 'How would you like to login?', message: 'How would you like to login?',
@ -122,7 +133,7 @@ export function confirm(
message: string, message: string,
yesMessage?: string, yesMessage?: string,
) { ) {
return Promise.try(function() { return Bluebird.try(function() {
if (yesOption) { if (yesOption) {
if (yesMessage) { if (yesMessage) {
console.log(yesMessage); console.log(yesMessage);
@ -219,7 +230,7 @@ export function awaitDevice(uuid: string) {
`Waiting for ${deviceName} to come online`, `Waiting for ${deviceName} to come online`,
); );
const poll = (): Promise<void> => { const poll = (): Bluebird<void> => {
return balena.models.device.isOnline(uuid).then(function(isOnline) { return balena.models.device.isOnline(uuid).then(function(isOnline) {
if (isOnline) { if (isOnline) {
spinner.stop(); spinner.stop();
@ -230,7 +241,7 @@ export function awaitDevice(uuid: string) {
// not start again if it was already started // not start again if it was already started
spinner.start(); 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<T>( export function selectFromList<T>(
message: string, message: string,
choices: Array<T & { name: string }>, choices: Array<T & { name: string }>,
): Promise<T> { ): Bluebird<T> {
return getForm().ask({ return getForm().ask({
message, message,
type: 'list', type: 'list',