普通网友 2018-12-24 12:14
浏览 6
已采纳

在Dockerize Golang模板中从文件内容设置变量

I want to use the jsonQuery syntax in dockerize to parse traefik's acme.json and emit cert/key files for TLS settings in another service.

jsonQuery accepts a string, which the example gives as an environment variable {{ .Env.myJson }}

How might I get the string contents of a file:

{{with $myJsonContent := <insert magic here> }}
    # extract key to file 
{{end}}
  • 写回答

2条回答 默认 最新

  • duanfang7757 2018-12-26 19:12
    关注

    I can't see any way to add a function to the template, as dockerize doesn't expose the addition of functions to the template prior to parsing. So you'll either have to (1) get the contents of acme.json into an environment variable, or (2) modify dockerize to include a jsonFileQuery function in the templates.

    1. Add the contents of acme.json to the environment variables before running dockerize - then access as in the example. This could be done with a small go program, added to the container and run via CMD prior to the CMD dockerize

    2. Fork dockerize and change jsonQuery: Fork dockerize and change line 83 of template.go:
      from: parser, err := gojq.NewStringQuery(jsonObj)
      to: parser, err := gojq.NewFileQuery(jsonObj)
      Then use:

      {{with $myJsonContent := jsonQuery "/opt/traefik/acme.json" "toplevelobject"  }}
          # extract key to file 
      {{end}}
      

      gojq.NewStringQuery() is a function behind the jsonQuery template function. The gojq.NewFileQuery() version has the same signature as the StringQuery but reads the file at the path in the input string instead of using the input string as json.

      OR

      Merge new jsonFileQuery template function into dockerize: submit an issue to dockerize to add jsonFileQuery to the template functions. Seems like it could be set up the same as jsonQuery but with the small difference above. In template.go, add the jsonFileQuery function and assign it to jsonFileQuery in the template.FuncMap{} within generateFile().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据