Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/geth: make it possible to autopilot removedb #28721

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ import (

var (
removedbCommand = &cli.Command{
Action: removeDB,
Name: "removedb",
Usage: "Remove blockchain and state databases",
ArgsUsage: "",
Action: removeDB,
Name: "removedb",
Usage: "Remove blockchain and state databases. If arguments are provided, " +
"they are interpreted as comma-separated answers to questions.",
ArgsUsage: " <? comma-separated answers>",
Flags: utils.DatabaseFlags,
Description: `
Remove blockchain and state databases`,
Expand Down Expand Up @@ -202,20 +203,29 @@ func removeDB(ctx *cli.Context) error {
var (
rootDir = stack.ResolvePath("chaindata")
ancientDir = config.Eth.DatabaseFreezer
answers = strings.Split(ctx.Args().Get(0), ",")
)
switch {
case ancientDir == "":
ancientDir = filepath.Join(stack.ResolvePath("chaindata"), "ancient")
case !filepath.IsAbs(ancientDir):
ancientDir = config.Node.ResolvePath(ancientDir)
}
var oracle = func() string {
var ret string
if len(answers) > 0 {
ret = answers[0]
answers = answers[1:]
}
return ret
}
// Delete state data
statePaths := []string{rootDir, filepath.Join(ancientDir, rawdb.StateFreezerName)}
confirmAndRemoveDB(statePaths, "state data")
confirmAndRemoveDB(statePaths, "state data", oracle)

// Delete ancient chain
chainPaths := []string{filepath.Join(ancientDir, rawdb.ChainFreezerName)}
confirmAndRemoveDB(chainPaths, "ancient chain")
confirmAndRemoveDB(chainPaths, "ancient chain", oracle)
return nil
}

Expand All @@ -238,14 +248,23 @@ func removeFolder(dir string) {

// confirmAndRemoveDB prompts the user for a last confirmation and removes the
// list of folders if accepted.
func confirmAndRemoveDB(paths []string, kind string) {
func confirmAndRemoveDB(paths []string, kind string, oracle func() string) {
var (
confirm bool
err error
)
msg := fmt.Sprintf("Location(s) of '%s': \n", kind)
for _, path := range paths {
msg += fmt.Sprintf("\t- %s\n", path)
}
fmt.Println(msg)

confirm, err := prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove '%s'?", kind))
if answer := oracle(); answer != "" {
fmt.Printf("Remove '%s'? [y/n] %s\n", kind, answer)
confirm = (answer == "y")
} else {
confirm, err = prompt.Stdin.PromptConfirm(fmt.Sprintf("Remove '%s'?", kind))
}
switch {
case err != nil:
utils.Fatalf("%v", err)
Expand Down
Loading