From 02bedd712a19dab976b12d5f98b5e92f2a8b969f Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Mon, 27 Jan 2020 07:31:58 -0600 Subject: [PATCH] Add example of parsing YADM_HOOK_FULL_COMMAND --- .../parsing_full_command_example/README.md | 7 +++++ .../parsing_full_command_example/pre_log | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 contrib/hooks/parsing_full_command_example/README.md create mode 100755 contrib/hooks/parsing_full_command_example/pre_log diff --git a/contrib/hooks/parsing_full_command_example/README.md b/contrib/hooks/parsing_full_command_example/README.md new file mode 100644 index 0000000..3593a5e --- /dev/null +++ b/contrib/hooks/parsing_full_command_example/README.md @@ -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 diff --git a/contrib/hooks/parsing_full_command_example/pre_log b/contrib/hooks/parsing_full_command_example/pre_log new file mode 100755 index 0000000..0af52f3 --- /dev/null +++ b/contrib/hooks/parsing_full_command_example/pre_log @@ -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