diff --git a/README.md b/README.md index 8a6e8cc0c04..aefc50db2c6 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,9 @@ Use `:` to specify a source or target inside an instance. #### `limactl delete` `limactl delete [--force] `: delete the instance +#### `limactl factory-reset` +`limactl factory-reset `: factory reset the instance + #### `limactl edit` `limactl edit `: edit the instance diff --git a/cmd/limactl/factory-reset.go b/cmd/limactl/factory-reset.go new file mode 100644 index 00000000000..2295edacade --- /dev/null +++ b/cmd/limactl/factory-reset.go @@ -0,0 +1,61 @@ +package main + +import ( + "errors" + "os" + "path/filepath" + "strings" + + "github.com/lima-vm/lima/pkg/store" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func newFactoryResetCommand() *cobra.Command { + var resetCommand = &cobra.Command{ + Use: "factory-reset INSTANCE", + Short: "Factory reset an instance of Lima", + Args: cobra.MaximumNArgs(1), + RunE: factoryResetAction, + ValidArgsFunction: factoryResetBashComplete, + } + return resetCommand +} + +func factoryResetAction(cmd *cobra.Command, args []string) error { + instName := DefaultInstanceName + if len(args) > 0 { + instName = args[0] + } + + inst, err := store.Inspect(instName) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + logrus.Infof("Instance %q not found", instName) + return nil + } + return err + } + + stopInstanceForcibly(inst) + + fi, err := os.ReadDir(inst.Dir) + if err != nil { + return err + } + for _, f := range fi { + path := filepath.Join(inst.Dir, f.Name()) + if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") { + logrus.Infof("Removing %q", path) + if err := os.Remove(path); err != nil { + logrus.Error(err) + } + } + } + logrus.Infof("Instance %q has been factory reset", instName) + return nil +} + +func factoryResetBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return bashCompleteInstanceNames(cmd) +} diff --git a/cmd/limactl/main.go b/cmd/limactl/main.go index 2281bec45cf..61fded29348 100644 --- a/cmd/limactl/main.go +++ b/cmd/limactl/main.go @@ -83,6 +83,7 @@ func newApp() *cobra.Command { newShowSSHCommand(), newDebugCommand(), newEditCommand(), + newFactoryResetCommand(), ) return rootCmd }