Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Usage: make_event my-event test "hello world" -> {"name":"my-event","test":"hello world","unixtime":123}
- make_event() {
- if ! (( "$#" % 2)); then
- echo "log_event expects an uneven number of arguments"
- echo "Usage: $0 <event-name> [[<key> <val>]...]"
- exit 1
- fi
- build_pairs() {
- if [[ "$#" -eq 1 ]]; then
- return
- fi
- echo --arg "var$1" "$2" --arg "val$1" \""$3"\" "$(build_pairs $(($1 + 1)) "${@:4}")"
- }
- build_pipeline() {
- if [[ "$#" -eq 1 ]]; then
- return
- fi
- echo ' | .[$'var$1'] = $'val$1 $(build_pipeline $(($1 + 1)) "${@:4}")
- }
- local PIPELINE=$(build_pipeline 0 "${@:2}")
- local PAIRS=$(build_pairs 0 "${@:2}")
- eval jq -c -n $PAIRS \
- --arg name "$1" \
- --arg unixtime "$(date +%s)" \
- --arg formattedtime \"$(date)\" \' \
- ' . | .["name"] = $name ' \
- ' | .["unixtime"] = $unixtime ' \
- ' | .["formattedtime"] = $formattedtime ' \
- "$PIPELINE"\' <<< "{}"
- }
- log_event() {
- EVENT="$(make_event "$@")"
- echo "$(date) BACKUP_EVENT|$EVENT"
- if [[ ! -z "$EVENT_LOG" ]]; then
- echo "$EVENT" >> "$EVENT_LOG"
- fi
- }
Add Comment
Please, Sign In to add comment