Skip to content

Instantly share code, notes, and snippets.

@ezheidtmann
Created October 6, 2021 17:49
Show Gist options
  • Save ezheidtmann/c38b5f42aca0e2d2eba18d67eb6f3593 to your computer and use it in GitHub Desktop.
Save ezheidtmann/c38b5f42aca0e2d2eba18d67eb6f3593 to your computer and use it in GitHub Desktop.
SAM CLI utilities
def _quote_sam_parameter_value(value: str):
"""Quote a parameter value for SAM CLI as best we can
Trying to reverse SAM's _unqoute_wrapped_quotes()
https://github.com/aws/aws-sam-cli/blob/56bba34a5e4739c87e0831d6bf73f6a683ba6134/samcli/cli/types.py#L40
- Fail if the value contains a literal backslash; there's no way to safely
handle this if the backslash occurs in the following situations:
- is the last character in the string
- precedes a space character
- precedes a double or single quote
- Prepend literal backslashes to double quotes, to preserve double quotes
- Enclose in double quotes to preserve spaces
"""
if "\\" in value:
raise ValueError(
"Cannot encode literal backslashes in SAM parameter value; SAM will mangle!"
)
value = value.replace('"', '\\"')
return f'"{value}"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment