加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何从bash调用参数化的Jsonnet?

发布时间:2020-12-15 21:26:03 所属栏目:安全 来源:网络整理
导读:我无法理解如何最好地参数化Jsonnet文件,以便我可以从bash和另一个Jsonnet文件调用相同的文件. 假设我有一个名为template.jsonnet的简单模板: { // Required arguments name:: error "'name' must be specified",port:: error "'port' must be specified",.
我无法理解如何最好地参数化Jsonnet文件,以便我可以从bash和另一个Jsonnet文件调用相同的文件.

假设我有一个名为template.jsonnet的简单模板:

{
  // Required arguments
  name:: error "'name' must be specified",port:: error "'port' must be specified",...,}

我可以很容易地将它合并到另一个Jsonnet文件中,并提供其所需的参数值:

{
  local template = = import "./template.jsonnet";

  template + {
    name:: "service",port:: 8080,}

我正在努力确定从bash调用template.jsonnet以达到相同结果的预期方式.

我可以使用–ext-str但这似乎需要std.extVar(x)

GitHub issue建议–tla-code可能是std.extVar()的替代品,但我不明白如何根据我的需要使用它.

后续问题是:如何对参数这是一个数组:

{
  local template = = import "./template.jsonnet";

  template + {
    name:: "service",port:: [8080,8081,8082],}

解决方法

最直接的方法是使用一些内联jsonnet:

jsonnet -e '(import "template.jsonnet") + { name: "service",port: 8080 }'

为了便于参数化,您可以使用extVars或顶级参数(TLA).

jsonnet -e 'function(name,port) (import "template.jsonnet") + { name: name,port: port }' --tla-str name="blah" --tla-code port='[8080,8081]'

要么

jsonnet -e '(import "template.jsonnet") + { name: std.extVar("name"),port: std.extVar("port") }' --ext-str name="blah" --ext-code port='[8080,8081]'

更好的方法是使template.jsonnet成为一个函数并使用–tla-code / –tla-str:

function(name,port) {
    name:: name,port:: port
    // Sidenote: the fields are hidden here,because they use ::,// use : to make them appear when the object is manifested.
    // Sidenote 2: You can provide default argument values. 
    // For example function(name,port=8080) makes port optional.
  }

然后在另一个jsonnet文件中,您可以按如下方式使用它:

local template = import "./template.jsonnet";
{

  filled_template: template(
    name="service",port=8080 // or port=[8080,8082]
  )
}

您可以使用shell中的模板,如下所示:

jsonnet --tla-code name='"some name"' --tla-code port=8080 template.jsonnet

请注意名称是如何引用标记的(没有外部’它们将被shell解释).那是因为您可以传递任何评估为tla-code中任何类型的jsonnet代码.

如果你想逐字传递一个字符串,你可以使用–tla-str:

jsonnet --tla-str name="some name" --tla-code port=8080 template.jsonnet

另一方面,您可以将数组(或对象或任何jsonnet代码)传递给–tla-code:

jsonnet --tla-code name='"some name"' --tla-code port='[8080,8082]' template.jsonnet

或者,如果您不想更改template.jsonnet,则可以使用包装器文件来提供我所描述的接口:

template_func.jsonnet:

local template = import "./template.jsonnet";
function(name,port) template + {
  name: name,port: port
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读