Skip to content

Instantly share code, notes, and snippets.

@l2D
Last active March 26, 2024 09:06
Show Gist options
  • Save l2D/51389881160b2bb9fdafc75277186b84 to your computer and use it in GitHub Desktop.
Save l2D/51389881160b2bb9fdafc75277186b84 to your computer and use it in GitHub Desktop.
Convert `.env` file to YAML file. (Bash, Shell)
#!/bin/bash
# Usage: ./script.sh input.env output.yaml
input_file=$1
output_file=$2
# Check if the input file exists
if [ ! -f "$input_file" ]; then
echo "Input file does not exist: $input_file"
exit 1
fi
# Create a temporary file
temp_file=$(mktemp)
# Add a newline to the end of the input file if it doesn't already have one
if [[ $(tail -c1 "$input_file" | wc -l) -ne 1 ]]; then
echo "" >> "$input_file"
fi
# Read the input file line by line
while IFS= read -r line
do
# If the line is a comment, write it to the temporary file as is
if [[ $line == \#* ]]; then
echo "$line" >> "$temp_file"
continue
fi
# If the line is empty, write it to the temporary file as is
if [[ -z "$line" ]]; then
echo "$line" >> "$temp_file"
continue
fi
# Split the line into key and value
IFS='=' read -ra kv <<< "$line"
if [ ${#kv[@]} -lt 1 ]; then
echo "Invalid .env syntax in line: $line"
exit 1
fi
# Write the key and value to the temporary file in YAML format
# If the value is empty, write an empty string
# If the value is a number, write it without quotes
# If the value is already enclosed in quotes, write it as is
if [ ${#kv[@]} -eq 1 ]; then
echo "${kv[0]}: ''" >> "$temp_file"
elif [[ ${kv[1]} =~ ^[0-9]+$ ]]; then
echo "${kv[0]}: ${kv[1]}" >> "$temp_file"
elif [[ ${kv[1]} =~ ^\".*\"$ || ${kv[1]} =~ ^\'.*\'$ ]]; then
echo "${kv[0]}: ${kv[1]}" >> "$temp_file"
else
echo "${kv[0]}: '${kv[1]}'" >> "$temp_file"
fi
done < "$input_file"
# Move the temporary file to the output file
mv "$temp_file" "$output_file"
@l2D
Copy link
Author

l2D commented Jan 13, 2024

Script Documentation

Usage

./script.sh input.env output.yaml

Description

This script converts a .env file to a .yaml file. It takes two arguments: an input file (input.env) and an output file (output.yaml). The script reads the environment variable definitions from the .env file, processes them, and outputs them in YAML format.

Requirements

  • Bash shell environment
  • Input .env file

Arguments

  1. input_file: Path to the .env file to be converted.
  2. output_file: Path where the resulting .yaml file will be saved.

Steps Performed by the Script

  1. Check Existence of Input File: Verifies if the specified input file exists. If not, it outputs an error message and exits.
  2. Temporary File Creation: A temporary file is created for intermediate processing.
  3. Newline Addition: Adds a newline at the end of the input file if it is missing.
  4. Processing Lines: The script reads the input file line by line and processes each line based on its content:
    • Comments: Lines starting with # are treated as comments and copied to the temporary file as is.
    • Empty Lines: Empty lines are copied to the temporary file as is.
    • Key-Value Pairs: Lines are split into key-value pairs based on the = delimiter. The script checks for various conditions:
      • If the line does not conform to the .env syntax, an error message is displayed, and the script exits.
      • If the key has no value, it is written to the temporary file with an empty string as the value.
      • If the value is a number, it is written as is.
      • If the value is enclosed in quotes, it is written as is.
      • In all other cases, the value is enclosed in single quotes.
  5. Creating Output File: The temporary file is renamed to the specified output file name.

Error Handling

  • If the input file does not exist or if there is invalid syntax in the .env file, the script will terminate and display an appropriate error message.

Example

Suppose your input.env file contains:

# Sample ENV file
API_KEY=abcdef123456
DATABASE_URL=mysql://username:password@localhost:3306/mydatabase
APP_NAME=MyApp
SECRET_KEY=@#$%^&*()
GREETING_MESSAGE=Hello, World!
DEBUG_MODE=
# This is commend
DATABASE_HOST=127.0.0.1
DATABASE_PORT=3306

VERSION=1022
LAST_UPDATE="2024-01-11 07:00"

Running ./script.sh input.env output.yaml will produce output.yaml:

# Sample ENV file
API_KEY: 'abcdef123456'
DATABASE_URL: 'mysql://username:password@localhost:3306/mydatabase'
APP_NAME: 'MyApp'
SECRET_KEY: '@#$%^&*()'
GREETING_MESSAGE: 'Hello, World!'
DEBUG_MODE: ''
# This is commend
DATABASE_HOST: '127.0.0.1'
DATABASE_PORT: 3306

VERSION: 1022
LAST_UPDATE: "2024-01-11 07:00"

Notes

  • The script is sensitive to the format of the .env file. Proper syntax and line formatting are required for correct conversion.
  • Comments and empty lines in the .env file are preserved in the .yaml output.
  • The script temporarily modifies the input file by appending a newline if it's missing at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment