From 29e8c44f24adea289b51158f8f970f57e05d3e5a Mon Sep 17 00:00:00 2001 From: Jan Dubois Date: Wed, 27 Aug 2025 00:23:40 -0700 Subject: [PATCH] Add yq subcommands to limactl and lima-guestagent Adding it to `lima-guestagent` allows us to use `yq` in provisioning scripts without having to install it from a package repository or downloading it from GitHub. Adding it to `limactl` makes it available to integration tests without having to install it. It is essentially free because we already use `yqlib` inside `limactl` anyways, , so the executable size doesn't really change. The size of the (compressed) guestagent for aarch64 increases from 12MB to 14MB, which seems acceptable. These are "hidden" commands that don't show up in --help output. Signed-off-by: Jan Dubois --- cmd/lima-guestagent/main_linux.go | 2 ++ cmd/limactl/main.go | 2 ++ cmd/yq/yq.go | 47 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 cmd/yq/yq.go diff --git a/cmd/lima-guestagent/main_linux.go b/cmd/lima-guestagent/main_linux.go index 875880a60c1..1e203c4ce0b 100644 --- a/cmd/lima-guestagent/main_linux.go +++ b/cmd/lima-guestagent/main_linux.go @@ -9,11 +9,13 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/lima-vm/lima/v2/cmd/yq" "github.com/lima-vm/lima/v2/pkg/debugutil" "github.com/lima-vm/lima/v2/pkg/version" ) func main() { + yq.MaybeRunYQ() if err := newApp().Execute(); err != nil { logrus.Fatal(err) } diff --git a/cmd/limactl/main.go b/cmd/limactl/main.go index 8b5c5a51c22..05074f0f6b6 100644 --- a/cmd/limactl/main.go +++ b/cmd/limactl/main.go @@ -17,6 +17,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/lima-vm/lima/v2/cmd/yq" "github.com/lima-vm/lima/v2/pkg/debugutil" "github.com/lima-vm/lima/v2/pkg/driver/external/server" "github.com/lima-vm/lima/v2/pkg/fsutil" @@ -32,6 +33,7 @@ const ( ) func main() { + yq.MaybeRunYQ() if runtime.GOOS == "windows" { extras, hasExtra := os.LookupEnv("_LIMA_WINDOWS_EXTRA_PATH") if hasExtra && strings.TrimSpace(extras) != "" { diff --git a/cmd/yq/yq.go b/cmd/yq/yq.go new file mode 100644 index 00000000000..0203f16dc5c --- /dev/null +++ b/cmd/yq/yq.go @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: Copyright The Lima Authors +// SPDX-License-Identifier: Apache-2.0 + +// SPDX-FileCopyrightText: Copyright (c) 2017 Mike Farah + +// This file has been adapted from https://github.com/mikefarah/yq/blob/v4.47.1/yq.go + +package yq + +import ( + "os" + "path/filepath" + "strings" + + command "github.com/mikefarah/yq/v4/cmd" +) + +func main() { + cmd := command.New() + args := os.Args[1:] + _, _, err := cmd.Find(args) + if err != nil && args[0] != "__complete" { + // default command when nothing matches... + newArgs := []string{"eval"} + cmd.SetArgs(append(newArgs, os.Args[1:]...)) + } + code := 0 + if err := cmd.Execute(); err != nil { + code = 1 + } + os.Exit(code) +} + +// MaybeRunYQ runs as `yq` if the program name or first argument is `yq`. +// Only returns to caller if os.Args doesn't contain a `yq` command. +func MaybeRunYQ() { + progName := filepath.Base(os.Args[0]) + // remove all extensions, so we match "yq.lima.exe" + progName, _, _ = strings.Cut(progName, ".") + if progName == "yq" { + main() + } + if len(os.Args) > 1 && os.Args[1] == "yq" { + os.Args = os.Args[1:] + main() + } +}