<p>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 <code>helm/config/file.txt</code>, 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:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: one-line-cm
data:
  first-line:
    {{- range .Files.Lines &quot;config/file.txt&quot; }}
    {{ . }}
    {{ break }} # not a real thing
    {{- end }}
</code></pre>
<p>Unfortunately, <code>break</code> 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: <a href="https://stackoverflow.com/questions/60966946/helm-break-loop-range-in-template">Helm: break loop (range) in template</a></p>
<p>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.</p>

<p>EDIT:<br />
I've determined the following is the cleanest solution:</p>
<pre><code>.Files.Lines &quot;config/file.txt&quot; | first
</code></pre>
<p>(As a side note, I had to pipe to <code>squote</code> in my acutal solution due to my file contents containing special characters)</p>
<hr />
<p>After poking around in the helm <a href="https://helm.sh/docs/chart_template_guide/function_list/" rel="nofollow noreferrer">docs</a> for alternative functions, I came up with a solution that works, it's just not that pretty:</p>
<pre><code>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 }}
</code></pre>
<p>This is what's happening above (working inside outward):</p>
<ol>
<li><code>.Files.Get &quot;config/file.txt&quot;</code> is returning a string representation of the file contents.</li>
<li><code>regexSplit &quot;\\n&quot; &lt;step-1&gt; -1</code> is splitting the file contents from step-1 by newline (-1 means return the max number of substring matches possible)</li>
<li><code>index &lt;step-2&gt; 0</code> is grabbing the first item (index 0) from the list returned by step-2.</li>
</ol>
<p>Hope this is able to help others in similar situations, and I am still open to alternative solution suggestions.</p>
