fix: daemon: avoid prompting to remove chain when noninteractive (#11582)

1. Let the user specify `--remove-existing-chain=false` to avoid the
   prompt and fail if there's an existing chain.
2. Only prompt when interactive. Otherwise, _don't_ remove the chain
   by-default.
This commit is contained in:
Steven Allen 2024-01-18 13:23:49 -08:00 committed by GitHub
parent 2cd6aea94b
commit 3685cb5e12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -178,6 +178,8 @@ var DaemonCmd = &cli.Command{
return xerrors.Errorf("enabling runtime metrics: %w", err)
}
interactive := cctx.Bool("interactive")
if cctx.Bool("manage-fdlimit") {
if _, _, err := ulimit.ManageFdLimit(); err != nil {
log.Errorf("setting file descriptor limit: %s", err)
@ -299,8 +301,8 @@ var DaemonCmd = &cli.Command{
willImportChain = true
}
willRemoveChain := cctx.Bool("remove-existing-chain")
if willImportChain && !willRemoveChain {
var willRemoveChain bool
if interactive && willImportChain && !cctx.IsSet("remove-existing-chain") {
// Confirm with the user about the intention to remove chain data.
reader := bufio.NewReader(os.Stdin)
fmt.Print("Importing chain or snapshot will by default delete existing local chain data. Do you want to proceed and delete? (yes/no): ")
@ -309,14 +311,16 @@ var DaemonCmd = &cli.Command{
return xerrors.Errorf("reading user input: %w", err)
}
userInput = strings.ToLower(strings.TrimSpace(userInput))
if userInput == "yes" {
switch userInput {
case "yes":
willRemoveChain = true
} else if userInput == "no" {
case "no":
willRemoveChain = false
} else {
default:
return fmt.Errorf("invalid input. please answer with 'yes' or 'no'")
}
} else {
willRemoveChain = cctx.Bool("remove-existing-chain")
}
if willRemoveChain {