使用Go模板获取Docker镜像路径的最后一个元素。

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

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 &quot;table {{.Names}}\\t{{.Image}}\\t{{.Status}}\\t{{.Command}}&quot;

NAMES                    IMAGE                                                           STATUS          COMMAND
a_container              a_local_image                                                   Up 36 minutes   &quot;python…&quot;
another_container        registry.example.com/group/subgroup/project/my_image:latest     Up 38 minutes   &quot;go…&quot;

I would like:

docker ps --format &quot;table {{.Names}}\\t{{ &lt;magic .Image&gt; }}\\t{{.Status}}\\t{{.Command}}&quot;

NAMES                    IMAGE               STATUS          COMMAND
a_container              a_local_image       Up 36 minutes   &quot;python…&quot;
another_container        my_image:latest     Up 38 minutes   &quot;go…&quot;

so basically, get what is after the last /, like basename does.

This is what I tried:

# No fixed length
docker ps --format &quot;table {{ slice .Image  0 10}}&quot;

#&#160;No last index available
docker ps --format &#39;table {{ (split &quot;/&quot; .Image) }}&#39;

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

获取模板数组的最后一个元素没有好的方法。即使可以使用indexlen内置函数,也不能使用算术运算来获取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 := &quot;&quot; }}{{ range split .Image &quot;/&quot; }}{{ $image = . }}{{ end }}{{ $image }}

Basically it does the following:

  • declare a var $image
  • split the .Image on /
  • iterate over the array string and just assign to $image
  • at the end of the range, $image will hold the value of the last element
  • print $image

Full command (with enclosing apex &#39; so you don't have to escape quotes):

docker ps --format &#39;table {{.Names}}\t{{ $image := &quot;&quot; }}{{ range split .Image &quot;/&quot; }}{{ $image = . }}{{ end }}{{ $image }}\t{{.Status}}\t{{.Command}}&#39;

答案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 &lt;(docker ps --format &#39;table {{.ID}}&#39;) \
    &lt;(docker ps --format &#39;table {{.Image}}&#39; | awk -F &#39;/&#39; &#39;{print $NF}&#39;) \
    &lt;(docker ps --format &#39;table {{.Command}} {{.CreatedAt}} {{.Status}} {{.Ports}} {{.Names}}&#39;)

Explain:

  • docker ps --format &#39;table {{.ID}}&#39; get the ID column of docker ps
  • docker ps --format &#39;table {{.Image}}&#39; | awk -F &#39;/&#39; &#39;{print $NF}&#39;), frist get the Image column, then use awk to 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 paste command to combine the output of above command.

huangapple
  • 本文由 发表于 2021年9月11日 15:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/69140937.html
匿名

发表评论

匿名网友

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

确定