cmd/geth: make it possible to autopilot removedb

This commit is contained in:
Martin Holst Swende 2023-12-21 20:58:28 +01:00
parent cca94792a4
commit bb5f202bb2
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -43,12 +43,23 @@ import (
) )
var ( var (
removeStateDataFlag = &cli.BoolFlag{
Name: "remove.state",
Usage: "If set, selects the state data for removal",
}
removeChainDataFlag = &cli.BoolFlag{
Name: "remove.chain",
Usage: "If set, selects the state data for removal",
}
removedbCommand = &cli.Command{ removedbCommand = &cli.Command{
Action: removeDB, Action: removeDB,
Name: "removedb", Name: "removedb",
Usage: "Remove blockchain and state databases", Usage: "Remove blockchain and state databases. If arguments are provided, " +
"they are interpreted as comma-separated answers to questions.",
ArgsUsage: "", ArgsUsage: "",
Flags: utils.DatabaseFlags, Flags: flags.Merge(utils.DatabaseFlags,
[]cli.Flag{removeStateDataFlag, removeChainDataFlag}),
Description: ` Description: `
Remove blockchain and state databases`, Remove blockchain and state databases`,
} }
@ -211,11 +222,11 @@ func removeDB(ctx *cli.Context) error {
} }
// Delete state data // Delete state data
statePaths := []string{rootDir, filepath.Join(ancientDir, rawdb.StateFreezerName)} statePaths := []string{rootDir, filepath.Join(ancientDir, rawdb.StateFreezerName)}
confirmAndRemoveDB(statePaths, "state data") confirmAndRemoveDB(statePaths, "state data", ctx, removeStateDataFlag.Name)
// Delete ancient chain // Delete ancient chain
chainPaths := []string{filepath.Join(ancientDir, rawdb.ChainFreezerName)} chainPaths := []string{filepath.Join(ancientDir, rawdb.ChainFreezerName)}
confirmAndRemoveDB(chainPaths, "ancient chain") confirmAndRemoveDB(chainPaths, "ancient chain", ctx, removeChainDataFlag.Name)
return nil return nil
} }
@ -238,14 +249,26 @@ func removeFolder(dir string) {
// confirmAndRemoveDB prompts the user for a last confirmation and removes the // confirmAndRemoveDB prompts the user for a last confirmation and removes the
// list of folders if accepted. // list of folders if accepted.
func confirmAndRemoveDB(paths []string, kind string) { func confirmAndRemoveDB(paths []string, kind string, ctx *cli.Context, removeFlagName string) {
var (
confirm bool
err error
)
msg := fmt.Sprintf("Location(s) of '%s': \n", kind) msg := fmt.Sprintf("Location(s) of '%s': \n", kind)
for _, path := range paths { for _, path := range paths {
msg += fmt.Sprintf("\t- %s\n", path) msg += fmt.Sprintf("\t- %s\n", path)
} }
fmt.Println(msg) fmt.Println(msg)
if ctx.IsSet(removeFlagName) {
confirm, err := prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove '%s'?", kind)) confirm = ctx.Bool(removeFlagName)
if confirm {
fmt.Printf("Remove '%s'? [y/n] y\n", kind)
} else {
fmt.Printf("Remove '%s'? [y/n] n\n", kind)
}
} else {
confirm, err = prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove '%s'?", kind))
}
switch { switch {
case err != nil: case err != nil:
utils.Fatalf("%v", err) utils.Fatalf("%v", err)