From 19b994ca15f601891f5f57d6c1d103ffd03b3b9e Mon Sep 17 00:00:00 2001 From: sh-cha Date: Tue, 6 Aug 2024 14:14:15 +0900 Subject: [PATCH] add reset cmd to delete bot db --- cmd/opinitd/key.go | 2 +- cmd/opinitd/main.go | 18 ------------------ cmd/opinitd/reset.go | 37 +++++++++++++++++++++++++++++++++++++ cmd/opinitd/root.go | 1 + 4 files changed, 39 insertions(+), 19 deletions(-) delete mode 100644 cmd/opinitd/main.go create mode 100644 cmd/opinitd/reset.go diff --git a/cmd/opinitd/key.go b/cmd/opinitd/key.go index 9312e06..fc7a0a1 100644 --- a/cmd/opinitd/key.go +++ b/cmd/opinitd/key.go @@ -68,7 +68,7 @@ func keysAddCmd(ctx *cmdContext) *cobra.Command { Example: strings.TrimSpace(` $ keys add localnet key1 $ keys add l2 key2 --bech32 celestia -$ keys add l2 key2 --restore mnemonic.txt`), +$ keys add l2 key2 --recover --source mnemonic.txt`), RunE: func(cmd *cobra.Command, args []string) error { chainId := args[0] keyName := args[1] diff --git a/cmd/opinitd/main.go b/cmd/opinitd/main.go deleted file mode 100644 index 57f94f8..0000000 --- a/cmd/opinitd/main.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "fmt" - "os" -) - -// TODO: use cmd package to build and run the bot -// just test the bot with this main function - -func main() { - rootCmd := NewRootCmd() - - if err := rootCmd.Execute(); err != nil { - fmt.Fprintln(rootCmd.OutOrStderr(), err) - os.Exit(1) - } -} diff --git a/cmd/opinitd/reset.go b/cmd/opinitd/reset.go new file mode 100644 index 0000000..7eb87c8 --- /dev/null +++ b/cmd/opinitd/reset.go @@ -0,0 +1,37 @@ +package main + +import ( + "os" + "path" + + "github.com/spf13/cobra" + + bottypes "github.com/initia-labs/opinit-bots-go/bot/types" +) + +func resetStateCmd(ctx *cmdContext) *cobra.Command { + cmd := &cobra.Command{ + Use: "reset-db [bot-name]", + Args: cobra.ExactArgs(1), + Short: "Reset a bot's db.", + Long: `Reset a bot's db. +`, + RunE: func(cmd *cobra.Command, args []string) error { + botType := bottypes.BotTypeFromString(args[0]) + switch botType { + case bottypes.BotTypeExecutor: + dbPath := path.Join(ctx.homePath, string(botType)) + err := os.RemoveAll(dbPath + ".db") + if err != nil { + return err + } + err = os.Remove(path.Join(ctx.homePath, "batch")) + if err != nil { + return err + } + } + return nil + }, + } + return cmd +} diff --git a/cmd/opinitd/root.go b/cmd/opinitd/root.go index 327712e..64438b4 100644 --- a/cmd/opinitd/root.go +++ b/cmd/opinitd/root.go @@ -45,6 +45,7 @@ func NewRootCmd() *cobra.Command { initCmd(ctx), startCmd(ctx), keysCmd(ctx), + resetStateCmd(ctx), version.NewVersionCommand(), )