普通网友 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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。