|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/lima-vm/lima/v2/pkg/memory" |
| 14 | + "github.com/lima-vm/lima/v2/pkg/store" |
| 15 | +) |
| 16 | + |
| 17 | +func newMemoryCommand() *cobra.Command { |
| 18 | + memoryCmd := &cobra.Command{ |
| 19 | + Use: "memory", |
| 20 | + Short: "Manage instance memory", |
| 21 | + PersistentPreRun: func(*cobra.Command, []string) { |
| 22 | + logrus.Warn("`limactl memory` is experimental") |
| 23 | + }, |
| 24 | + GroupID: advancedCommand, |
| 25 | + } |
| 26 | + memoryCmd.AddCommand(newMemoryGetCommand()) |
| 27 | + memoryCmd.AddCommand(newMemorySetCommand()) |
| 28 | + |
| 29 | + return memoryCmd |
| 30 | +} |
| 31 | + |
| 32 | +func newMemoryGetCommand() *cobra.Command { |
| 33 | + getCmd := &cobra.Command{ |
| 34 | + Use: "get INSTANCE", |
| 35 | + Short: "Get current memory", |
| 36 | + Long: "Get the currently used total memory of an instance, in MiB", |
| 37 | + Args: cobra.MinimumNArgs(1), |
| 38 | + RunE: memoryGetAction, |
| 39 | + ValidArgsFunction: memoryBashComplete, |
| 40 | + } |
| 41 | + |
| 42 | + return getCmd |
| 43 | +} |
| 44 | + |
| 45 | +func memoryGetAction(cmd *cobra.Command, args []string) error { |
| 46 | + ctx := cmd.Context() |
| 47 | + instName := args[0] |
| 48 | + |
| 49 | + inst, err := store.Inspect(ctx, instName) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + _ = inst |
| 55 | + mem := 0 // TODO: implement |
| 56 | + fmt.Fprintf(cmd.OutOrStdout(), "%d\n", mem>>20) |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func newMemorySetCommand() *cobra.Command { |
| 61 | + setCmd := &cobra.Command{ |
| 62 | + Use: "set INSTANCE memory AMOUNT", |
| 63 | + Short: "Set target memory", |
| 64 | + Long: "Set the target total memory of an instance, in MiB", |
| 65 | + Args: cobra.MinimumNArgs(2), |
| 66 | + RunE: memorySetAction, |
| 67 | + ValidArgsFunction: memoryBashComplete, |
| 68 | + } |
| 69 | + |
| 70 | + return setCmd |
| 71 | +} |
| 72 | + |
| 73 | +func memorySetAction(cmd *cobra.Command, args []string) error { |
| 74 | + ctx := cmd.Context() |
| 75 | + instName := args[0] |
| 76 | + meg, err := strconv.Atoi(args[1]) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + inst, err := store.Inspect(ctx, instName) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + return memory.SetTarget(ctx, inst, int64(meg)<<20) |
| 87 | +} |
| 88 | + |
| 89 | +func memoryBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 90 | + return bashCompleteInstanceNames(cmd) |
| 91 | +} |
0 commit comments