November 19, 2020

Grafana Toml to environment variables

The following snippet creates environment variables from your Grafana toml.

#!/bin/sh

# Script writes to env file and shows JSON lines on stdout

if [ -z "$2" ]; then
	echo "USAGE: $0 ./inputfile.toml ./output.env"
fi

TF=/tmp/temp-grafana-conf.toml
echo "[default]" > $TF
cat $1|sed 's #.*$  ' >> $TF
> $2

while read l; do
	if `echo $l|grep -q '^#'`; then
		continue
	fi
	if `echo $l|grep -q '^\[.*\]$'`; then
		PREFIX=`echo $l|tr -d '\[\]'`
		continue
	fi
	if `echo $l|grep -q '='`; then
		NAME="GF_$PREFIX"
		NAME=$NAME"_`echo $l|awk '{print $1}'`"
		NAME=`echo $NAME|tr a-z\.\- A-Z__`
		VAL=`echo $l|sed 's/^.*\ =\ //'`
		echo "export $NAME=$VAL" >> $2
		echo "\"$NAME\": \"$VAL\","|sed 's "" " g'
		continue
	fi
done < $TF

The script isn’t perfect (arrays and mid line comments will fail) but it’ll save you some manual work.

Blog by lent.ink