balena-io / balena-cli

The official balena CLI tool.
Apache License 2.0
452 stars 138 forks source link

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

Open AndreMaz opened 2 years ago

AndreMaz commented 2 years ago

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.
matiasAS commented 8 months ago

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

`#!/bin/bash

if [ "$#" -lt 2 ]; then echo "Uso: $0 [ ... ]" 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 `

netliteIT commented 4 months ago

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