Skip to content

Commit

Permalink
Create pathToYaml script
Browse files Browse the repository at this point in the history
  • Loading branch information
lindhe committed Aug 3, 2024
1 parent 43fbafd commit 83fb589
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Terminal helper programs.

* `copy.sh`: Copy the contents of a file to the clipboard.
* `bbb.sh`: A smart wrapper for base64 decoding.
* `pathToYaml.sh`: Takes `foo.bar` and gives you YAML!
61 changes: 61 additions & 0 deletions terminal/pathToYaml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

# {{{

set -euo pipefail

stderr() {
echo -e "${@}" 1>&2
}

fail() {
stderr "${1:-}"
stderr ""
stderr "Exiting …"
exit "${2:-1}"
}

if [[ $# -eq 1 ]]; then
if [[ "${1}" == "-h" ]]; then
stderr ""
stderr "USAGE:"
stderr " echo 'foo.bar.boo.far' | ${0}"
stderr " ${0} 'foo.bar.boo.far'"
stderr ""
exit 0
fi
fi

if [[ $# -eq 0 ]]; then
# no args => stdin
readarray inputarray
else
inputarray=("${@}")
fi
declare -r inputarray

# }}}

if [[ ${inputarray[0]} =~ "=" ]]; then
# If a.b.c=foo, split it:
path_string="$( echo "${inputarray[0]}" | cut -d '=' -f 1)"
value_string="$( echo "${inputarray[0]}" | cut -d '=' -f 2)"
else
path_string="${inputarray[0]}"
value_string=""
fi
declare -r path_string value_string

SPACES_PER_INDENTATION=2

indentations=0
for part in ${path_string//./ }; do
spaces=$((indentations*SPACES_PER_INDENTATION))
if [[ ${spaces} -eq 0 ]]; then
echo -en "${part}:"
else
echo -en "\n$(printf ' %.0s' $(seq ${spaces}))${part}:"
fi
indentations=$((indentations+1))
done
echo -en " ${value_string}\n"

0 comments on commit 83fb589

Please sign in to comment.