普通网友 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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3