【问题标题】:How to get all subdirectories in jsonnet?如何获取jsonnet中的所有子目录?
【发布时间】:2022-06-15 22:33:12
【问题描述】:

我正在使用 jsonnet 动态创建一个 gitlab-ci.yml 文件。 现在我想为我的客户目录中的每个目录创建一个作业。 为此,我想将所有目录名称读入一个数组。

我该怎么做?

伪代码如下所示: customers:: [c for c in getAllDirectories("path/to/customers-directory")]

非常感谢!

【问题讨论】:

    标签: jsonnet


    【解决方案1】:

    按照设计jsonnet 没有任何方法可以读取“环境数据”(广义地说:文件/目录/设备、环境变量等)。但是,它确实提供了通过 CLI 参数“注入”此数据的方法,请参阅 https://jsonnet.org/learning/tutorial.html#parameterize-entire-config

    在您的情况下,这可以通过以下示例代码来实现:

    测试目录

    $ mkdir -p /tmp/customers.d/{foo,bar,baz}
    $ ls -F /tmp/customers.d/
    bar/  baz/  foo/
    

    代码 (dirs.jsonnet)

    // Expect ext-var containing new-line separated list of dirs
    local customers = std.split(std.extVar('customers'), '\n');
    
    customers
    

    CLI 运行和输出

    $ jsonnet --ext-str customers="$(find /tmp/customers.d/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n')" dirs.jsonnet
    [
       "bar",
       "foo",
       "baz"
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-08
      • 2014-01-07
      • 2019-08-14
      • 2011-02-04
      • 2011-02-01
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多