history ls: fix '--format' flag to accept go templates#3683
history ls: fix '--format' flag to accept go templates#3683thaJeztah wants to merge 3 commits intodocker:masterfrom
Conversation
|
Ugh; we should disable this linter; it's too opinionated; in this case it's also .. wrong because there's fields being shadowed by wrappers 🤔 I'll push a "WIP" commit, but may open a PR to disable this linter (I know we disabled it elsewhere). |
4e31781 to
db5f3fc
Compare
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Before this PR, attempting to use a format template would always fail;
```bash
docker buildx history ls --no-trunc --format 'table {{.Status}}\t{{.NumTotalSteps}}\t{{.Duration}}\t{{.Name}}'
ERROR: template parsing error: template: :1:14: executing "" at <.NumTotalSteps>: can't evaluate field NumTotalSteps in type *history.lsContext
```
With this PR, formatting works (mostly); the formatting templates don't
match the JSON response (which can be confusing), and not all fields have
a table defined (which can result in `<no value>` used).
```bash
docker buildx history ls --no-trunc --format 'table {{.Status}}\t{{.NumTotalSteps}}\t{{.Duration}}\t{{.Name}}'
STATUS <no value> DURATION NAME
Completed 16 17.1s buildx (binaries)
Completed 12 19.9s buildkit/hack/dockerfiles/vendor.Dockerfile (update)
Completed 123 28.7s docker (dev-base)
Completed 26 7.9s
Completed 12 2m 41s cli/dockerfiles/Dockerfile.vendor (update)
```
Or a slightly more adventurous;
```bash
docker buildx history ls --no-trunc --format '{{printf "{\"status\":%s,\"steps\":%s,\"duration\":%s,\"name\":%s}" (json .Status) (json .NumTotalSteps) (json .Duration) (json .Name)}}'
{"status":"Completed","steps":16,"duration":"17.1s","name":"buildx (binaries)"}
{"status":"Completed","steps":12,"duration":"19.9s","name":"buildkit/hack/dockerfiles/vendor.Dockerfile (update)"}
{"status":"Completed","steps":123,"duration":"28.7s","name":"docker (dev-base)"}
{"status":"Completed","steps":26,"duration":"7.9s","name":""}
{"status":"Completed","steps":12,"duration":"2m 41s","name":"cli/dockerfiles/Dockerfile.vendor (update)"}
{"status":"Error","steps":11,"duration":"3m 14s","name":"cli/dockerfiles/Dockerfile.vendor (update)"}
```
The flag description has been updated to align with other `--format` flags;
```bash
docker buildx history ls --help
Usage: docker buildx history ls [OPTIONS]
List build records
Options:
--builder string Override the configured builder instance
-D, --debug Enable debug logging
--filter stringArray Provide filter values (e.g., "status=error")
--format string Format output using a custom template:
'table': Print output in table format with column headers (default)
'table TEMPLATE': Print output in table format using the given Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more information about
formatting output with templates (default "table")
--local List records for current repository only
--no-trunc Don't truncate output
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
db5f3fc to
314e30b
Compare
| "CreatedAt": lsHeaderCreated, | ||
| "Duration": lsHeaderDuration, | ||
| "Link": lsHeaderLink, | ||
| "BuildID": lsHeaderBuildID, |
There was a problem hiding this comment.
Is there a way for us to keep Ref and CreatedAt in here and maybe add a deprecated message? If this is a direct field access, maybe we can put it into the context as something like deprecatedAttribute which prints the name to stderr when its String() method is accessed? If it's a method, we can just add the print to the method itself.
There was a problem hiding this comment.
Yes, I can have a look; probably could also add both as template function, pointing to the same implementation .
Before this PR, attempting to use a format template would always fail;
With this PR, formatting works (mostly); the formatting templates don't match the JSON response (which can be confusing), and not all fields have a table defined (which can result in
<no value>used).Or a slightly more adventurous;
The flag description has been updated to align with other
--formatflags;