balena-cli icon indicating copy to clipboard operation
balena-cli copied to clipboard

[FEATURE] `balena env add --file </path/to/.env>` to read .env file

Open AndreMaz opened this issue 3 years ago • 2 comments

Problem: Currently the only way to add an env. var via CLI is by using env add command, which only allows to set a single variable at the time. This approach is not very efficient when there are several env. vars.

Possible solution: It would be nice to be able to read several vars from an .env file and add them to a Fleet/Device. For example, balena end add --file </path/to/.env>

Context: CLI version: 13.3.0

env commands include:
  env add <name> [value]   add env or config variable to fleets, devices or services
  env rename <id> <value>  change the value of a config or env var for a fleet, device or service
  env rm <id>              remove a config or env var from a fleet, device or service

Run balena help -v for a list of all available commands,
 or balena help <command> for detailed help on a specific command.

AndreMaz avatar Mar 11 '22 11:03 AndreMaz

Hello @AndreMaz , I made the following script to solve the problem you describe:

`#!/bin/bash

if [ "$#" -lt 2 ]; then echo "Uso: $0 <fleet_name> <env_file_path> [ ... <serviceN>]" exit 1 fi

FLEET_NAME="$1" ENV_FILE="$2" shift 2 SERVICE_ARRAY=("$@")

for SERVICE_NAME in "${SERVICE_ARRAY[@]}"; do while IFS= read -r line || [ -n "$line" ]; do if [ -n "$line" ]; then IFS='=' read -r var value <<< "$line" echo "balena env add --fleet "$FLEET_NAME" $var $value --service "$SERVICE_NAME"" balena env add --fleet "$FLEET_NAME" $var $value --service "$SERVICE_NAME" fi done < "$ENV_FILE" done `

matiasAS avatar Dec 21 '23 02:12 matiasAS

How to avoid containers multiple restarts when variables are a lot?

netliteIT avatar Apr 24 '24 07:04 netliteIT