我正在为一系列部署编写Helm图表。我提供的值可以是:
[my_value: "/opt/my-path"
或my_value: "/opt/my-path/"
现在,我要确保路径的末尾始终只有一个/
。
如何使用Go
模板执行此操作?
您可以使用/
函数修剪后缀trimSuffix
,在此处http://masterminds.github.io/sprig/strings.html进行说明,并在末尾手动添加/
。因此,无论原始值是多少,您始终都会在末尾获得/
。例子
values.yaml:
path_with_slash: "/my/path/"
path_without_slash: "/my/path"
在模板文件中:
{{ $path_with_slash := trimSuffix "/" .Values.path_with_slash }}
{{ $path_without_slash := trimSuffix "/" .Values.path_without_slash }}
path_with_slash: "{{ $path_with_slash }}/"
path_without_slash: "{{ $path_without_slash }}/"
渲染文件:
path_with_slash: "/my/path/"
path_without_slash: "/my/path/"