Skip to content

Commit a5dc386

Browse files
committed
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 <jan.dubois@suse.com>
1 parent 22af485 commit a5dc386

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ jobs:
6060
# the allow list corresponds to https://github.com/cncf/foundation/blob/e5db022a0009f4db52b89d9875640cf3137153fe/allowed-third-party-license-policy.md
6161
run: go-licenses check --include_tests ./... --allowed_licenses=$(cat ./hack/allowed-licenses.txt)
6262
- name: Check license boilerplates
63-
run: go tool -modfile=./hack/tools/go.mod ltag -t ./hack/ltag --check -v
63+
# the --excludes option takes directory names, not paths, so we can't specify "cmd/yq" but just "yq"
64+
run: go tool -modfile=./hack/tools/go.mod ltag -t ./hack/ltag --check --excludes yq -v
6465
- name: Check protobuf files
6566
run: go tool -modfile=./hack/tools/go.mod protolint .
6667

cmd/lima-guestagent/main_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"github.com/sirupsen/logrus"
1010
"github.com/spf13/cobra"
1111

12+
"github.com/lima-vm/lima/v2/cmd/yq"
1213
"github.com/lima-vm/lima/v2/pkg/debugutil"
1314
"github.com/lima-vm/lima/v2/pkg/version"
1415
)
1516

1617
func main() {
18+
yq.MaybeRunYQ()
1719
if err := newApp().Execute(); err != nil {
1820
logrus.Fatal(err)
1921
}

cmd/limactl/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/sirupsen/logrus"
1818
"github.com/spf13/cobra"
1919

20+
"github.com/lima-vm/lima/v2/cmd/yq"
2021
"github.com/lima-vm/lima/v2/pkg/debugutil"
2122
"github.com/lima-vm/lima/v2/pkg/driver/external/server"
2223
"github.com/lima-vm/lima/v2/pkg/fsutil"
@@ -32,6 +33,7 @@ const (
3233
)
3334

3435
func main() {
36+
yq.MaybeRunYQ()
3537
if runtime.GOOS == "windows" {
3638
extras, hasExtra := os.LookupEnv("_LIMA_WINDOWS_EXTRA_PATH")
3739
if hasExtra && strings.TrimSpace(extras) != "" {

cmd/yq/yq.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-FileCopyrightText: Copyright (c) 2017 Mike Farah
3+
// SPDX-License-Identifier: MIT
4+
5+
// This file has been adapted from https://github.com/mikefarah/yq/blob/master/yq.go
6+
7+
package yq
8+
9+
import (
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
14+
command "github.com/mikefarah/yq/v4/cmd"
15+
)
16+
17+
func main() {
18+
cmd := command.New()
19+
args := os.Args[1:]
20+
_, _, err := cmd.Find(args)
21+
if err != nil && args[0] != "__complete" {
22+
// default command when nothing matches...
23+
newArgs := []string{"eval"}
24+
cmd.SetArgs(append(newArgs, os.Args[1:]...))
25+
}
26+
code := 0
27+
if err := cmd.Execute(); err != nil {
28+
code = 1
29+
}
30+
os.Exit(code)
31+
}
32+
33+
// MaybeRunYQ runs as `yq` if the program name or first argument is `yq`.
34+
// Only returns to caller if os.Args doesn't contain a `yq` command.
35+
func MaybeRunYQ() {
36+
progName := filepath.Base(os.Args[0])
37+
progName = strings.TrimSuffix(progName, filepath.Ext(progName))
38+
if progName == "yq" {
39+
main()
40+
}
41+
if len(os.Args) > 1 && os.Args[1] == "yq" {
42+
os.Args = os.Args[1:]
43+
main()
44+
}
45+
}

0 commit comments

Comments
 (0)