Search code examples
dockergogo-templates

How do you left align a printf on Docker format


I am trying to align a set of docker service ps calls together using printf but it seems to align things on the right when I use printf

for service in $(command docker service ls --format "{{.Name}}")
do
  command docker service ps \
    --format "table {{ .Name | printf \"%20.20s\" }}\t{{ .Image | printf \"%40.40s\" }}\t{{ .Node | printf \"%20.20s\" }}\t{{.CurrentState}}\t{{.Error}}" \
    $service
done

How do I make the printf left align?


Solution

  • The template's printf function uses the fmt package. The fmt package documents there's a - flag:

    - pad with spaces on the right rather than the left (left-justify the field)
    

    So instead of

    {{ printf "%20.20s" }}
    

    Simply use

    {{ printf "%-20.20s" }}