2017-12-20 21:46:01 +00:00
|
|
|
/*
|
2020-06-26 11:46:58 +00:00
|
|
|
Copyright 2016-2020 Balena Ltd.
|
2017-12-20 21:46:01 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2020-02-27 12:38:50 +00:00
|
|
|
import type * as BalenaSdk from 'balena-sdk';
|
2019-03-12 22:07:57 +00:00
|
|
|
import _ = require('lodash');
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-06-26 11:46:58 +00:00
|
|
|
import {
|
|
|
|
exitWithExpectedError,
|
|
|
|
instanceOf,
|
|
|
|
NotLoggedInError,
|
|
|
|
ExpectedError,
|
|
|
|
} from '../errors';
|
2020-07-08 17:03:10 +00:00
|
|
|
import { getBalenaSdk, getVisuals, stripIndent, getCliForm } from './lazy';
|
2019-03-12 22:07:57 +00:00
|
|
|
import validation = require('./validation');
|
2020-07-01 14:26:40 +00:00
|
|
|
import { delay } from './helpers';
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
export function authenticate(options: {}): Promise<void> {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm()
|
2018-01-09 15:05:24 +00:00
|
|
|
.run(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
message: 'Email:',
|
|
|
|
name: 'email',
|
|
|
|
type: 'input',
|
|
|
|
validate: validation.validateEmail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
message: 'Password:',
|
|
|
|
name: 'password',
|
|
|
|
type: 'password',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
{ override: options },
|
|
|
|
)
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(balena.auth.login)
|
|
|
|
.then(balena.auth.twoFactor.isPassed)
|
2018-01-09 15:05:24 +00:00
|
|
|
.then((isTwoFactorAuthPassed: boolean) => {
|
|
|
|
if (isTwoFactorAuthPassed) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm()
|
2018-01-09 15:05:24 +00:00
|
|
|
.ask({
|
|
|
|
message: 'Two factor auth challenge:',
|
|
|
|
name: 'code',
|
|
|
|
type: 'input',
|
|
|
|
})
|
2018-10-19 14:38:50 +00:00
|
|
|
.then(balena.auth.twoFactor.challenge)
|
2018-01-09 15:05:24 +00:00
|
|
|
.catch((error: any) => {
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.auth.logout().then(() => {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (
|
2018-10-19 14:38:50 +00:00
|
|
|
error.name === 'BalenaRequestError' &&
|
2018-01-09 15:05:24 +00:00
|
|
|
error.statusCode === 401
|
|
|
|
) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new ExpectedError('Invalid two factor authentication code');
|
2018-01-09 15:05:24 +00:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
2018-01-04 16:17:43 +00:00
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
/**
|
|
|
|
* Check if logged in, and throw `NotLoggedInError` if not.
|
|
|
|
* Note: `NotLoggedInError` is an `ExpectedError`.
|
|
|
|
*/
|
2019-10-31 01:40:57 +00:00
|
|
|
export async function checkLoggedIn(): Promise<void> {
|
2019-03-25 15:16:23 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
if (!(await balena.auth.isLoggedIn())) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new NotLoggedInError(stripIndent`
|
|
|
|
Login required: use the “balena login” command to log in.
|
|
|
|
`);
|
2019-03-25 15:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
export function askLoginType() {
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask<'web' | 'credentials' | 'token' | 'register'>({
|
2017-12-20 21:46:01 +00:00
|
|
|
message: 'How would you like to login?',
|
|
|
|
name: 'loginType',
|
|
|
|
type: 'list',
|
2018-01-09 15:05:24 +00:00
|
|
|
choices: [
|
|
|
|
{
|
|
|
|
name: 'Web authorization (recommended)',
|
|
|
|
value: 'web',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Credentials',
|
|
|
|
value: 'credentials',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Authentication token',
|
|
|
|
value: 'token',
|
|
|
|
},
|
|
|
|
{
|
2018-10-19 14:38:50 +00:00
|
|
|
name: "I don't have a balena account!",
|
2018-01-09 15:05:24 +00:00
|
|
|
value: 'register',
|
|
|
|
},
|
|
|
|
],
|
2018-01-04 14:07:55 +00:00
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function selectDeviceType() {
|
2019-01-12 21:20:19 +00:00
|
|
|
return getBalenaSdk()
|
|
|
|
.models.config.getDeviceTypes()
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((deviceTypes) => {
|
2019-11-14 09:53:38 +00:00
|
|
|
deviceTypes = _.sortBy(deviceTypes, 'name').filter(
|
2020-06-15 22:53:07 +00:00
|
|
|
(dt) => dt.state !== 'DISCONTINUED',
|
2019-11-14 09:53:38 +00:00
|
|
|
);
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask({
|
2019-01-12 21:20:19 +00:00
|
|
|
message: 'Device Type',
|
|
|
|
type: 'list',
|
|
|
|
choices: _.map(deviceTypes, ({ slug: value, name }) => ({
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
})),
|
|
|
|
});
|
2018-01-04 14:07:55 +00:00
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 19:13:18 +00:00
|
|
|
/**
|
|
|
|
* Display interactive confirmation prompt.
|
|
|
|
* If the user declines, then either an error will be thrown,
|
|
|
|
* or `exitWithExpectedError` will be called (if exitIfDeclined true).
|
|
|
|
* @param yesOption - automatically confirm if true
|
|
|
|
* @param message - message to display with prompt
|
|
|
|
* @param yesMessage - message to display if automatically confirming
|
|
|
|
* @param exitIfDeclined - exitWithExpectedError when decline if true
|
|
|
|
*/
|
2020-07-01 14:26:40 +00:00
|
|
|
export async function confirm(
|
2018-10-25 12:57:45 +00:00
|
|
|
yesOption: boolean,
|
2018-01-09 15:05:24 +00:00
|
|
|
message: string,
|
2018-10-25 12:57:45 +00:00
|
|
|
yesMessage?: string,
|
2019-09-30 19:56:57 +00:00
|
|
|
exitIfDeclined = false,
|
2018-01-09 15:05:24 +00:00
|
|
|
) {
|
2020-07-01 14:26:40 +00:00
|
|
|
if (yesOption) {
|
|
|
|
if (yesMessage) {
|
|
|
|
console.log(yesMessage);
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
2020-07-01 14:26:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-08 17:03:10 +00:00
|
|
|
const confirmed = await getCliForm().ask<boolean>({
|
2020-07-01 14:26:40 +00:00
|
|
|
message,
|
|
|
|
type: 'confirm',
|
|
|
|
default: false,
|
2018-01-04 14:07:55 +00:00
|
|
|
});
|
2020-07-01 14:26:40 +00:00
|
|
|
|
|
|
|
if (!confirmed) {
|
|
|
|
const err = new Error('Aborted');
|
|
|
|
if (exitIfDeclined) {
|
|
|
|
exitWithExpectedError(err);
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:14:28 +00:00
|
|
|
export function selectApplication(
|
2020-04-25 10:47:23 +00:00
|
|
|
filter?: (app: BalenaSdk.Application) => boolean,
|
2020-07-15 14:26:31 +00:00
|
|
|
errorOnEmptySelection = false,
|
2018-10-19 11:14:28 +00:00
|
|
|
) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-01-09 15:05:24 +00:00
|
|
|
.hasAny()
|
2020-06-15 22:53:07 +00:00
|
|
|
.then(function (hasAnyApplications) {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (!hasAnyApplications) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new ExpectedError("You don't have any applications");
|
2018-01-09 15:05:24 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application.getAll();
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
|
|
|
.filter(filter || _.constant(true))
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((applications) => {
|
2020-07-15 14:26:31 +00:00
|
|
|
if (errorOnEmptySelection && applications.length === 0) {
|
|
|
|
throw new ExpectedError('No suitable applications found for selection');
|
|
|
|
}
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Select an application',
|
|
|
|
type: 'list',
|
2020-06-15 22:53:07 +00:00
|
|
|
choices: _.map(applications, (application) => ({
|
2017-12-20 21:46:01 +00:00
|
|
|
name: `${application.app_name} (${application.device_type})`,
|
2018-01-04 14:07:55 +00:00
|
|
|
value: application.app_name,
|
2018-01-09 15:05:24 +00:00
|
|
|
})),
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function selectOrCreateApplication() {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.application
|
2018-01-09 15:05:24 +00:00
|
|
|
.hasAny()
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((hasAnyApplications) => {
|
2019-03-12 22:07:57 +00:00
|
|
|
if (!hasAnyApplications) {
|
2019-05-31 18:22:40 +00:00
|
|
|
// Just to make TS happy
|
|
|
|
return Promise.resolve(undefined);
|
2019-03-12 22:07:57 +00:00
|
|
|
}
|
2018-01-09 15:05:24 +00:00
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
return balena.models.application.getAll().then((applications) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
const appOptions = _.map<
|
2018-10-19 14:38:50 +00:00
|
|
|
BalenaSdk.Application,
|
2018-01-09 15:05:24 +00:00
|
|
|
{ name: string; value: string | null }
|
2020-06-15 22:53:07 +00:00
|
|
|
>(applications, (application) => ({
|
2018-01-09 15:05:24 +00:00
|
|
|
name: `${application.app_name} (${application.device_type})`,
|
|
|
|
value: application.app_name,
|
|
|
|
}));
|
|
|
|
|
|
|
|
appOptions.unshift({
|
|
|
|
name: 'Create a new application',
|
|
|
|
value: null,
|
|
|
|
});
|
|
|
|
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Select an application',
|
|
|
|
type: 'list',
|
|
|
|
choices: appOptions,
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
2018-01-09 15:05:24 +00:00
|
|
|
})
|
2020-06-15 22:53:07 +00:00
|
|
|
.then((application) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (application) {
|
|
|
|
return application;
|
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Choose a Name for your new application',
|
|
|
|
type: 'input',
|
|
|
|
validate: validation.validateApplicationName,
|
2017-12-20 21:46:01 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
export async function awaitDevice(uuid: string) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2020-07-01 14:26:40 +00:00
|
|
|
const deviceName = await balena.models.device.getName(uuid);
|
|
|
|
const visuals = getVisuals();
|
|
|
|
const spinner = new visuals.Spinner(
|
|
|
|
`Waiting for ${deviceName} to come online`,
|
|
|
|
);
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
const poll = async (): Promise<void> => {
|
|
|
|
const isOnline = await balena.models.device.isOnline(uuid);
|
|
|
|
if (isOnline) {
|
|
|
|
spinner.stop();
|
|
|
|
console.info(`The device **${deviceName}** is online!`);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Spinner implementation is smart enough to
|
|
|
|
// not start again if it was already started
|
|
|
|
spinner.start();
|
|
|
|
|
|
|
|
await delay(3000);
|
|
|
|
await poll();
|
|
|
|
}
|
|
|
|
};
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
console.info(`Waiting for ${deviceName} to connect to balena...`);
|
|
|
|
await poll();
|
|
|
|
return uuid;
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
export async function awaitDeviceOsUpdate(
|
|
|
|
uuid: string,
|
|
|
|
targetOsVersion: string,
|
|
|
|
) {
|
2019-06-11 12:08:15 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
const deviceName = await balena.models.device.getName(uuid);
|
|
|
|
const visuals = getVisuals();
|
|
|
|
const progressBar = new visuals.Progress(
|
|
|
|
`Updating the OS of ${deviceName} to v${targetOsVersion}`,
|
|
|
|
);
|
|
|
|
progressBar.update({ percentage: 0 });
|
|
|
|
|
|
|
|
const poll = async (): Promise<void> => {
|
|
|
|
const [
|
|
|
|
osUpdateStatus,
|
|
|
|
{ overall_progress: osUpdateProgress },
|
|
|
|
] = await Promise.all([
|
|
|
|
balena.models.device.getOsUpdateStatus(uuid),
|
|
|
|
balena.models.device.get(uuid, { $select: 'overall_progress' }),
|
|
|
|
]);
|
|
|
|
if (osUpdateStatus.status === 'done') {
|
|
|
|
console.info(
|
|
|
|
`The device ${deviceName} has been updated to v${targetOsVersion} and will restart shortly!`,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-11 12:08:15 +00:00
|
|
|
|
2020-07-01 14:26:40 +00:00
|
|
|
if (osUpdateStatus.error) {
|
|
|
|
console.error(
|
|
|
|
`Failed to complete Host OS update on device ${deviceName}!`,
|
|
|
|
);
|
|
|
|
exitWithExpectedError(osUpdateStatus.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osUpdateProgress !== null) {
|
|
|
|
// Avoid resetting to 0% at end of process when device goes offline.
|
|
|
|
progressBar.update({ percentage: osUpdateProgress });
|
|
|
|
}
|
|
|
|
|
|
|
|
await delay(3000);
|
|
|
|
await poll();
|
|
|
|
};
|
|
|
|
|
|
|
|
await poll();
|
|
|
|
return uuid;
|
2019-06-11 12:08:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 21:46:01 +00:00
|
|
|
export function inferOrSelectDevice(preferredUuid: string) {
|
2019-01-12 21:20:19 +00:00
|
|
|
const balena = getBalenaSdk();
|
2018-10-19 14:38:50 +00:00
|
|
|
return balena.models.device
|
2018-01-09 15:05:24 +00:00
|
|
|
.getAll()
|
2020-06-15 22:53:07 +00:00
|
|
|
.filter<BalenaSdk.Device>((device) => device.is_online)
|
|
|
|
.then((onlineDevices) => {
|
2018-01-09 15:05:24 +00:00
|
|
|
if (_.isEmpty(onlineDevices)) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new ExpectedError("You don't have any devices online");
|
2018-01-09 15:05:24 +00:00
|
|
|
}
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-06-15 22:53:07 +00:00
|
|
|
const defaultUuid = _(onlineDevices).map('uuid').includes(preferredUuid)
|
2018-01-09 15:05:24 +00:00
|
|
|
? preferredUuid
|
|
|
|
: onlineDevices[0].uuid;
|
2017-12-20 21:46:01 +00:00
|
|
|
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask({
|
2018-01-09 15:05:24 +00:00
|
|
|
message: 'Select a device',
|
|
|
|
type: 'list',
|
|
|
|
default: defaultUuid,
|
2020-06-15 22:53:07 +00:00
|
|
|
choices: _.map(onlineDevices, (device) => ({
|
2018-10-19 11:14:28 +00:00
|
|
|
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(
|
|
|
|
0,
|
|
|
|
7,
|
|
|
|
)})`,
|
2018-01-09 15:05:24 +00:00
|
|
|
value: device.uuid,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
});
|
2017-12-20 21:46:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 10:24:44 +00:00
|
|
|
export async function getOnlineTargetUuid(
|
|
|
|
sdk: BalenaSdk.BalenaSDK,
|
|
|
|
applicationOrDevice: string,
|
|
|
|
) {
|
2019-06-26 09:51:05 +00:00
|
|
|
const Logger = await import('../utils/logger');
|
2019-09-11 18:34:43 +00:00
|
|
|
const logger = Logger.getLogger();
|
2019-06-06 10:24:44 +00:00
|
|
|
const appTest = validation.validateApplicationName(applicationOrDevice);
|
|
|
|
const uuidTest = validation.validateUuid(applicationOrDevice);
|
|
|
|
|
|
|
|
if (!appTest && !uuidTest) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new ExpectedError(
|
|
|
|
`Device or application not found: ${applicationOrDevice}`,
|
|
|
|
);
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a definite device UUID...
|
|
|
|
if (uuidTest && !appTest) {
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(
|
|
|
|
`Fetching device by UUID ${applicationOrDevice} (${typeof applicationOrDevice})`,
|
|
|
|
);
|
2020-01-20 21:21:05 +00:00
|
|
|
return (
|
|
|
|
await sdk.models.device.get(applicationOrDevice, {
|
|
|
|
$select: ['uuid'],
|
|
|
|
$filter: { is_online: true },
|
|
|
|
})
|
|
|
|
).uuid;
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, it may be a device OR an application...
|
|
|
|
try {
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(
|
|
|
|
`Fetching application by name ${applicationOrDevice} (${typeof applicationOrDevice})`,
|
|
|
|
);
|
2019-06-06 10:24:44 +00:00
|
|
|
const app = await sdk.models.application.get(applicationOrDevice);
|
|
|
|
const devices = await sdk.models.device.getAllByApplication(app.id, {
|
|
|
|
$filter: { is_online: true },
|
|
|
|
});
|
|
|
|
|
|
|
|
if (_.isEmpty(devices)) {
|
2020-06-26 11:46:58 +00:00
|
|
|
throw new ExpectedError('No accessible devices are online');
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 17:03:10 +00:00
|
|
|
return await getCliForm().ask({
|
2019-06-06 10:24:44 +00:00
|
|
|
message: 'Select a device',
|
|
|
|
type: 'list',
|
|
|
|
default: devices[0].uuid,
|
2020-06-15 22:53:07 +00:00
|
|
|
choices: _.map(devices, (device) => ({
|
2019-06-06 10:24:44 +00:00
|
|
|
name: `${device.device_name || 'Untitled'} (${device.uuid.slice(
|
|
|
|
0,
|
|
|
|
7,
|
|
|
|
)})`,
|
|
|
|
value: device.uuid,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2020-07-08 17:21:37 +00:00
|
|
|
const { BalenaApplicationNotFound } = await import('balena-errors');
|
2020-04-18 01:31:13 +00:00
|
|
|
if (!instanceOf(err, BalenaApplicationNotFound)) {
|
2019-06-06 10:24:44 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(`Application not found`);
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// it wasn't an application, maybe it's a device...
|
2019-06-26 09:51:05 +00:00
|
|
|
logger.logDebug(
|
|
|
|
`Fetching device by UUID ${applicationOrDevice} (${typeof applicationOrDevice})`,
|
|
|
|
);
|
2020-01-20 21:21:05 +00:00
|
|
|
return (
|
|
|
|
await sdk.models.device.get(applicationOrDevice, {
|
|
|
|
$select: ['uuid'],
|
|
|
|
$filter: { is_online: true },
|
|
|
|
})
|
|
|
|
).uuid;
|
2019-06-06 10:24:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:12:51 +00:00
|
|
|
export function selectFromList<T>(
|
|
|
|
message: string,
|
|
|
|
choices: Array<T & { name: string }>,
|
2020-07-01 14:26:40 +00:00
|
|
|
): Promise<T> {
|
2020-07-08 17:03:10 +00:00
|
|
|
return getCliForm().ask<T>({
|
2018-04-25 14:20:07 +00:00
|
|
|
message,
|
|
|
|
type: 'list',
|
2020-06-15 22:53:07 +00:00
|
|
|
choices: _.map(choices, (s) => ({
|
2018-04-25 14:20:07 +00:00
|
|
|
name: s.name,
|
|
|
|
value: s,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
}
|