Skip to content

Commit

Permalink
Merge pull request #757 from Junnplus/reset-cmd
Browse files Browse the repository at this point in the history
Add factory reset cmd
  • Loading branch information
jandubois authored Mar 26, 2022
2 parents 3aca16d + a261edf commit e8075e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ Use `<INSTANCE>:<FILENAME>` to specify a source or target inside an instance.
#### `limactl delete`
`limactl delete [--force] <INSTANCE>`: delete the instance

#### `limactl factory-reset`
`limactl factory-reset <INSTANCE>`: factory reset the instance

#### `limactl edit`
`limactl edit <INSTANCE>`: edit the instance

Expand Down
61 changes: 61 additions & 0 deletions cmd/limactl/factory-reset.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func newApp() *cobra.Command {
newShowSSHCommand(),
newDebugCommand(),
newEditCommand(),
newFactoryResetCommand(),
)
return rootCmd
}
Expand Down

0 comments on commit e8075e2

Please sign in to comment.