|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "github.com/lima-vm/lima/cmd/limactl/editflags" |
| 15 | + "github.com/lima-vm/lima/pkg/instance" |
| 16 | + "github.com/lima-vm/lima/pkg/limayaml" |
| 17 | + networks "github.com/lima-vm/lima/pkg/networks/reconcile" |
| 18 | + "github.com/lima-vm/lima/pkg/store" |
| 19 | + "github.com/lima-vm/lima/pkg/store/filenames" |
| 20 | + "github.com/lima-vm/lima/pkg/yqutil" |
| 21 | +) |
| 22 | + |
| 23 | +func newCloneCommand() *cobra.Command { |
| 24 | + cloneCommand := &cobra.Command{ |
| 25 | + Use: "clone OLDINST NEWINST", |
| 26 | + Short: "Clone an instance of Lima", |
| 27 | + Long: `Clone an instance of Lima. |
| 28 | +
|
| 29 | +Not to be confused with 'limactl copy' ('limactl cp'). |
| 30 | +`, |
| 31 | + Args: WrapArgsError(cobra.ExactArgs(2)), |
| 32 | + RunE: cloneAction, |
| 33 | + ValidArgsFunction: cloneBashComplete, |
| 34 | + GroupID: advancedCommand, |
| 35 | + } |
| 36 | + editflags.RegisterEdit(cloneCommand) |
| 37 | + return cloneCommand |
| 38 | +} |
| 39 | + |
| 40 | +func cloneAction(cmd *cobra.Command, args []string) error { |
| 41 | + ctx := cmd.Context() |
| 42 | + flags := cmd.Flags() |
| 43 | + tty, err := flags.GetBool("tty") |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + oldInstName, newInstName := args[0], args[1] |
| 49 | + oldInst, err := store.Inspect(oldInstName) |
| 50 | + if err != nil { |
| 51 | + if errors.Is(err, os.ErrNotExist) { |
| 52 | + return fmt.Errorf("instance %q not found", oldInstName) |
| 53 | + } |
| 54 | + return err |
| 55 | + } |
| 56 | + if oldInst.Status == store.StatusRunning { |
| 57 | + return errors.New("cannot clone a running instance") |
| 58 | + } |
| 59 | + |
| 60 | + if err = instance.Clone(ctx, oldInstName, newInstName); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + newInst, err := store.Inspect(newInstName) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + yqExprs, err := editflags.YQExpressions(flags, false) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + if len(yqExprs) > 0 { |
| 74 | + // TODO: reduce duplicated codes across cloneAction and editAction |
| 75 | + yq := yqutil.Join(yqExprs) |
| 76 | + filePath := filepath.Join(newInst.Dir, filenames.LimaYAML) |
| 77 | + yContent, err := os.ReadFile(filePath) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + yBytes, err := yqutil.EvaluateExpression(yq, yContent) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + y, err := limayaml.LoadWithWarnings(yBytes, filePath) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if err := limayaml.Validate(y, true); err != nil { |
| 90 | + return saveRejectedYAML(yBytes, err) |
| 91 | + } |
| 92 | + if err := limayaml.ValidateAgainstLatestConfig(yBytes, yContent); err != nil { |
| 93 | + return saveRejectedYAML(yBytes, err) |
| 94 | + } |
| 95 | + if err := os.WriteFile(filePath, yBytes, 0o644); err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + newInst, err = store.Inspect(newInst.Name) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if !tty { |
| 105 | + // use "start" to start it |
| 106 | + return nil |
| 107 | + } |
| 108 | + startNow, err := askWhetherToStart() |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + if !startNow { |
| 113 | + return nil |
| 114 | + } |
| 115 | + err = networks.Reconcile(ctx, newInst.Name) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + return instance.Start(ctx, newInst, "", false) |
| 120 | +} |
| 121 | + |
| 122 | +func cloneBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 123 | + return bashCompleteInstanceNames(cmd) |
| 124 | +} |
0 commit comments