balena-cli/lib/utils/env-common.ts
Pagan Gazzard 667358b23e Import just strip-tags from common-tags to reduce startup time
This saves ~100ms for me on startup with no fast-boot cache and ~50ms
with a fast-boot cache

Change-type: patch
2020-06-25 22:56:43 +01:00

110 lines
3.5 KiB
TypeScript

/**
* @license
* Copyright 2019 Balena Ltd.
*
* 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.
*/
import { flags } from '@oclif/command';
import { stripIndent } from './lazy';
import { ExpectedError } from '../errors';
import type { IBooleanFlag } from '@oclif/parser/lib/flags';
export const booleanConfig: IBooleanFlag<boolean> = flags.boolean({
char: 'c',
description:
'select a configuration variable (may be used together with the --device option)',
default: false,
exclusive: ['service'],
});
export const booleanDevice: IBooleanFlag<boolean> = flags.boolean({
char: 'd',
description:
'select a device-specific variable instead of an application (fleet) variable',
default: false,
});
export const booleanService: IBooleanFlag<boolean> = flags.boolean({
char: 's',
description:
'select a service variable (may be used together with the --device option)',
default: false,
exclusive: ['config'],
});
export const rmRenameHelp = stripIndent`
Variables are selected by their database ID (as reported by the 'balena envs'
command) and one of six database "resource types":
- application (fleet) environment variable
- application (fleet) configuration variable (--config)
- application (fleet) service variable (--service)
- device environment variable (--device)
- device configuration variable (--device --config)
- device service variable (--device --service)
The --device option selects a device-specific variable instead of an application
(fleet) variable.
The --config option selects a configuration variable. Configuration variable
names typically start with the 'BALENA_' or 'RESIN_' prefixes and are used to
configure balena platform features.
The --service option selects a service variable, which is an environment variable
that applies to a specifc service (application container) in a microservices
(multicontainer) application.
The --service and --config options cannot be used together, but they can be
used alongside the --device option to select a device-specific service or
configuration variable.
`;
/**
* Return an API database resource name like 'device_config_variable' or
* 'service_environment_variable' given three boolean arguments.
* @param isConfig Whether the resource is a configuration variable
* @param isDevice Whether the resource is a device variable
* @param isService Whether the resource is a service variable
*/
export function getVarResourceName(
isConfig: boolean,
isDevice: boolean,
isService: boolean,
): string {
return isDevice
? isConfig
? 'device_config_variable'
: isService
? 'device_service_environment_variable'
: 'device_environment_variable'
: isConfig
? 'application_config_variable'
: isService
? 'service_environment_variable'
: 'application_environment_variable';
}
/**
* Check that the given string looks like and parses like a decimal integer,
* and return the parsed value.
*/
export function parseDbId(id: string): number {
if (/^[\d]+$/.exec(id) == null) {
throw new ExpectedError("The variable's ID must be an integer");
}
return Number(id);
}