lbcwallet/cmd/dropwtxmgr/main.go

118 lines
2.3 KiB
Go
Raw Normal View History

// Copyright (c) 2015-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
2020-08-11 21:53:44 +02:00
"time"
2018-05-24 04:02:33 +02:00
"github.com/jessevdk/go-flags"
btcutil "github.com/lbryio/lbcutil"
"github.com/lbryio/lbcwallet/wallet"
"github.com/lbryio/lbcwallet/walletdb"
_ "github.com/lbryio/lbcwallet/walletdb/bdb"
)
const defaultNet = "mainnet"
2020-08-11 21:53:44 +02:00
var (
datadir = btcutil.AppDataDir("lbcwallet", false)
2020-08-11 21:53:44 +02:00
)
// Flags.
var opts = struct {
2020-08-11 21:53:44 +02:00
Force bool `short:"f" description:"Force removal without prompt"`
DbPath string `long:"db" description:"Path to wallet database"`
DropLabels bool `long:"droplabels" description:"Drop transaction labels"`
Timeout time.Duration `long:"timeout" description:"Timeout value when opening the wallet database"`
}{
2020-08-11 21:53:44 +02:00
Force: false,
DbPath: filepath.Join(datadir, defaultNet, wallet.WalletDBName),
Timeout: wallet.DefaultDBTimeout,
}
func init() {
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
}
func yes(s string) bool {
switch s {
case "y", "Y", "yes", "Yes":
return true
default:
return false
}
}
func no(s string) bool {
switch s {
case "n", "N", "no", "No":
return true
default:
return false
}
}
func main() {
os.Exit(mainInt())
}
func mainInt() int {
fmt.Println("Database path:", opts.DbPath)
_, err := os.Stat(opts.DbPath)
if os.IsNotExist(err) {
fmt.Println("Database file does not exist")
return 1
}
for !opts.Force {
fmt.Print("Drop all lbcwallet transaction history? [y/N] ")
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
if !scanner.Scan() {
// Exit on EOF.
return 0
}
err := scanner.Err()
if err != nil {
fmt.Println()
fmt.Println(err)
return 1
}
resp := scanner.Text()
if yes(resp) {
break
}
if no(resp) || resp == "" {
return 0
}
fmt.Println("Enter yes or no.")
}
2020-08-11 21:53:44 +02:00
db, err := walletdb.Open("bdb", opts.DbPath, true, opts.Timeout)
if err != nil {
fmt.Println("Failed to open database:", err)
return 1
}
defer db.Close()
fmt.Println("Dropping lbcwallet transaction history")
err = wallet.DropTransactionHistory(db, !opts.DropLabels)
if err != nil {
fmt.Println("Failed to drop and re-create namespace:", err)
return 1
}
return 0
}