balena-cli
Version:
The official balena Command Line Interface
211 lines (205 loc) • 9.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const _ = require("lodash");
const errors_1 = require("../../errors");
const cf = require("../../utils/common-flags");
const lazy_1 = require("../../utils/lazy");
const messages_1 = require("../../utils/messages");
class EnvListCmd extends core_1.Command {
async run() {
const { flags: options } = await this.parse(EnvListCmd);
const variables = [];
const { checkLoggedIn } = await Promise.resolve().then(() => require('../../utils/patterns'));
await checkLoggedIn();
if (!options.fleet && !options.device) {
throw new errors_1.ExpectedError('Missing --fleet or --device option');
}
const balena = (0, lazy_1.getBalenaSdk)();
let fleetSlug = options.fleet
? await (await Promise.resolve().then(() => require('../../utils/sdk'))).getFleetSlug(balena, options.fleet)
: undefined;
let fullUUID;
if (options.device) {
const { getDeviceAndMaybeAppFromUUID } = await Promise.resolve().then(() => require('../../utils/cloud'));
const [device, app] = await getDeviceAndMaybeAppFromUUID(balena, options.device, ['uuid'], ['slug']);
fullUUID = device.uuid;
if (app) {
fleetSlug = app.slug;
}
}
if (fleetSlug && options.service) {
await validateServiceName(balena, options.service, fleetSlug);
}
variables.push(...(await getAppVars(balena, fleetSlug, options)));
if (fullUUID) {
variables.push(...(await getDeviceVars(balena, fullUUID, fleetSlug, options)));
}
if (!options.json && variables.length === 0) {
const target = (options.service ? `service "${options.service}" of ` : '') +
(options.fleet
? `fleet "${options.fleet}"`
: `device "${options.device}"`);
throw new errors_1.ExpectedError(`No environment variables found for ${target}`);
}
await this.printVariables(variables, options);
}
async printVariables(varArray, options) {
const fields = ['id', 'name', 'value', 'fleet'];
varArray = varArray.map((i) => {
i.fleet || (i.fleet = options.json ? null : 'N/A');
return i;
});
if (options.device) {
fields.push(options.json ? 'deviceUUID' : 'deviceUUID => DEVICE');
}
if (!options.config) {
fields.push(options.json ? 'serviceName' : 'serviceName => SERVICE');
}
if (options.json) {
const { pickAndRename } = await Promise.resolve().then(() => require('../../utils/helpers'));
const mapped = varArray.map((o) => pickAndRename(o, fields));
this.log(JSON.stringify(mapped, null, 4));
}
else {
this.log((0, lazy_1.getVisuals)().table.horizontal(_.sortBy(varArray, (v) => v.name), fields));
}
}
}
EnvListCmd.aliases = ['envs'];
EnvListCmd.deprecateAliases = true;
EnvListCmd.description = (0, lazy_1.stripIndent) `
List the environment or config variables of a fleet, device or service.
List the environment or configuration variables of a fleet, device or
service, as selected by the respective command-line options. (A service
corresponds to a Docker image/container in a microservices fleet.)
The results include fleet-wide (multiple devices), device-specific (multiple
services on a specific device) and service-specific variables that apply to the
selected fleet, device or service. It can be thought of as including inherited
variables; for example, a service inherits device-wide variables, and a device
inherits fleet-wide variables.
The printed output may include DEVICE and/or SERVICE columns to distinguish
between fleet-wide, device-specific and service-specific variables.
An asterisk in these columns indicates that the variable applies to
"all devices" or "all services".
The --config option is used to list "configuration variables" that control
balena platform features, as opposed to custom environment variables defined
by the user. The --config and the --service options are mutually exclusive
because configuration variables cannot be set for specific services.
The --json option is recommended when scripting the output of this command,
because the JSON format is less likely to change and it better represents data
types like lists and empty strings. The 'jq' utility may be helpful in shell
scripts (https://stedolan.github.io/jq/manual/). When --json is used, an empty
JSON array ([]) is printed instead of an error message when no variables exist
for the given query. When querying variables for a device, note that the fleet
name may be null in JSON output (or 'N/A' in tabular output) if the fleet that
the device belonged to is no longer accessible by the current user (for example,
in case the current user was removed from the fleet by the fleet's owner).
${messages_1.applicationIdInfo.split('\n').join('\n\t\t')}
`;
EnvListCmd.examples = [
'$ balena env list --fleet myorg/myfleet',
'$ balena env list --fleet MyFleet --json',
'$ balena env list --fleet MyFleet --service MyService',
'$ balena env list --fleet MyFleet --config',
'$ balena env list --device 7cf02a6',
'$ balena env list --device 7cf02a6 --json',
'$ balena env list --device 7cf02a6 --config --json',
'$ balena env list --device 7cf02a6 --service MyService',
];
EnvListCmd.flags = {
fleet: { ...cf.fleet, exclusive: ['device'] },
config: core_1.Flags.boolean({
default: false,
char: 'c',
description: 'show configuration variables only',
exclusive: ['service'],
}),
device: { ...cf.device, exclusive: ['fleet'] },
json: cf.json,
service: { ...cf.service, exclusive: ['config'] },
};
exports.default = EnvListCmd;
async function validateServiceName(sdk, serviceName, fleetSlug) {
const services = await sdk.models.service.getAllByApplication(fleetSlug, {
$select: 'id',
$filter: { service_name: serviceName },
});
if (services.length === 0) {
throw new errors_1.ExpectedError(`Service "${serviceName}" not found for fleet "${fleetSlug}"`);
}
}
async function getAppVars(sdk, fleetSlug, options) {
const appVars = [];
if (!fleetSlug) {
return appVars;
}
const vars = await sdk.models.application[options.config ? 'configVar' : 'envVar'].getAllByApplication(fleetSlug);
fillInInfoFields(vars, fleetSlug);
appVars.push(...vars);
if (!options.config) {
const pineOpts = {
$expand: {
service: {},
},
};
if (options.service) {
pineOpts.$filter = {
service: {
service_name: options.service,
},
};
}
const serviceVars = await sdk.models.service.var.getAllByApplication(fleetSlug, pineOpts);
fillInInfoFields(serviceVars, fleetSlug);
appVars.push(...serviceVars);
}
return appVars;
}
async function getDeviceVars(sdk, fullUUID, fleetSlug, options) {
const printedUUID = options.json ? fullUUID : options.device;
const deviceVars = [];
if (options.config) {
const deviceConfigVars = await sdk.models.device.configVar.getAllByDevice(fullUUID);
fillInInfoFields(deviceConfigVars, fleetSlug, printedUUID);
deviceVars.push(...deviceConfigVars);
}
else {
const pineOpts = {
$expand: {
service_install: {
$expand: 'installs__service',
},
},
};
if (options.service) {
pineOpts.$filter = {
service_install: {
installs__service: { service_name: options.service },
},
};
}
const deviceServiceVars = await sdk.models.device.serviceVar.getAllByDevice(fullUUID, pineOpts);
fillInInfoFields(deviceServiceVars, fleetSlug, printedUUID);
deviceVars.push(...deviceServiceVars);
const deviceEnvVars = await sdk.models.device.envVar.getAllByDevice(fullUUID);
fillInInfoFields(deviceEnvVars, fleetSlug, printedUUID);
deviceVars.push(...deviceEnvVars);
}
return deviceVars;
}
function fillInInfoFields(varArray, fleetSlug, deviceUUID) {
var _a, _b, _c;
for (const envVar of varArray) {
if ('service' in envVar) {
envVar.serviceName = (_a = envVar.service[0]) === null || _a === void 0 ? void 0 : _a.service_name;
}
else if ('service_install' in envVar) {
envVar.serviceName = (_c = ((_b = envVar.service_install[0]) === null || _b === void 0 ? void 0 : _b.installs__service)[0]) === null || _c === void 0 ? void 0 : _c.service_name;
}
envVar.fleet = fleetSlug;
envVar.serviceName = envVar.serviceName || '*';
envVar.deviceUUID = deviceUUID || '*';
}
}
//# sourceMappingURL=list.js.map
;