Created
July 9, 2020 10:01
-
-
Save garethahealy/d5ce3ff9457e3c3f1487269195bc49e5 to your computer and use it in GitHub Desktop.
resolved packages in rego policy directory, filters based on a regex input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get_rego_namespaces | |
# ==================== | |
# | |
# Summary: Resolves the package names in your rego policies against a regex lookup | |
# | |
# Usage: get_rego_namespaces ${regex} | |
# | |
# Options: | |
# <regex> Regex pattern matching package name | |
# Globals: | |
# none | |
# Returns: | |
# string - list of --namespaces {which-matched-regex} | |
get_rego_namespaces() { | |
local regex="${1:-.*}" | |
declare -a namespaces | |
# shellcheck disable=SC2038 | |
for file in $(find policy/* -name "*.rego" -type f | xargs); do | |
read -r line < "${file}" | |
local current="$(echo ${line/package/} | xargs)" | |
if [[ "${current}" =~ ${regex} ]]; then | |
if [[ ! "${namespaces[*]}" =~ ${current} ]]; then | |
namespaces+=("--namespace ${current}") | |
fi | |
fi | |
done | |
echo "${namespaces[*]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment