Skip to content

Conversation

@jandubois
Copy link
Member

@jandubois jandubois commented Sep 7, 2025

The --yq option is only compatible with JSON or YAML output and will apply the yq expression to the result before writing it to the output.

The following 2 commands are functionally identical (but may result in different formatting, see below):

limactl ls default --yq .dir
limactl ls default --format json | limactl yq .dir

The output of limactl list --json will be pretty-printed when it goes to a terminal. This means it is no longer one line per JSON object, but has much improved legibility. Output to a file or pipe maintains JSON Lines rules.

Also colorize the output of limactl list, limactl info, limactl tmpl copy, and limactl tmpl yq if the output goes to the terminal.

Unfortunately the output of yamlfmt cannot be colorized, so the terminal output may look slightly different from the output to a file or pipe (mostly the extra indentation of list elements), but this seems like a reasonable compromise.

Sample screenshot to show the difference:

CleanShot 2025-09-07 at 13 53 31@2x

The colors come from https://github.com/mikefarah/yq/blob/master/pkg/yqlib/color_print.go and are not configurable.

@jandubois jandubois added this to the v2.0.0 milestone Sep 7, 2025
@jandubois jandubois force-pushed the yq-options branch 5 times, most recently from a0187a0 to dcc3e31 Compare September 7, 2025 21:34
The --yq option is only compatible with JSON or YAML output and will
apply the yq expression to the result before writing it to the output.

The following 2 commands are functionally identical (but may result in different
formatting, see below):

    limactl ls default --yq .dir
    limactl ls default --format json | limactl yq .dir

The output of `limactl list --json` will be pretty-printed when it goes
to a terminal. This means it is no longer one line per JSON object, but
has much improved legibility. Output to a file or pipe maintains JSON
Lines rules.

Also colorize the output of `limactl list`, `limactl info`, `limactl tmpl copy`,
and `limactl tmpl yq` if the output goes to the terminal.

Unfortunately the output of `yamlfmt` cannot be colorized, so the terminal
output may look slightly different from the output to a file or pipe (mostly
the extra indentation of list elements), but this seems like a reasonable
compromise.

Signed-off-by: Jan Dubois <[email protected]>
@AkihiroSuda
Copy link
Member

Why not use limactl list --format '{{.Dir}}' ?

@jandubois
Copy link
Member Author

Why not use limactl list --format '{{.Dir}}' ?

l ls default --format '{{.Config.User}}'
{0x14000a033e0 0x14000a033f0 0x14000a034d0 0x14000a03410 0x1400092fdd8}l ls default --yq .config.user
{
    "name": "jan",
    "comment": "Jan Dubois",
    "home": "/home/jan.linux",
    "shell": "/bin/bash",
    "uid": 501
}

@jandubois
Copy link
Member Author

And to elaborate on this a bit more, limactl ls --list-fields tells you about the Config field (without further explanation), but limactl ls default --format '{{.Config}} doesn't help you to figure out the structure underneath it.

You have to know to look into https://github.com/lima-vm/lima/blob/master/pkg/limatype/lima_yaml.go to find out the Go internal structure.

With --yq you just use the structure that is returned by limactl ls default --format json and take it from there. Everything is easily discoverable. For a non-Lima developer, the .Config value is totally opaque.

I would consider the Go template format a legacy option that we added because we didn't have anything better yet.

@AkihiroSuda
Copy link
Member

❯ l ls default --format '{{.Config.User}}'
{0x14000a033e0 0x14000a033f0 0x14000a034d0 0x14000a03410 0x1400092fdd8}

--format '{{json .Config.User}}' should work

@jandubois
Copy link
Member Author

--format '{{json .Config.User}}' should work

It does, but it is still obscure. How do you know the struct field is called .User?

It makes much more sense for the user to be able to use the same field name that is used in lima.yaml.

Why do they have to check the Lima source code to figure out how to write the template expression?

@jandubois
Copy link
Member Author

jandubois commented Sep 8, 2025

Using --yq is also about more than simply selecting a field from the structure. You can use it to filter the list of your instances according to whatever criteria you want (see #3240, which we probably should remove from the 2.0 milestone).

l ls --yq 'select(.config.vmType == "vz").name'
bar
clone
defaultl ls --yq 'select(.config.vmType == "qemu").name'
qemul ls --yq 'select(.status == "Running").name'
default
qemu

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 9eedf51 into lima-vm:master Sep 8, 2025
36 checks passed
@AkihiroSuda
Copy link
Member

Probably we should have bats tests

@jandubois jandubois deleted the yq-options branch September 8, 2025 05:20
@jandubois
Copy link
Member Author

Probably we should have bats tests

They will come. I've been working on BATS tests; that how I found some of the issues with limactl ls, like the JSON and YAML data being different.

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