diff --git a/completion/_balena b/completion/_balena index c35716d5..1f7a14dd 100644 --- a/completion/_balena +++ b/completion/_balena @@ -16,7 +16,7 @@ _balena() { config_cmds=( generate inject read reconfigure write ) device_cmds=( deactivate detect identify init list local-mode logs move os-update pin public-url purge reboot register rename restart rm shutdown ssh start-service stop-service track-fleet ) devices_cmds=( supported ) - env_cmds=( add list rename rm ) + env_cmds=( list rename rm set ) fleet_cmds=( create list pin purge rename restart rm track-latest ) internal_cmds=( osinit ) local_cmds=( configure flash ) diff --git a/completion/balena-completion.bash b/completion/balena-completion.bash index 5176f067..3830c5a7 100644 --- a/completion/balena-completion.bash +++ b/completion/balena-completion.bash @@ -15,7 +15,7 @@ _balena_complete() config_cmds="generate inject read reconfigure write" device_cmds="deactivate detect identify init list local-mode logs move os-update pin public-url purge reboot register rename restart rm shutdown ssh start-service stop-service track-fleet" devices_cmds="supported" - env_cmds="add list rename rm" + env_cmds="list rename rm set" fleet_cmds="create list pin purge rename restart rm track-latest" internal_cmds="osinit" local_cmds="configure flash" diff --git a/docs/balena-cli.md b/docs/balena-cli.md index b6ddb351..9ad242ce 100644 --- a/docs/balena-cli.md +++ b/docs/balena-cli.md @@ -222,10 +222,10 @@ are encouraged to regularly update the balena CLI to the latest version. - Environment Variables - - [env add](#env-add) - [env list](#env-list) - [env rename](#env-rename) - [env rm](#env-rm) + - [env set](#env-set) - Fleets @@ -2072,83 +2072,6 @@ produce JSON output instead of tabular output # Environment Variables -## env add - -### Description - -Add an environment or config variable to one or more fleets, devices or -services, as selected by the respective command-line options. Either the ---fleet or the --device option must be provided, and either may be be -used alongside the --service option to define a service-specific variable. -(A service corresponds to a Docker image/container in a microservices fleet.) -When the --service option is used in conjunction with the --device option, -the service variable applies to the selected device only. Otherwise, it -applies to all devices of the selected fleet. If the --service option is -omitted, the variable applies to all services. - -If VALUE is omitted, the CLI will attempt to use the value of the environment -variable of same name in the CLI process' environment. In this case, a warning -message will be printed. Use `--quiet` to suppress it. - -'BALENA_' or 'RESIN_' are reserved variable name prefixes used to identify -"configuration variables". Configuration variables control balena platform -features and are treated specially by balenaOS and the balena supervisor -running on devices. They are also stored differently in the balenaCloud API -database. Configuration variables cannot be set for specific services, -therefore the --service option cannot be used when the variable name starts -with a reserved prefix. When defining custom fleet variables, please avoid -these reserved prefixes. - -Fleets may be specified by fleet name or slug. Fleet slugs are -the recommended option, as they are unique and unambiguous. Slugs can be -listed with the `balena fleet list` command. Note that slugs may change if the -fleet is renamed. Fleet names are not unique and may result in "Fleet is -ambiguous" errors at any time (even if it "used to work in the past"), for -example if the name clashes with a newly created public fleet, or with fleets -from other balena accounts that you may be invited to join under any role. -For this reason, fleet names are especially discouraged in scripts (e.g. CI -environments). - -Examples: - - $ balena env add TERM --fleet MyFleet - $ balena env add EDITOR vim -f myorg/myfleet - $ balena env add EDITOR vim --fleet MyFleet,MyFleet2 - $ balena env add EDITOR vim --fleet MyFleet --service MyService - $ balena env add EDITOR vim --fleet MyFleet,MyFleet2 --service MyService,MyService2 - $ balena env add EDITOR vim --device 7cf02a6 - $ balena env add EDITOR vim --device 7cf02a6,d6f1433 - $ balena env add EDITOR vim --device 7cf02a6 --service MyService - $ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2 - -### Arguments - -#### NAME - -environment or config variable name - -#### VALUE - -variable value; if omitted, use value from this process' environment - -### Options - -#### -f, --fleet FLEET - -fleet name or slug (preferred) - -#### -d, --device DEVICE - -device UUID - -#### -q, --quiet - -suppress warning messages - -#### -s, --service SERVICE - -service name - ## env list ### Aliases @@ -2367,6 +2290,90 @@ select a service variable (may be used together with the --device option) do not prompt for confirmation before deleting the variable +## env set + +### Aliases + +- `env add` + + +To use one of the aliases, replace `env set` with the alias. + +### Description + +Add or update an environment or config variable to one or more fleets, devices or +services, as selected by the respective command-line options. Either the +--fleet or the --device option must be provided, and either may be be +used alongside the --service option to define a service-specific variable. +(A service corresponds to a Docker image/container in a microservices fleet.) +When the --service option is used in conjunction with the --device option, +the service variable applies to the selected device only. Otherwise, it +applies to all devices of the selected fleet. If the --service option is +omitted, the variable applies to all services. + +If VALUE is omitted, the CLI will attempt to use the value of the environment +variable of same name in the CLI process' environment. In this case, a warning +message will be printed. Use `--quiet` to suppress it. + +'BALENA_' or 'RESIN_' are reserved variable name prefixes used to identify +"configuration variables". Configuration variables control balena platform +features and are treated specially by balenaOS and the balena supervisor +running on devices. They are also stored differently in the balenaCloud API +database. Configuration variables cannot be set for specific services, +therefore the --service option cannot be used when the variable name starts +with a reserved prefix. When defining custom fleet variables, please avoid +these reserved prefixes. + +Fleets may be specified by fleet name or slug. Fleet slugs are +the recommended option, as they are unique and unambiguous. Slugs can be +listed with the `balena fleet list` command. Note that slugs may change if the +fleet is renamed. Fleet names are not unique and may result in "Fleet is +ambiguous" errors at any time (even if it "used to work in the past"), for +example if the name clashes with a newly created public fleet, or with fleets +from other balena accounts that you may be invited to join under any role. +For this reason, fleet names are especially discouraged in scripts (e.g. CI +environments). + +Examples: + + $ balena env set TERM --fleet MyFleet + $ balena env set EDITOR vim -f myorg/myfleet + $ balena env set EDITOR vim --fleet MyFleet,MyFleet2 + $ balena env set EDITOR vim --fleet MyFleet --service MyService + $ balena env set EDITOR vim --fleet MyFleet,MyFleet2 --service MyService,MyService2 + $ balena env set EDITOR vim --device 7cf02a6 + $ balena env set EDITOR vim --device 7cf02a6,d6f1433 + $ balena env set EDITOR vim --device 7cf02a6 --service MyService + $ balena env set EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2 + +### Arguments + +#### NAME + +environment or config variable name + +#### VALUE + +variable value; if omitted, use value from this process' environment + +### Options + +#### -f, --fleet FLEET + +fleet name or slug (preferred) + +#### -d, --device DEVICE + +device UUID + +#### -q, --quiet + +suppress warning messages + +#### -s, --service SERVICE + +service name + # Fleets ## fleet create diff --git a/src/commands/env/set.ts b/src/commands/env/set.ts index 8c6fccba..036ccfd3 100644 --- a/src/commands/env/set.ts +++ b/src/commands/env/set.ts @@ -35,11 +35,13 @@ interface ArgsDef { value?: string; } -export default class EnvAddCmd extends Command { - public static description = stripIndent` - Add env or config variable to fleets, devices or services. +export default class EnvSetCmd extends Command { + public static aliases = ['env add']; - Add an environment or config variable to one or more fleets, devices or + public static description = stripIndent` + Add or update env or config variable to fleets, devices or services. + + Add or update an environment or config variable to one or more fleets, devices or services, as selected by the respective command-line options. Either the --fleet or the --device option must be provided, and either may be be used alongside the --service option to define a service-specific variable. @@ -66,15 +68,15 @@ export default class EnvAddCmd extends Command { `; public static examples = [ - '$ balena env add TERM --fleet MyFleet', - '$ balena env add EDITOR vim -f myorg/myfleet', - '$ balena env add EDITOR vim --fleet MyFleet,MyFleet2', - '$ balena env add EDITOR vim --fleet MyFleet --service MyService', - '$ balena env add EDITOR vim --fleet MyFleet,MyFleet2 --service MyService,MyService2', - '$ balena env add EDITOR vim --device 7cf02a6', - '$ balena env add EDITOR vim --device 7cf02a6,d6f1433', - '$ balena env add EDITOR vim --device 7cf02a6 --service MyService', - '$ balena env add EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2', + '$ balena env set TERM --fleet MyFleet', + '$ balena env set EDITOR vim -f myorg/myfleet', + '$ balena env set EDITOR vim --fleet MyFleet,MyFleet2', + '$ balena env set EDITOR vim --fleet MyFleet --service MyService', + '$ balena env set EDITOR vim --fleet MyFleet,MyFleet2 --service MyService,MyService2', + '$ balena env set EDITOR vim --device 7cf02a6', + '$ balena env set EDITOR vim --device 7cf02a6,d6f1433', + '$ balena env set EDITOR vim --device 7cf02a6 --service MyService', + '$ balena env set EDITOR vim --device 7cf02a6,d6f1433 --service MyService,MyService2', ]; public static args = { @@ -101,7 +103,7 @@ export default class EnvAddCmd extends Command { }; public async run() { - const { args: params, flags: options } = await this.parse(EnvAddCmd); + const { args: params, flags: options } = await this.parse(EnvSetCmd); const cmd = this; if (!options.fleet && !options.device) { diff --git a/src/hooks/prerun.ts b/src/hooks/prerun.ts index 3e3e1d47..eae3f41d 100644 --- a/src/hooks/prerun.ts +++ b/src/hooks/prerun.ts @@ -76,7 +76,7 @@ const DEFAULT_OFFLINE_COMPATIBLE = false; * See: https://oclif.io/docs/hooks * * This hook is used to track CLI command signatures (usage analytics). - * A command signature is something like "env add NAME [VALUE]". That's + * A command signature is something like "env set NAME [VALUE]". That's * literally so: 'NAME' and 'VALUE' are NOT replaced with actual values. */ diff --git a/src/preparser.ts b/src/preparser.ts index fbeedc72..8afcb3cd 100644 --- a/src/preparser.ts +++ b/src/preparser.ts @@ -44,7 +44,7 @@ export async function preparseArgs(argv: string[]): Promise { else if (['--help', '-h'].includes(cmdSlice[0])) { cmdSlice[0] = 'help'; } - // convert e.g. 'balena help env add' to 'balena env add --help' + // convert e.g. 'balena help env set' to 'balena env set --help' if ( cmdSlice.length > 1 && cmdSlice[0] === 'help' && diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 7aa69314..e71d8b46 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -48,7 +48,7 @@ interface LoggerAdapter { class Logger { public static readonly Level = Level; // `Logger.command` is currently set in `preparser.ts` - public static command: string; // CLI cmd, e.g. 'push', 'env add', ... + public static command: string; // CLI cmd, e.g. 'push', 'env set', ... public streams: { build: NodeJS.ReadWriteStream; diff --git a/tests/commands/env/add.spec.ts b/tests/commands/env/add.spec.ts index c16489ed..4c91eb9d 100644 --- a/tests/commands/env/add.spec.ts +++ b/tests/commands/env/add.spec.ts @@ -20,7 +20,7 @@ import { expect } from 'chai'; import { BalenaAPIMock } from '../../nock/balena-api-mock'; import { runCommand } from '../../helpers'; -describe('balena env add', function () { +describe('balena env set', function () { let api: BalenaAPIMock; const fullUUID = 'f63fd7d7812c34c4c14ae023fdff05f5'; @@ -42,7 +42,7 @@ describe('balena env add', function () { .post(/^\/v\d+\/device_environment_variable($|\?)/) .reply(200, 'OK'); - const { out, err } = await runCommand(`env add TEST 1 -d ${fullUUID}`); + const { out, err } = await runCommand(`env set TEST 1 -d ${fullUUID}`); expect(out.join('')).to.equal(''); expect(err.join('')).to.equal(''); @@ -53,7 +53,7 @@ describe('balena env add', function () { delete process.env.TEST_ENV_VAR_ADD_NO_VALUE_REJECTED; const { out, err } = await runCommand( - `env add TEST_ENV_VAR_ADD_NO_VALUE_REJECTED -d ${fullUUID}`, + `env set TEST_ENV_VAR_ADD_NO_VALUE_REJECTED -d ${fullUUID}`, ); expect(out.join('')).to.equal(''); @@ -74,7 +74,7 @@ describe('balena env add', function () { process.env.TEST_ENV_VAR_ADD_NO_VALUE = '4'; const { out, err } = await runCommand( - `env add TEST_ENV_VAR_ADD_NO_VALUE -d ${fullUUID}`, + `env set TEST_ENV_VAR_ADD_NO_VALUE -d ${fullUUID}`, ); delete process.env.TEST_ENV_VAR_ADD_NO_VALUE; @@ -95,7 +95,7 @@ describe('balena env add', function () { .reply(200, 'OK'); const { out, err } = await runCommand( - `env add TEST_EMPTY_STRING '' -d ${fullUUID}`, + `env set TEST_EMPTY_STRING '' -d ${fullUUID}`, ); expect(out.join('')).to.equal(''); diff --git a/tests/commands/help.spec.ts b/tests/commands/help.spec.ts index 5e985e40..5fc3b930 100644 --- a/tests/commands/help.spec.ts +++ b/tests/commands/help.spec.ts @@ -65,7 +65,7 @@ ADDITIONAL COMMANDS device shutdown shutdown a device device ssh SSH into the host or application container of a device devices supported list the supported device types (like 'raspberrypi3' or 'intel-nuc') - env add add env or config variable to application(s), device(s) or service(s) + env set add or update env or config variable to application(s), device(s) or service(s) env list list the environment or config variables of an application, device or service env rename change the value of a config or env var for an app, device or service env rm remove a config or env var from an application, device or service diff --git a/tests/test-data/pkg/expected-warnings-darwin-arm64.txt b/tests/test-data/pkg/expected-warnings-darwin-arm64.txt index 44d053e2..7738c6e2 100644 --- a/tests/test-data/pkg/expected-warnings-darwin-arm64.txt +++ b/tests/test-data/pkg/expected-warnings-darwin-arm64.txt @@ -136,9 +136,6 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/devices/supported.js -> Warning Entry 'main' not found in %1 - %1: node_modules/@oclif/core/package.json - %2: build/commands/env/add.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/list.js @@ -148,6 +145,9 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/rm.js +> Warning Entry 'main' not found in %1 + %1: node_modules/@oclif/core/package.json + %2: build/commands/env/set.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/fleet/create.js diff --git a/tests/test-data/pkg/expected-warnings-darwin-x64.txt b/tests/test-data/pkg/expected-warnings-darwin-x64.txt index fa29ba8b..66a475ad 100644 --- a/tests/test-data/pkg/expected-warnings-darwin-x64.txt +++ b/tests/test-data/pkg/expected-warnings-darwin-x64.txt @@ -136,9 +136,6 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/devices/supported.js -> Warning Entry 'main' not found in %1 - %1: node_modules/@oclif/core/package.json - %2: build/commands/env/add.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/list.js @@ -148,6 +145,9 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/rm.js +> Warning Entry 'main' not found in %1 + %1: node_modules/@oclif/core/package.json + %2: build/commands/env/set.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/fleet/create.js diff --git a/tests/test-data/pkg/expected-warnings-linux-arm64.txt b/tests/test-data/pkg/expected-warnings-linux-arm64.txt index 44d053e2..7738c6e2 100644 --- a/tests/test-data/pkg/expected-warnings-linux-arm64.txt +++ b/tests/test-data/pkg/expected-warnings-linux-arm64.txt @@ -136,9 +136,6 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/devices/supported.js -> Warning Entry 'main' not found in %1 - %1: node_modules/@oclif/core/package.json - %2: build/commands/env/add.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/list.js @@ -148,6 +145,9 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/rm.js +> Warning Entry 'main' not found in %1 + %1: node_modules/@oclif/core/package.json + %2: build/commands/env/set.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/fleet/create.js diff --git a/tests/test-data/pkg/expected-warnings-linux-x64.txt b/tests/test-data/pkg/expected-warnings-linux-x64.txt index fa29ba8b..66a475ad 100644 --- a/tests/test-data/pkg/expected-warnings-linux-x64.txt +++ b/tests/test-data/pkg/expected-warnings-linux-x64.txt @@ -136,9 +136,6 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/devices/supported.js -> Warning Entry 'main' not found in %1 - %1: node_modules/@oclif/core/package.json - %2: build/commands/env/add.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/list.js @@ -148,6 +145,9 @@ > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/env/rm.js +> Warning Entry 'main' not found in %1 + %1: node_modules/@oclif/core/package.json + %2: build/commands/env/set.js > Warning Entry 'main' not found in %1 %1: node_modules/@oclif/core/package.json %2: build/commands/fleet/create.js diff --git a/tests/test-data/pkg/expected-warnings-win32-x64.txt b/tests/test-data/pkg/expected-warnings-win32-x64.txt index e5e74c81..e278eabd 100644 --- a/tests/test-data/pkg/expected-warnings-win32-x64.txt +++ b/tests/test-data/pkg/expected-warnings-win32-x64.txt @@ -136,9 +136,6 @@ > Warning Entry 'main' not found in %1 %1: node_modules\@oclif\core\package.json %2: build\commands\devices\supported.js -> Warning Entry 'main' not found in %1 - %1: node_modules\@oclif\core\package.json - %2: build\commands\env\add.js > Warning Entry 'main' not found in %1 %1: node_modules\@oclif\core\package.json %2: build\commands\env\list.js @@ -148,6 +145,9 @@ > Warning Entry 'main' not found in %1 %1: node_modules\@oclif\core\package.json %2: build\commands\env\rm.js +> Warning Entry 'main' not found in %1 + %1: node_modules\@oclif\core\package.json + %2: build\commands\env\set.js > Warning Entry 'main' not found in %1 %1: node_modules\@oclif\core\package.json %2: build\commands\fleet\create.js