Add example of parsing YADM_HOOK_FULL_COMMAND

This commit is contained in:
Tim Byrne 2020-01-27 07:31:58 -06:00
parent 9a2883985b
commit 02bedd712a
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
2 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,7 @@
## Example of parsing `$YADM_HOOK_FULL_COMMAND`
Contributed by Tim Byrne
Hook | Description
---- | -----------
pre_log | Provides an example of parsing `$YADM_HOOK_FULL_COMMAND` in Bash

View File

@ -0,0 +1,26 @@
#!/bin/bash
# yadm exposes all parameters of the command which triggers a hook. Those
# parameters are exported as the environment variable YADM_HOOK_FULL_COMMAND.
# Any spaces, tabs, or backslashes in those parameters are escaped with a
# backslash. The function `parse_full_command()` is a demonstration of parsing
# those values which may be escaped.
function parse_full_command() {
local delim=$'\x1e' # ASCII Record Separator
local space=$'\x1f' # ASCII Unit Separator
local tab=$'\t' # ASCII TAB
local cmd
cmd="$YADM_HOOK_FULL_COMMAND"
cmd="${cmd//\\ /$space}" # swap escaped spaces for `1f`
cmd="${cmd//\\\\/\\}" # fix escaped backslashes
cmd="${cmd//\\$tab/$tab}" # fix escaped tabs
cmd="${cmd// /$delim}" # convert space delimiters to `1c`
cmd="${cmd//$space/ }" # convert `1f` back to spaces
# parse data into an array
IFS=$delim read -r -a full_cmd <<< "$cmd"
}
parse_full_command
for param in "${full_cmd[@]}"; do
echo "Parameter: '$param'"
done