Created
May 24, 2020 23:43
-
-
Save petrohs/3970ba4e84843d727b948be9fffcd245 to your computer and use it in GitHub Desktop.
parse_yaml¦ convertir un archivo en formato yaml a export de variables bash
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 | |
ayuda() { echo ' | |
* parse_yaml.bash * | |
DESCRIPCION: --- | |
USO: parse_yaml.bash [-k] archivo.yaml [pref] | |
USO: parse_yaml.bash [-i] [-h|-?] [-v] | |
OPCIONES: archivo.yaml archivo con configuraciones en formato yaml | |
pref perfijo a usar de manera opcional | |
-k en lugar de mostrar en pantalla, crea un archivo temporal | |
-i Informacion del uso y configuracion | |
-h Ayuda | |
-v Version | |
DEPENDENCIAS: --- | |
BUGS: --- | |
NOTAS: --- | |
VERSIONES: 20190307 0.1.0 ohs/ Opciones | |
URL: https://code.i-harness.com/en/q/4c8468 | |
';} | |
#ayuda y version | |
if [[ "$1" = "" ]] || [[ "$1" = "-h" ]] || [[ "$1" = "--help" ]] || [[ "$1" = "-?" ]] || [[ "$1" = "--ayuda" ]]; then ayuda; exit 1; | |
elif [[ "$1" = "-v" ]] || [[ "$1" = "--version" ]]; then ayuda | grep VERSIONES | cut -d: -f2 | cut -d/ -f1; exit 1; fi; | |
##info | |
if [[ "$1" = "-i" ]]; then echo 'parse yaml | |
Este programa permite convertir un archivo en formato yaml a export de | |
variables bash. | |
El formato general es: | |
``` | |
#comentario | |
nombreSeccion1: | |
¬¬variable1:¬¬¬¬valor1 | |
¬¬variable2:¬¬¬¬$(echo valor2) | |
nombreSeccion2: | |
¬¬variable1:¬¬¬¬${nombreSeccion1_variable1}/valor1 | |
¬¬variable2:¬¬¬¬valor2 | |
``` | |
los ¬ son espacios en blanco, obligatorios son los primero dos antes | |
de cada variable, los espacios despues de los : no son relevantes. | |
La salida que se obtiene es: | |
``` | |
export nombreSeccion1_variable1="valor1" | |
export nombreSeccion1_variable2="$(echo valor2)" | |
export nombreSeccion2_variable1="${nombreSeccion1_variable1}/valor1" | |
export nombreSeccion2_variable2="valor2" | |
``` | |
Si se pasa un `PREFIJO_` se obtendria: | |
``` | |
export PREFIJO_nombreSeccion1_variable1="valor1" | |
``` | |
Por omision es a la salida estandar que facilmete se pueden redireccionar. | |
Si se usa la opcion `-k` el contenido se guarda en un archivo y la | |
salida estandar sera el nombre del archivo. | |
'; exit 0; fi; | |
## | |
if [[ "$1" = "-k" ]] | |
then | |
archYaml="$2"; | |
prefix="$3"; | |
else | |
archYaml="$1"; | |
prefix="$2"; | |
tempo=""; | |
fi | |
tempo="$(mktemp ${archYaml}.XXXXXXXXXX)"; | |
s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') | |
sed -ne "s|^\($s\):|\1|" \ | |
-e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ | |
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" ${archYaml} | | |
awk -F$fs '{ | |
indent = length($1)/2; | |
vname[indent] = $2; | |
for (i in vname) {if (i > indent) {delete vname[i]}} | |
if (length($3) > 0) { | |
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")} | |
printf("export %s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3); | |
} | |
}' >$tempo | |
if [[ "$1" = "-k" ]] | |
then | |
echo "$tempo"; | |
else | |
cat $tempo | |
rm $tempo | |
fi | |
#Ser cultos para ser libres | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment