balena-cli
Version:
The official balena Command Line Interface
128 lines • 4.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRelease = getRelease;
exports.$getRelease = $getRelease;
exports.getDevice = getDevice;
exports.resolveDeviceUuidParam = resolveDeviceUuidParam;
exports.resolveDeviceUuidsParam = resolveDeviceUuidsParam;
exports.getApplication = getApplication;
exports.getFleetSlug = getFleetSlug;
exports.getOwnOrganizations = getOwnOrganizations;
const validation_1 = require("./validation");
async function getRelease(commitOrIdOrRawVersion, options) {
const { getBalenaSdk } = await Promise.resolve().then(() => require('./lazy'));
const sdk = getBalenaSdk();
return $getRelease(sdk, commitOrIdOrRawVersion, options);
}
async function $getRelease(sdk, commitOrIdOrRawVersion, options) {
if (typeof commitOrIdOrRawVersion === 'string' &&
commitOrIdOrRawVersion.length > 0) {
const releases = (await sdk.pine.get({
resource: 'release',
options: sdk.utils.mergePineOptions({
$filter: {
commit: { $startswith: commitOrIdOrRawVersion },
},
}, options),
}));
if (releases.length === 0) {
throw new sdk.errors.BalenaReleaseNotFound(commitOrIdOrRawVersion);
}
if (releases.length > 1) {
throw new sdk.errors.BalenaAmbiguousRelease(commitOrIdOrRawVersion);
}
return releases[0];
}
return await sdk.models.release.get(commitOrIdOrRawVersion, options);
}
const isFullUuid = (v) => typeof v === 'string' && (0, validation_1.validateLongUuid)(v);
async function getDevice(uuidOrId, options) {
const { getBalenaSdk } = await Promise.resolve().then(() => require('./lazy'));
const sdk = getBalenaSdk();
if (typeof uuidOrId === 'string' &&
uuidOrId.length > 0 &&
!isFullUuid(uuidOrId)) {
const devices = (await sdk.pine.get({
resource: 'device',
options: sdk.utils.mergePineOptions({
$filter: {
uuid: { $startswith: uuidOrId },
},
}, options),
}));
if (devices.length > 1) {
throw new sdk.errors.BalenaAmbiguousDevice(uuidOrId);
}
const device = devices[0];
if (device == null) {
throw new sdk.errors.BalenaDeviceNotFound(uuidOrId);
}
return device;
}
return await sdk.models.device.get(uuidOrId, options);
}
async function resolveDeviceUuidParam(uuidOrId) {
return isFullUuid(uuidOrId)
? uuidOrId
: (await getDevice(uuidOrId, { $select: 'uuid' })).uuid;
}
async function resolveDeviceUuidsParam(uuids) {
return await Promise.all(uuids.map(resolveDeviceUuidParam));
}
async function getApplication(sdk, nameOrSlugOrId, options) {
const { looksLikeFleetSlug } = await Promise.resolve().then(() => require('./validation'));
const whoamiResult = await sdk.auth.whoami();
const isDeviceActor = (whoamiResult === null || whoamiResult === void 0 ? void 0 : whoamiResult.actorType) === 'device';
if (isDeviceActor) {
const $filterByActor = {
$filter: {
owns__device: {
$any: {
$alias: 'd',
$expr: {
d: {
actor: whoamiResult.id,
},
},
},
},
},
};
options = options
? sdk.utils.mergePineOptions(options, $filterByActor)
: $filterByActor;
}
if (typeof nameOrSlugOrId === 'string' &&
!looksLikeFleetSlug(nameOrSlugOrId)) {
return await sdk.models.application.getAppByName(nameOrSlugOrId, options, isDeviceActor ? undefined : 'directly_accessible');
}
const getFunction = isDeviceActor
? sdk.models.application.get
: sdk.models.application.getDirectlyAccessible;
return getFunction(nameOrSlugOrId, options);
}
async function getFleetSlug(sdk, nameOrSlug) {
const { looksLikeFleetSlug } = await Promise.resolve().then(() => require('./validation'));
if (!looksLikeFleetSlug(nameOrSlug)) {
return (await getApplication(sdk, nameOrSlug, { $select: 'slug' })).slug;
}
return nameOrSlug.toLowerCase();
}
async function getOwnOrganizations(sdk, options) {
return await sdk.models.organization.getAll(sdk.utils.mergePineOptions({
$filter: {
organization_membership: {
$any: {
$alias: 'orm',
$expr: {
orm: {
user: (await sdk.auth.getUserInfo()).id,
},
},
},
},
},
$orderby: { name: 'asc' },
}, options));
}
//# sourceMappingURL=sdk.js.map