将文件解析为Helm模板。

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

Parsing file into helm template

问题

我尝试解析文件:

VAR1=https://com.dom
VAR2=abcd=efg=

使用以下模板:

{{- range $path, $_ :=  $.Files.Glob $envPath }}
    {{- range split "\n" ($.Files.Get $path) }}
      {{- if . }}
        {{- $kv := splitList "=" . -}}
          {{- $k := first $kv}}
          {{- if contains "RAW_" $k }}
            {{- first $kv | replace "RAW_" "" | nindent 2}}: {{last $kv | quote }}
            {{ else}}
            {{- first $kv | nindent 2}}: {{last $kv | replace "'" "" | b64enc | quote }}
          {{-  end }}
  {{-  end }}
    {{- end }}
{{- end }}
{{- end }}

得到结果:

data:
  VAR1: "aHR0cHM6Ly9jb20uZG9t"
  VAR2: ""

因为splitList获取的是最后一个"=",但我希望splitList只将第一个"="作为分隔符。注意:base64哈希值中有"="。

英文:

I try parsing file

VAR1=https://com.dom
VAR2=abcd=efg=

by template

{{- range $path, $_ :=  $.Files.Glob $envPath }}
    {{- range split "\n" ($.Files.Get $path) }}
      {{- if . }}
        {{- $kv := splitList "=" . -}}
          {{- $k := first $kv}}
          {{- if contains "RAW_" $k }}
            {{- first $kv | replace "RAW_" "" | nindent 2}}: {{last $kv | quote }}
            {{ else}}
            {{- first $kv | nindent 2}}: {{last $kv | replace "'" "" | b64enc | quote }}
          {{-  end }}
  {{-  end }}
    {{- end }}
{{- end }}
{{- end }}

and got

data:
  VAR1: "aHR0cHM6Ly9jb20uZG9t"
  VAR2: ""

becouse splitList get last "=", but I want that splitList take only first "=" will take as seporator.
note: base64 have "=" at your hashes.

答案1

得分: 1

.
├── Chart.yaml
├── charts
├── prop
│   └── varFile
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── configmap.yaml
│   └── deployment.yaml
└── values.yaml

prop/varFile

VAR1=https://com.dom
VAR2=abcd=efg=

VAR4

VAR5=

values.yaml

varFile: "prop/varFile"

templates/configmap.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  data: |-
    {{- range .Files.Lines .Values.varFile }}
    {{- $arr := split "=" . }}
    {{- $k := $arr._0 }}
    {{- if ge (len $arr) 2 }}
    {{ $k }}: {{ substr (int (add 1 (len $k))) (len .) . | b64enc | quote }}
    {{- else }}
    {{- if ge (len $k) 1 }}
    {{ $k }}: ""
    {{- end }}
    {{- end }}
    {{- end }}    

output:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  data: |-
    VAR1: "aHR0cHM6Ly9jb20uZG9t"
    VAR2: "YWJjZD1lZmc9"
    VAR4: ""
    VAR5: ""    
英文:

tree

.
├── Chart.yaml
├── charts
├── prop
│   └── varFile
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── configmap.yaml
│   └── deployment.yaml
└── values.yaml

prop/varFile

VAR1=https://com.dom
VAR2=abcd=efg=

VAR4

VAR5=

values.yaml

varFile: "prop/varFile"

templates/configmap.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  data: |-
    {{- range .Files.Lines .Values.varFile }}
    {{- $arr := split "=" . }}
    {{- $k := $arr._0 }}
    {{- if ge (len $arr) 2 }}
    {{ $k }}: {{ substr (int (add 1 (len $k))) (len .) . | b64enc | quote }}
    {{- else }}
    {{- if ge (len $k) 1 }}
    {{ $k }}: ""
    {{- end }}
    {{- end }}
    {{- end }}    

output:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data: 
  data: |-
    VAR1: "aHR0cHM6Ly9jb20uZG9t"
    VAR2: "YWJjZD1lZmc9"
    VAR4: ""
    VAR5: ""    

huangapple
  • 本文由 发表于 2022年8月4日 22:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/73237495.html
匿名

发表评论

匿名网友

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

确定