Skip to content

Commit e8075e2

Browse files
authored
Merge pull request #757 from Junnplus/reset-cmd
Add factory reset cmd
2 parents 3aca16d + a261edf commit e8075e2

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ Use `<INSTANCE>:<FILENAME>` to specify a source or target inside an instance.
214214
#### `limactl delete`
215215
`limactl delete [--force] <INSTANCE>`: delete the instance
216216

217+
#### `limactl factory-reset`
218+
`limactl factory-reset <INSTANCE>`: factory reset the instance
219+
217220
#### `limactl edit`
218221
`limactl edit <INSTANCE>`: edit the instance
219222

cmd/limactl/factory-reset.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/lima-vm/lima/pkg/store"
10+
"github.com/sirupsen/logrus"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func newFactoryResetCommand() *cobra.Command {
15+
var resetCommand = &cobra.Command{
16+
Use: "factory-reset INSTANCE",
17+
Short: "Factory reset an instance of Lima",
18+
Args: cobra.MaximumNArgs(1),
19+
RunE: factoryResetAction,
20+
ValidArgsFunction: factoryResetBashComplete,
21+
}
22+
return resetCommand
23+
}
24+
25+
func factoryResetAction(cmd *cobra.Command, args []string) error {
26+
instName := DefaultInstanceName
27+
if len(args) > 0 {
28+
instName = args[0]
29+
}
30+
31+
inst, err := store.Inspect(instName)
32+
if err != nil {
33+
if errors.Is(err, os.ErrNotExist) {
34+
logrus.Infof("Instance %q not found", instName)
35+
return nil
36+
}
37+
return err
38+
}
39+
40+
stopInstanceForcibly(inst)
41+
42+
fi, err := os.ReadDir(inst.Dir)
43+
if err != nil {
44+
return err
45+
}
46+
for _, f := range fi {
47+
path := filepath.Join(inst.Dir, f.Name())
48+
if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") {
49+
logrus.Infof("Removing %q", path)
50+
if err := os.Remove(path); err != nil {
51+
logrus.Error(err)
52+
}
53+
}
54+
}
55+
logrus.Infof("Instance %q has been factory reset", instName)
56+
return nil
57+
}
58+
59+
func factoryResetBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
60+
return bashCompleteInstanceNames(cmd)
61+
}

cmd/limactl/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func newApp() *cobra.Command {
8383
newShowSSHCommand(),
8484
newDebugCommand(),
8585
newEditCommand(),
86+
newFactoryResetCommand(),
8687
)
8788
return rootCmd
8889
}

0 commit comments

Comments
 (0)