# IMDS document queries
# vim:set filetype=sh:
# shellcheck shell=sh

. "$LIBDIR/tiny-cloud/document"

# Print an optional member beneath a required document path.
_imds_optional_value() {
	local file="$1" key="$2" keys value
	shift 2
	keys=$(yx -f "$file" -- "$@") || return 1
	printf '%s\n' "$keys" | grep -qxF "$key" || return 0
	value=$(yx -f "$file" -- "$@" "$key") || return 1
	case "$value" in ''|null) return 0;; esac
	printf '%s\n' "$value"
}

imds_document() {
	local directory="$1" path="$2" document file value

	case "$path" in
		''|/*) echo "imds: invalid document path '$path'" >&2; return 1;;
	esac
	case "/$path/" in
		*/../*|*/./*)
			echo "imds: invalid document path '$path'" >&2; return 1;;
	esac
	document="${path%%/*}"
	case "$document" in
		*[!A-Za-z0-9_.-]*)
			echo "imds: invalid document name '$document'" >&2; return 1;;
	esac
	file="$directory/$document"
	[ -f "$file" ] || return 1
	if [ "$path" = "$document" ]; then
		cat "$file"
		return
	fi

	value=$(document_select "$file" "${path#*/}") || return
	case "$value" in
		''|null) echo "imds: empty metadata value '$path'" >&2; return 1;;
	esac
	printf '%s\n' "$value"
}
