英文:
Get the last element of a docker image path using Go templates
问题
我想生成一个合成的docker ps表格,但我的docker镜像路径非常长。我尝试的解决方案是只显示镜像名称的最后一部分:
这是我目前的代码:
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Command}}"
我希望的结果是:
docker ps --format "table {{.Names}}\t{{ <magic .Image> }}\t{{.Status}}\t{{.Command}}"
所以基本上,我想要获取最后一个/之后的内容,就像basename命令一样。
这是我尝试过的代码:
# 没有固定长度
docker ps --format "table {{ slice .Image 0 10}}"
# 没有最后一个索引可用
docker ps --format 'table {{ (split "/" .Image) }}'
非常感谢任何帮助或解决方法。
附注:由于我使用docker cli,我不认为我可以定义自定义函数来在模板表达式中使用。
英文:
I would like to generate a synthetic docker ps table but my docker image paths are very long. The solution I am trying to do is to display only the last part of the image name:
This is what I have:
docker ps --format "table {{.Names}}\\t{{.Image}}\\t{{.Status}}\\t{{.Command}}"
NAMES IMAGE STATUS COMMAND
a_container a_local_image Up 36 minutes "python…"
another_container registry.example.com/group/subgroup/project/my_image:latest Up 38 minutes "go…"
I would like:
docker ps --format "table {{.Names}}\\t{{ <magic .Image> }}\\t{{.Status}}\\t{{.Command}}"
NAMES IMAGE STATUS COMMAND
a_container a_local_image Up 36 minutes "python…"
another_container my_image:latest Up 38 minutes "go…"
so basically, get what is after the last /, like basename does.
This is what I tried:
# No fixed length
docker ps --format "table {{ slice .Image 0 10}}"
# No last index available
docker ps --format 'table {{ (split "/" .Image) }}'
Any help or workaround is appreciated.
p.s.: since I use docker cli, I don't think I can define custom functions to use in the template expression.
答案1
得分: 2
获取模板数组的最后一个元素没有好的方法。即使可以使用index和len内置函数,也不能使用算术运算来获取len - 1。
以下是一个仅限于模板的技巧:
{{ $image := "" }}{{ range split .Image "/" }}{{ $image = . }}{{ end }}{{ $image }}
基本上它执行以下操作:
- 声明一个变量
$image - 在
/上使用split函数对.Image进行分割 - 遍历数组字符串并将其赋值给
$image - 在遍历结束时,
$image将保存最后一个元素的值 - 打印
$image
完整的命令(使用封闭的撇号'以避免转义引号):
docker ps --format 'table {{.Names}}\t{{ $image := "" }}{{ range split .Image "/" }}{{ $image = . }}{{ end }}{{ $image }}\t{{.Status}}\t{{.Command}}'
英文:
There is no good way to get the last element of a template array. Even though you can use index and len built-ins, you can't use arithmetic to get len - 1.
This is a template-only trick:
{{ $image := "" }}{{ range split .Image "/" }}{{ $image = . }}{{ end }}{{ $image }}
Basically it does the following:
- declare a var
$image splitthe.Imageon/- iterate over the array string and just assign to
$image - at the end of the range,
$imagewill hold the value of the last element - print
$image
Full command (with enclosing apex ' so you don't have to escape quotes):
docker ps --format 'table {{.Names}}\t{{ $image := "" }}{{ range split .Image "/" }}{{ $image = . }}{{ end }}{{ $image }}\t{{.Status}}\t{{.Command}}'
答案2
得分: 0
你可以尝试以下解决方法,我将其放在一个shell文件中,因为它太长了,但你也可以直接在shell中调用它。
test.sh
paste <(docker ps --format 'table {{.ID}}') \
<(docker ps --format 'table {{.Image}}' | awk -F '/' '{print $NF}') \
<(docker ps --format 'table {{.Command}} {{.CreatedAt}} {{.Status}} {{.Ports}} {{.Names}}')
解释:
docker ps --format 'table {{.ID}}'获取docker ps命令的ID列docker ps --format 'table {{.Image}}' | awk -F '/' '{print $NF}'),首先获取Image列,然后使用awk命令分割/,获取最后一个子列作为你所需的结果。- 剩下的部分用于获取
docker ps的其他列,根据你的需求。 - 最后,使用
paste命令将上述命令的输出合并在一起。
英文:
You may could use next workaround, I put it in a shell file just because it's too long, but you are ok to call it directly in shell.
test.sh
paste <(docker ps --format 'table {{.ID}}') \
<(docker ps --format 'table {{.Image}}' | awk -F '/' '{print $NF}') \
<(docker ps --format 'table {{.Command}} {{.CreatedAt}} {{.Status}} {{.Ports}} {{.Names}}')
Explain:
docker ps --format 'table {{.ID}}'get theIDcolumn ofdocker psdocker ps --format 'table {{.Image}}' | awk -F '/' '{print $NF}'), frist get theImagecolumn, then useawkto split/, get the last sub column as you required.- the left is used to get remained columns as you needed of
docker ps - Finally, use
pastecommand to combine the output of above command.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论