# This file is part of Haketilo # # Copyright (C) 2021, Wojtek Kosior # # This program is free software: you can redistribute it and/or modify # it under the terms of the CC0 1.0 Universal License as published by # the Creative Commons Corporation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # CC0 1.0 Universal License for more details. ENDL=" " # A "raw" echo, interprets neither backclash escapes nor command-line options. # Does not emit trailing newline. ech() { printf %s "$*" } errcho() { echo "$@" >&2 } map_set_instr() { echo "$1__$2='$3'" } map_set() { eval "$(map_set_instr "$@")" } map_set_export() { eval "export $(map_set_instr "$@")" } map_get() { eval "echo \"\$$1__$2\"" } map_del_instr() { echo "unset $1__$2" } map_del() { eval "$(map_del_instr "$@")" } sanitize() { echo "$1" | tr /.- _ } escape_regex_special() { ech "$1" | sed 's/\([]\.*?{},()[-]\)/\\\1/g' } # Note: We don't actually parse JSON. We extract needed keys with sed regexes # which does not work in the general case but is sufficient for now. get_json_key() { local KEY_REG="$(escape_regex_special "$1")" ech "$2" | sed 's/\(.*"'"$KEY_REG"'"[[:space:]]*:[[:space:]]*"\([^"]*\)"\)\?.*/\2/' | grep . | head -1 }