目标
我正在更新 service_account key 并将它们存储为 Firebase 环境变量。
错误
当我运行 firebase deploy --only functions
时,我看到:
Silver-Sliver:Issy dchaddportwine$ firebase deploy --only functions
=== Deploying to 'development-is'...
i deploying functions
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: Error occurred while parsing your function triggers.
Error: Failed to parse private key: Error: Invalid PEM formatted message.
问题
使用 firebase functions:config:set
更新会在字符串中引入额外的 \
字符。这是我在将 :set
应用于 private_key_id
后在终端中看到的。看到额外的反斜杠,其中 \n
变成了 \\n
?
Silver-Sliver:Issy dchaddportwine$ firebase functions:config:set service_account.private_key_id="-----BEGIN PRIVATE KEY-----\nMIIE...L5A==\n-----END PRIVATE KEY-----\n"
✔ Functions config updated.
Please deploy your functions for the change to take effect by running firebase deploy --only functions
Silver-Sliver:Issy dchaddportwine$ firebase functions:config:get
{
"service_account": {
"private_key_id": "-----BEGIN PRIVATE KEY-----\\nMIIE...L5A==\\n-----END PRIVATE KEY-----\\n",
问题
添加斜杠是错误,还是我的用户错误。我应该以不同的方式更新 private_key_id 吗?我该如何解决这个问题?
最佳答案
TLDR:将键的值用单引号括起来,并以 $
为前缀进行特殊处理。
假设一
新行的转义由shell程序完成。
测试 1
创建一个脚本来记录在 shell 中解析的参数。
> echo "console.log(process.argv)" > shell-args.js
运行
> node shell-args.js x="Always\nEscape"
[ '/Users/alẹ́tilẹ́/.nvm/versions/node/v9.10.1/bin/node',
'/Users/alẹ́tilẹ́/Tests/shell-args.js',
'x=Always\\nEscape' ]
假设2
连接并打印一个文件作为输入使 shell 参数保持原样。
echo "Always\nEscape" > always_escape
测试 2
运行
> node shell-args.js x="$(< always_escape)"
或
> node shell-args.js x="`< always_escape`"
[ '/Users/alẹ́tilẹ́/.nvm/versions/node/v9.10.1/bin/node',
'/Users/alẹ́tilẹ́/Tests/shell-args.js',
'x=Always\nEscape' ]
假设三
当使用 ANSI C 扩展时,换行符会得到特殊处理。
来自 man bash
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard...The expanded result is single-quoted, as if the dollar sign had not been present.
测试 3
运行
> node shell-args.js x=$'Always\nEscape'
[ '/Users/alẹ́tilẹ́/.nvm/versions/node/v9.10.1/bin/node',
'/Users/alẹ́tilẹ́/Tests/shell-args.js',
'x=Always\nEscape' ]
结论
将键的值用单引号括起来,并用$
作为前缀进行特殊处理。
关于Firebase 环境配置 - 向 private_key_id 添加了斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53161614/