Skip to content

Commit f015da5

Browse files
committed
limactl: add --cpus, --memory, --mount-type, --vm-type, ...
Signed-off-by: Akihiro Suda <[email protected]>
1 parent f02b86d commit f015da5

File tree

4 files changed

+101
-10
lines changed

4 files changed

+101
-10
lines changed

cmd/limactl/edit.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/AlecAivazis/survey/v2"
12+
"github.com/lima-vm/lima/cmd/limactl/editflags"
1113
"github.com/lima-vm/lima/pkg/editutil"
1214
"github.com/lima-vm/lima/pkg/limayaml"
1315
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
@@ -30,7 +32,7 @@ func newEditCommand() *cobra.Command {
3032
}
3133
// TODO: "survey" does not support using cygwin terminal on windows yet
3234
editCommand.Flags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "enable TUI interactions such as opening an editor, defaults to true when stdout is a terminal")
33-
editCommand.Flags().String("set", "", "modify the template inplace, using yq syntax")
35+
editflags.RegisterEdit(editCommand)
3436
return editCommand
3537
}
3638

@@ -57,17 +59,18 @@ func editAction(cmd *cobra.Command, args []string) error {
5759
if err != nil {
5860
return err
5961
}
60-
tty, err := cmd.Flags().GetBool("tty")
62+
flags := cmd.Flags()
63+
tty, err := flags.GetBool("tty")
6164
if err != nil {
6265
return err
6366
}
64-
yq, err := cmd.Flags().GetString("set")
67+
yqExprs, err := editflags.YQExpressions(flags)
6568
if err != nil {
6669
return err
6770
}
6871
var yBytes []byte
69-
if yq != "" {
70-
logrus.Warn("`--set` is experimental")
72+
if len(yqExprs) > 0 {
73+
yq := strings.Join(yqExprs, " | ")
7174
yBytes, err = yqutil.EvaluateExpression(yq, yContent)
7275
if err != nil {
7376
return err

cmd/limactl/editflags/editflags.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package editflags
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/sirupsen/logrus"
7+
"github.com/spf13/cobra"
8+
flag "github.com/spf13/pflag"
9+
)
10+
11+
// RegisterEdit registers flags related to in-place YAML modification.
12+
func RegisterEdit(cmd *cobra.Command) {
13+
flags := cmd.Flags()
14+
15+
flags.Int("cpus", 0, "number of CPUs") // Similar to colima's --cpu
16+
_ = cmd.RegisterFlagCompletionFunc("cpus", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
17+
return []string{"1", "2", "4", "8"}, cobra.ShellCompDirectiveNoFileComp
18+
})
19+
flags.Float32("memory", 0, "memory in GiB") // colima-compatible
20+
_ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
21+
return []string{"1", "2", "4", "8", "16", "32"}, cobra.ShellCompDirectiveNoFileComp
22+
})
23+
flags.String("mount-type", "", "mount type") // Similar to colima's --mount-type=(sshfs|9p|virtiofs), but "reverse-sshfs" is Lima is called "sshfs" in colima
24+
_ = cmd.RegisterFlagCompletionFunc("mount-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
25+
return []string{"reverse-sshfs", "9p", "virtiofs"}, cobra.ShellCompDirectiveNoFileComp
26+
})
27+
28+
flags.String("set", "", "modify the template inplace, using yq syntax")
29+
}
30+
31+
// RegisterStart registers flags related to in-place YAML modification.
32+
func RegisterStart(cmd *cobra.Command) {
33+
RegisterEdit(cmd)
34+
flags := cmd.Flags()
35+
flags.String("arch", "", "machine architecture") // colima-compatible
36+
_ = cmd.RegisterFlagCompletionFunc("arch", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
37+
return []string{"x86_64", "aarch64", "riscv64"}, cobra.ShellCompDirectiveNoFileComp
38+
})
39+
40+
flags.Float32("disk", 0, "disk size in GiB") // colima-compatible
41+
_ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
42+
return []string{"10", "30", "50", "100", "200"}, cobra.ShellCompDirectiveNoFileComp
43+
})
44+
45+
flags.String("vm-type", "", "virtual machine type") // colima-compatible
46+
_ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
47+
return []string{"qemu", "vz"}, cobra.ShellCompDirectiveNoFileComp
48+
})
49+
}
50+
51+
// YQExpressions returns YQ expressions.
52+
func YQExpressions(flags *flag.FlagSet) ([]string, error) {
53+
type def struct {
54+
flagName string
55+
fmt string
56+
experimental bool
57+
}
58+
defs := []def{
59+
{"cpus", ".cpus = %s", false},
60+
{"memory", ".memory = \"%sGiB\"", false},
61+
{"mount-type", ".mountType = %q", false},
62+
{"set", "%s", true},
63+
{"arch", ".arch = %q", false},
64+
{"disk", ".disk= \"%sGiB\"", true},
65+
{"vm-type", ".vmType = %q", false},
66+
}
67+
var exprs []string
68+
for _, def := range defs {
69+
v := flags.Lookup(def.flagName)
70+
if v != nil && v.Changed {
71+
if def.experimental {
72+
logrus.Warnf("`--%s` is experimental", def.flagName)
73+
}
74+
exprs = append(exprs, fmt.Sprintf(def.fmt, v.Value))
75+
}
76+
}
77+
return exprs, nil
78+
}

cmd/limactl/start.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/AlecAivazis/survey/v2"
1313
"github.com/containerd/containerd/identifiers"
14+
"github.com/lima-vm/lima/cmd/limactl/editflags"
1415
"github.com/lima-vm/lima/cmd/limactl/guessarg"
1516
"github.com/lima-vm/lima/pkg/editutil"
1617
"github.com/lima-vm/lima/pkg/ioutilx"
@@ -38,6 +39,9 @@ To create an instance "default" from a template "docker":
3839
$ limactl start --name=default template://docker
3940
4041
To create an instance "default" with modified parameters:
42+
$ limactl start --cpus=2 --memory=2
43+
44+
To create an instance "default" with yq expressions:
4145
$ limactl start --set='.cpus = 2 | .memory = "2GiB"'
4246
4347
To see the template list:
@@ -59,8 +63,8 @@ $ cat template.yaml | limactl start --name=local -
5963
}
6064
// TODO: "survey" does not support using cygwin terminal on windows yet
6165
startCommand.Flags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "enable TUI interactions such as opening an editor, defaults to true when stdout is a terminal")
66+
editflags.RegisterStart(startCommand)
6267
startCommand.Flags().String("name", "", "override the instance name")
63-
startCommand.Flags().String("set", "", "modify the template inplace, using yq syntax")
6468
startCommand.Flags().Bool("list-templates", false, "list available templates and exit")
6569
startCommand.Flags().Duration("timeout", start.DefaultWatchHostAgentEventsTimeout, "duration to wait for the instance to be running before timing out")
6670
return startCommand
@@ -77,20 +81,26 @@ func loadOrCreateInstance(cmd *cobra.Command, args []string) (*store.Instance, e
7781
err error
7882
)
7983

84+
flags := cmd.Flags()
85+
8086
// Create an instance, with menu TUI when TTY is available
81-
tty, err := cmd.Flags().GetBool("tty")
87+
tty, err := flags.GetBool("tty")
8288
if err != nil {
8389
return nil, err
8490
}
8591

86-
st.instName, err = cmd.Flags().GetString("name")
92+
st.instName, err = flags.GetString("name")
8793
if err != nil {
8894
return nil, err
8995
}
90-
st.yq, err = cmd.Flags().GetString("set")
96+
97+
yqExprs, err := editflags.YQExpressions(flags)
9198
if err != nil {
9299
return nil, err
93100
}
101+
if len(yqExprs) > 0 {
102+
st.yq = strings.Join(yqExprs, " | ")
103+
}
94104
const yBytesLimit = 4 * 1024 * 1024 // 4MiB
95105

96106
if ok, u := guessarg.SeemsTemplateURL(arg); ok {
@@ -280,7 +290,6 @@ func modifyInPlace(st *creatorState) error {
280290
if st.yq == "" {
281291
return nil
282292
}
283-
logrus.Warn("`--set` is experimental")
284293
out, err := yqutil.EvaluateExpression(st.yq, st.yBytes)
285294
if err != nil {
286295
return err

docs/experimental.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ The following features are experimental and subject to change:
1212
The following flags are experimental and subject to change:
1313

1414
- `start --set`, yq expression
15+
- `start --disk=<SIZE>`
1516
- `edit --set`, yq expression

0 commit comments

Comments
 (0)