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() + } +}