英文:
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: ""
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论