Skip to content

Commit cfa73e3

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 cfa73e3

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

LICENSE_YQ

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Below is a copy of https://github.com/mikefarah/yq/blob/master/LICENSE
2+
3+
This file is included in the Lima repository because `limactl yq` and
4+
`lima-hostagent yq` provide an embedded version of the `yq` utility.
5+
This license applies specifically to the embedded `yq` functionality;
6+
the rest of this repository is licensed under Apache 2.0.
7+
8+
------------------------------------------------------------------------------
9+
10+
The MIT License (MIT)
11+
12+
Copyright (c) 2017 Mike Farah
13+
14+
Permission is hereby granted, free of charge, to any person obtaining a copy
15+
of this software and associated documentation files (the "Software"), to deal
16+
in the Software without restriction, including without limitation the rights
17+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
copies of the Software, and to permit persons to whom the Software is
19+
furnished to do so, subject to the following conditions:
20+
21+
The above copyright notice and this permission notice shall be included in all
22+
copies or substantial portions of the Software.
23+
24+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30+
SOFTWARE.

cmd/lima-guestagent/main_linux.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,40 @@
44
package main
55

66
import (
7+
"os"
78
"strings"
89

10+
yq "github.com/mikefarah/yq/v4/cmd"
911
"github.com/sirupsen/logrus"
1012
"github.com/spf13/cobra"
1113

1214
"github.com/lima-vm/lima/v2/pkg/debugutil"
1315
"github.com/lima-vm/lima/v2/pkg/version"
1416
)
1517

18+
// yqmain is a copy of main() in https://github.com/mikefarah/yq/blob/master/yq.go
19+
func yqmain() {
20+
cmd := yq.New()
21+
args := os.Args[1:]
22+
_, _, err := cmd.Find(args)
23+
if err != nil && args[0] != "__complete" {
24+
// default command when nothing matches...
25+
newArgs := []string{"eval"}
26+
cmd.SetArgs(append(newArgs, os.Args[1:]...))
27+
}
28+
if err := cmd.Execute(); err != nil {
29+
os.Exit(1) //nolint:revive // it's intentional to call os.Exit in this function
30+
}
31+
}
32+
1633
func main() {
34+
// `lima-guestagent yq` executes the embedded `yq` command instead.
35+
if len(os.Args) > 1 && os.Args[1] == "yq" {
36+
os.Args = os.Args[1:]
37+
yqmain()
38+
return
39+
}
40+
1741
if err := newApp().Execute(); err != nil {
1842
logrus.Fatal(err)
1943
}

cmd/limactl/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/mattn/go-isatty"
17+
yq "github.com/mikefarah/yq/v4/cmd"
1718
"github.com/sirupsen/logrus"
1819
"github.com/spf13/cobra"
1920

@@ -31,7 +32,28 @@ const (
3132
advancedCommand = "advanced"
3233
)
3334

35+
// yqmain is a copy of main() in https://github.com/mikefarah/yq/blob/master/yq.go
36+
func yqmain() {
37+
cmd := yq.New()
38+
args := os.Args[1:]
39+
_, _, err := cmd.Find(args)
40+
if err != nil && args[0] != "__complete" {
41+
// default command when nothing matches...
42+
newArgs := []string{"eval"}
43+
cmd.SetArgs(append(newArgs, os.Args[1:]...))
44+
}
45+
if err := cmd.Execute(); err != nil {
46+
os.Exit(1) //nolint:revive // it's intentional to call os.Exit in this function
47+
}
48+
}
49+
3450
func main() {
51+
// `limactl yq` executes the embedded `yq` command instead.
52+
if len(os.Args) > 1 && os.Args[1] == "yq" {
53+
os.Args = os.Args[1:]
54+
yqmain()
55+
return
56+
}
3557
if runtime.GOOS == "windows" {
3658
extras, hasExtra := os.LookupEnv("_LIMA_WINDOWS_EXTRA_PATH")
3759
if hasExtra && strings.TrimSpace(extras) != "" {

0 commit comments

Comments
 (0)