-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #757 from Junnplus/reset-cmd
Add factory reset cmd
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters