Last active
September 11, 2024 13:20
-
-
Save l2D/51389881160b2bb9fdafc75277186b84 to your computer and use it in GitHub Desktop.
Convert `.env` file to YAML file. (Bash, Shell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
.env
fileArguments
input_file
: Path to the.env
file to be converted.output_file
: Path where the resulting.yaml
file will be saved.Steps Performed by the Script
#
are treated as comments and copied to the temporary file as is.=
delimiter. The script checks for various conditions:.env
syntax, an error message is displayed, and the script exits.Error Handling
.env
file, the script will terminate and display an appropriate error message.Example
Suppose your
input.env
file contains:Running
./script.sh input.env output.yaml
will produceoutput.yaml
:Notes
.env
file. Proper syntax and line formatting are required for correct conversion..env
file are preserved in the.yaml
output.