Skip to content

Create limactl tmpl yq command to query a template #3313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025

Conversation

jandubois
Copy link
Member

@jandubois jandubois commented Mar 6, 2025

The following commands are equivalent:

limactl tmpl yq $TEMPLATE $EXPRESSION
limactl tmpl copy --fill $TEMPLATE - | yq $EXPRESSION

The new yq variant is clearer, but more importantly does not require that yq is installed. Since we already include all the code in limactl anyways, we get this functionality almost for free1.

Example:

limactl tmpl yq template://default '.images[].location'
https://cloud-images.ubuntu.com/releases/24.10/release-20250129/ubuntu-24.10-server-cloudimg-amd64.img
https://cloud-images.ubuntu.com/releases/24.10/release-20250129/ubuntu-24.10-server-cloudimg-arm64.img
https://cloud-images.ubuntu.com/releases/24.10/release-20250129/ubuntu-24.10-server-cloudimg-riscv64.img
https://cloud-images.ubuntu.com/releases/24.10/release-20250129/ubuntu-24.10-server-cloudimg-armhf.img
https://cloud-images.ubuntu.com/releases/24.10/release/ubuntu-24.10-server-cloudimg-amd64.img
https://cloud-images.ubuntu.com/releases/24.10/release/ubuntu-24.10-server-cloudimg-arm64.img
https://cloud-images.ubuntu.com/releases/24.10/release/ubuntu-24.10-server-cloudimg-riscv64.img
https://cloud-images.ubuntu.com/releases/24.10/release/ubuntu-24.10-server-cloudimg-armhf.imglimactl tmpl yq template://default '.images[] | select(.arch == "aarch64").location'
https://cloud-images.ubuntu.com/releases/24.10/release-20250129/ubuntu-24.10-server-cloudimg-arm64.img
https://cloud-images.ubuntu.com/releases/24.10/release/ubuntu-24.10-server-cloudimg-arm64.img

Footnotes

  1. 10 lines for the Cobra definition and 20 lines for the implementation; the rest is just refactoring.

@jandubois jandubois force-pushed the tmpl-yq branch 2 times, most recently from 2d9e9e3 to f894b78 Compare March 6, 2025 06:01
templateYQCommand := &cobra.Command{
Use: "yq TEMPLATE EXPR",
Short: "Query template expressions",
Long: "Extract template data using a yq expression",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add how this differs from the standalone yq command

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference is that it works even when you don't have yq installed (e.g. on Windows GitHub runners). Not sure how you want to put this into the command help.

This is essentially the same as the gh api ... --jq option, having the tool already built in, so you don't need to worry about having it installed.


I'm planning to eventually add --yq options to limactl ls and limactl info as well, but not right now.

This comment was marked as outdated.

Copy link
Member Author

@jandubois jandubois Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the PR to produce the following output:

limactl tmpl yq --help
Use the builtin YQ evaluator to extract information from a template.
External references are embedded and default values are filled in
before the YQ expression is evaluated.

Example:
  limactl template yq template://default '.images[].location'

The example command is equivalent to using an external yq command like this:
  limactl template copy --fill template://default - | yq '.images[].location'

Usage:
  limactl template yq TEMPLATE EXPR [flags]
...

@jandubois jandubois force-pushed the tmpl-yq branch 2 times, most recently from 664f54a to 955d852 Compare March 6, 2025 06:33
The following commands are equivalent:

  limactl tmpl yq $TEMPLATE $EXPRESSION
  limactl tmpl copy --fill $TEMPLATE - | yq $EXPRESSION

The new `yq` variant is clearer, but more importantly does not require
that `yq` is installed. Since we already include all the code in limactl
anyways, we get this functionality almost for free.

Signed-off-by: Jan Dubois <[email protected]>
@jandubois jandubois requested a review from AkihiroSuda March 6, 2025 07:21
@AkihiroSuda AkihiroSuda added this to the v1.1.0 milestone Mar 7, 2025
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@AkihiroSuda AkihiroSuda merged commit 88f350b into lima-vm:master Mar 7, 2025
31 checks passed
@jandubois jandubois deleted the tmpl-yq branch March 7, 2025 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants