普通网友 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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,