有没有一种简单的方法可以使用Helm从文件中获取第一行?

huangapple go评论81阅读模式
英文:

Is there a simple way to grab the first line from a file with helm?

问题

我正在尝试使用Helm创建一个Kubernetes ConfigMap,它只包含配置文件中的第一行内容。我将文件放在helm/config/file.txt中,其中包含多行内容,但我只想提取第一行。我首次尝试的方法是循环遍历文件的每一行,但在第一次循环后退出:

apiVersion: v1
kind: ConfigMap
metadata:
  name: one-line-cm
data:
  first-line:
    {{- range .Files.Lines "config/file.txt" }}
    {{ . }}
    {{ break }} # 这不是真实的语法
    {{- end }}

不幸的是,break在Helm中似乎不是一个概念/函数,尽管它在Golang中是存在的。我通过艰难的方式发现了这一点,并在这篇文章中阅读到了一个类似的问题:https://stackoverflow.com/questions/60966946/helm-break-loop-range-in-template

我并不固执于使用循环,我只是想知道是否有其他解决方案来执行使用Helm语法从文件中提取第一行的简单任务。

英文:

I am attempting to create a kubernetes ConfigMap with helm, which simply consists of the first line within a config file. I put my file in helm/config/file.txt, which has several lines of content, but I only want to extract the first. My first attempt at this was to loop over the lines of the file (naturally), but quit out after the first loop:

apiVersion: v1
kind: ConfigMap
metadata:
  name: one-line-cm
data:
  first-line:
    {{- range .Files.Lines "config/file.txt" }}
    {{ . }}
    {{ break }} # not a real thing
    {{- end }}

Unfortunately, break doesn't seem to be a concept/function in helm, even though it is within golang. I discovered this the hard way, as well as reading about a similar question in this other post: https://stackoverflow.com/questions/60966946/helm-break-loop-range-in-template

I'm not stuck on using a loop, I'm just wondering if there's another solution to perform the simple task of extracting the first line from a file with helm syntax.

答案1

得分: 1

我已经确定以下是最简洁的解决方案:

.Files.Lines "config/file.txt" | first

(顺便提一下,由于我的文件内容包含特殊字符,我在实际解决方案中必须使用squote进行管道处理)


在浏览Helm的文档以寻找替代函数后,我找到了一个可行的解决方案,只是不太美观:

apiVersion: v1
kind: ConfigMap
metadata:
  name: one-line-cm
data:
  first-line: |
    {{ index (regexSplit "\n" (.Files.Get "config/file.txt") -1) 0 }}

上面的代码实现了以下功能(从内到外):

  1. .Files.Get "config/file.txt" 返回文件内容的字符串表示。
  2. regexSplit "\n" <step-1> -1 将步骤1中的文件内容按换行符分割(-1 表示返回尽可能多的子字符串匹配)。
  3. index <step-2> 0 从步骤2返回的列表中获取第一项(索引为0)。

希望这能帮助到其他遇到类似情况的人,我仍然欢迎其他解决方案的建议。

英文:

EDIT:
I've determined the following is the cleanest solution:

.Files.Lines &quot;config/file.txt&quot; | first

(As a side note, I had to pipe to squote in my acutal solution due to my file contents containing special characters)


After poking around in the helm docs for alternative functions, I came up with a solution that works, it's just not that pretty:

apiVersion: v1
kind: ConfigMap
metadata:
  name: one-line-cm
data:
  first-line: |
    {{ index (regexSplit &quot;\\n&quot; (.Files.Get &quot;config/file.txt&quot;) -1) 0 }}

This is what's happening above (working inside outward):

  1. .Files.Get &quot;config/file.txt&quot; is returning a string representation of the file contents.
  2. regexSplit &quot;\\n&quot; &lt;step-1&gt; -1 is splitting the file contents from step-1 by newline (-1 means return the max number of substring matches possible)
  3. index &lt;step-2&gt; 0 is grabbing the first item (index 0) from the list returned by step-2.

Hope this is able to help others in similar situations, and I am still open to alternative solution suggestions.

huangapple
  • 本文由 发表于 2022年5月12日 07:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/72208634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定