确保Go模板中的路径始终以斜杠结尾

问题描述 投票:0回答:1

我正在为一系列部署编写Helm图表。我提供的值可以是:

[my_value: "/opt/my-path"my_value: "/opt/my-path/"

现在,我要确保路径的末尾始终只有一个/

如何使用Go模板执行此操作?

kubernetes-helm go-templates sprig-template-functions
1个回答
0
投票

您可以使用/函数修剪后缀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/"
© www.soinside.com 2019 - 2024. All rights reserved.