构建 golang,包括 firebase 凭证文件 .json

Edu*_*uez 5 go firebase firebase-authentication go-build

我编写了一个 Golang 程序,旨在解析 scv 文件并将数据上传到 FireStore,该程序的构建是为了与只编写 scv 路径来上传信息的人共享。

我用它来使用 firebase-admin:

opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
Run Code Online (Sandbox Code Playgroud)

当我这样做时,这种方法效果很好:

$ go run main.go
Run Code Online (Sandbox Code Playgroud)

但是当我构建项目并执行二进制文件时,我得到了这个:

$ cannot read credentials file: open firebase-config.json: no such file or directory 
Run Code Online (Sandbox Code Playgroud)

我确实读过这种方法,但这也意味着共享firebase-config.json,这个 Go 程序的想法只是共享可执行文件,没有任何其他文档。

有没有办法构建包含 json 的程序或像这样直接使用 json 验证 firebase-admin ?:

opt := option.WithCredentials({< authentication json >})
Run Code Online (Sandbox Code Playgroud)

我没有找到使用此方法WithCredential的文档或示例。

Edu*_*uez 1

我不知道这是否是解决此问题的最佳方法,但这就是我所做的:

  1. 我决定将此脚本可执行文件放入/opt/< dir-name >/bin
  2. 我编写了一个额外的函数来创建 auth .json /opt/< dir-name >/auth.json
  3. 所有脚本都使用此/opt/< dir-name >/auth.json文件执行
  4. 我编写了另一个函数,它在执行结束时删除auth.json

所以,现在我可以这样做:

var file string = "/opt/< dir-name >/auth.json" 
sa := option.WithCredentialsFile(file)
Run Code Online (Sandbox Code Playgroud)

现在我可以使用指令“安装到/opt/< dir-name>/bin并将其添加到您的$PATH ”来共享此脚本