From 76748dd42c6c0e7930d06892068ad58e322429d1 Mon Sep 17 00:00:00 2001 From: Ye Sijun Date: Fri, 25 Mar 2022 16:49:26 +0800 Subject: [PATCH 1/2] add factory reset cmd Signed-off-by: Ye Sijun --- README.md | 3 ++ cmd/limactl/factory-reset.go | 76 ++++++++++++++++++++++++++++++++++++ cmd/limactl/main.go | 1 + 3 files changed, 80 insertions(+) create mode 100644 cmd/limactl/factory-reset.go 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..6663d663dd3 --- /dev/null +++ b/cmd/limactl/factory-reset.go @@ -0,0 +1,76 @@ +package main + +import ( + "errors" + "os" + "path/filepath" + "strings" + + networks "github.com/lima-vm/lima/pkg/networks/reconcile" + "github.com/lima-vm/lima/pkg/start" + "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) + + startNow, err := askWhetherToStart() + if err != nil { + return err + } + if !startNow { + return nil + } + ctx := cmd.Context() + err = networks.Reconcile(ctx, inst.Name) + if err != nil { + return err + } + return start.Start(ctx, inst) +} + +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 } From a261edf196f19a173395cb3fd995ad97778fc6fb Mon Sep 17 00:00:00 2001 From: Ye Sijun Date: Sat, 26 Mar 2022 13:11:09 +0800 Subject: [PATCH 2/2] remove the start ask after factory reset Signed-off-by: Ye Sijun --- cmd/limactl/factory-reset.go | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/cmd/limactl/factory-reset.go b/cmd/limactl/factory-reset.go index 6663d663dd3..2295edacade 100644 --- a/cmd/limactl/factory-reset.go +++ b/cmd/limactl/factory-reset.go @@ -6,8 +6,6 @@ import ( "path/filepath" "strings" - networks "github.com/lima-vm/lima/pkg/networks/reconcile" - "github.com/lima-vm/lima/pkg/start" "github.com/lima-vm/lima/pkg/store" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -55,20 +53,7 @@ func factoryResetAction(cmd *cobra.Command, args []string) error { } } logrus.Infof("Instance %q has been factory reset", instName) - - startNow, err := askWhetherToStart() - if err != nil { - return err - } - if !startNow { - return nil - } - ctx := cmd.Context() - err = networks.Reconcile(ctx, inst.Name) - if err != nil { - return err - } - return start.Start(ctx, inst) + return nil } func factoryResetBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {