From b7bc8342a0ca8d79df3e1a1f2c4912c9a8af797e Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 22 Aug 2023 02:55:19 +0800 Subject: [PATCH] fix: linter issues --- cmd/cmd.go | 22 +++++++++--------- cmd/daemon/init.go | 32 +++++++++++++------------- cmd/daemon/start.go | 4 ++-- cmd/wallet/address.go | 18 +++++++-------- cmd/wallet/create.go | 8 +++---- cmd/wallet/history.go | 6 ++--- cmd/wallet/recover.go | 4 ++-- cmd/wallet/tx.go | 44 ++++++++++++++++++------------------ util/bech32m/bech32m_test.go | 3 ++- 9 files changed, 71 insertions(+), 70 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 16cd9bf6d..06e2bf369 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -77,9 +77,9 @@ func PromptConfirm(label string) bool { result, err := prompt.Run() if err != nil { if !errors.Is(promptui.ErrAbort, err) { - PrintErrorMsg("prompt error: %v", err) + PrintErrorMsgf("prompt error: %v", err) } else { - PrintWarnMsg("Aborted.") + PrintWarnMsgf("Aborted.") } os.Exit(1) } @@ -155,7 +155,7 @@ func FatalErrorCheck(err error) { } } -func PrintErrorMsg(format string, a ...interface{}) { +func PrintErrorMsgf(format string, a ...interface{}) { if terminalSupported() { // Print error msg with red color format = fmt.Sprintf("\033[31m[ERROR] %s\033[0m", format) @@ -163,7 +163,7 @@ func PrintErrorMsg(format string, a ...interface{}) { fmt.Printf(format+"\n", a...) } -func PrintSuccessMsg(format string, a ...interface{}) { +func PrintSuccessMsgf(format string, a ...interface{}) { if terminalSupported() { // Print successful msg with green color format = fmt.Sprintf("\033[32m%s\033[0m", format) @@ -171,7 +171,7 @@ func PrintSuccessMsg(format string, a ...interface{}) { fmt.Printf(format+"\n", a...) } -func PrintWarnMsg(format string, a ...interface{}) { +func PrintWarnMsgf(format string, a ...interface{}) { if terminalSupported() { // Print warning msg with yellow color format = fmt.Sprintf("\033[33m%s\033[0m", format) @@ -179,11 +179,11 @@ func PrintWarnMsg(format string, a ...interface{}) { fmt.Printf(format+"\n", a...) } -func PrintInfoMsg(format string, a ...interface{}) { +func PrintInfoMsgf(format string, a ...interface{}) { fmt.Printf(format+"\n", a...) } -func PrintInfoMsgBold(format string, a ...interface{}) { +func PrintInfoMsgBoldf(format string, a ...interface{}) { if terminalSupported() { format = fmt.Sprintf("\033[1m%s\033[0m", format) } @@ -199,7 +199,7 @@ func PrintJSONData(data []byte) { err := json.Indent(&out, data, "", " ") FatalErrorCheck(err) - PrintInfoMsg(out.String()) + PrintInfoMsgf(out.String()) } func PrintJSONObject(obj interface{}) { @@ -342,8 +342,8 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, confPath := PactusConfigPath(workingDir) conf, err := config.LoadFromFile(confPath, true) if err != nil { - PrintWarnMsg("Unable to load the config: %s", err) - PrintInfoMsg("Attempting to restore the config to the default values...") + PrintWarnMsgf("Unable to load the config: %s", err) + PrintInfoMsgf("Attempting to restore the config to the default values...") // First, try to open the old config file in non-strict mode confBack, err := config.LoadFromFile(confPath, false) @@ -369,7 +369,7 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, panic("not yet implemented!") } - PrintSuccessMsg("Config restored to the default values") + PrintSuccessMsgf("Config restored to the default values") conf, _ = config.LoadFromFile(confPath, true) // This time it should be OK } diff --git a/cmd/daemon/init.go b/cmd/daemon/init.go index 11bc183f9..ad0e1683e 100644 --- a/cmd/daemon/init.go +++ b/cmd/daemon/init.go @@ -31,17 +31,17 @@ func buildInitCmd(parentCmd *cobra.Command) { initCmd.Run = func(_ *cobra.Command, _ []string) { workingDir, _ := filepath.Abs(*workingDirOpt) if !util.IsDirNotExistsOrEmpty(workingDir) { - cmd.PrintErrorMsg("The working directory is not empty: %s", workingDir) + cmd.PrintErrorMsgf("The working directory is not empty: %s", workingDir) return } mnemonic := "" if len(*restoreOpt) == 0 { mnemonic = wallet.GenerateMnemonic(128) cmd.PrintLine() - cmd.PrintInfoMsg("Your wallet seed is:") - cmd.PrintInfoMsgBold(" " + mnemonic) + cmd.PrintInfoMsgf("Your wallet seed is:") + cmd.PrintInfoMsgBoldf(" " + mnemonic) cmd.PrintLine() - cmd.PrintWarnMsg("Write down this seed on a piece of paper to recover your validator key in future.") + cmd.PrintWarnMsgf("Write down this seed on a piece of paper to recover your validator key in future.") cmd.PrintLine() confirmed := cmd.PromptConfirm("Do you want to continue") if !confirmed { @@ -53,13 +53,13 @@ func buildInitCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) } cmd.PrintLine() - cmd.PrintInfoMsg("Enter a password for wallet") + cmd.PrintInfoMsgf("Enter a password for wallet") password := cmd.PromptPassword("Password", true) cmd.PrintLine() - cmd.PrintInfoMsgBold("How many validators do you want to create?") - cmd.PrintInfoMsg("Each node can run up to 32 validators, and each validator can hold up to 1000 staked coins.") - cmd.PrintInfoMsg("You can define validators based on the amount of coins you want to stake.") + cmd.PrintInfoMsgBoldf("How many validators do you want to create?") + cmd.PrintInfoMsgf("Each node can run up to 32 validators, and each validator can hold up to 1000 staked coins.") + cmd.PrintInfoMsgf("You can define validators based on the amount of coins you want to stake.") numValidators := cmd.PromptInputWithRange("Number of Validators", 7, 1, 32) chain := genesis.Mainnet @@ -74,23 +74,23 @@ func buildInitCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsgBold("Validator addresses:") + cmd.PrintInfoMsgBoldf("Validator addresses:") for i, addr := range validatorAddrs { - cmd.PrintInfoMsg("%v- %s", i+1, addr) + cmd.PrintInfoMsgf("%v- %s", i+1, addr) } cmd.PrintLine() - cmd.PrintInfoMsgBold("Reward addresses:") + cmd.PrintInfoMsgBoldf("Reward addresses:") for i, addr := range rewardAddrs { - cmd.PrintInfoMsg("%v- %s", i+1, addr) + cmd.PrintInfoMsgf("%v- %s", i+1, addr) } cmd.PrintLine() - cmd.PrintInfoMsgBold("Network: %v", chain.String()) + cmd.PrintInfoMsgBoldf("Network: %v", chain.String()) cmd.PrintLine() - cmd.PrintSuccessMsg("A pactus node is successfully initialized at %v", workingDir) + cmd.PrintSuccessMsgf("A pactus node is successfully initialized at %v", workingDir) cmd.PrintLine() - cmd.PrintInfoMsg("You can start the node by running this command:") - cmd.PrintInfoMsg("./pactus-daemon start -w %v", workingDir) + cmd.PrintInfoMsgf("You can start the node by running this command:") + cmd.PrintInfoMsgf("./pactus-daemon start -w %v", workingDir) } } diff --git a/cmd/daemon/start.go b/cmd/daemon/start.go index d0ae1431a..d1d985989 100644 --- a/cmd/daemon/start.go +++ b/cmd/daemon/start.go @@ -35,7 +35,7 @@ func buildStartCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) if *pprofOpt != "" { - cmd.PrintWarnMsg("Starting Debug pprof server on: http://%s/debug/pprof/", *pprofOpt) + cmd.PrintWarnMsgf("Starting Debug pprof server on: http://%s/debug/pprof/", *pprofOpt) server := &http.Server{ Addr: *pprofOpt, ReadHeaderTimeout: 3 * time.Second, @@ -65,7 +65,7 @@ func buildStartCmd(parentCmd *cobra.Command) { cmd.TrapSignal(func() { node.Stop() - cmd.PrintInfoMsg("Exiting ...") + cmd.PrintInfoMsgf("Exiting ...") }) // run forever (the node will not be returned) diff --git a/cmd/wallet/address.go b/cmd/wallet/address.go index 2d64d12d9..0b70183c6 100644 --- a/cmd/wallet/address.go +++ b/cmd/wallet/address.go @@ -62,7 +62,7 @@ func buildAllAddressesCmd(parentCmd *cobra.Command) { line += " (Imported)" } - cmd.PrintInfoMsg(line) + cmd.PrintInfoMsgf(line) } } } @@ -87,7 +87,7 @@ func buildNewAddressCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("%s", addr) + cmd.PrintInfoMsgf("%s", addr) } } @@ -112,7 +112,7 @@ func buildBalanceCmd(parentCmd *cobra.Command) { stake, err := wallet.Stake(*addrArg) cmd.FatalErrorCheck(err) - cmd.PrintInfoMsg("balance: %v\tstake: %v", + cmd.PrintInfoMsgf("balance: %v\tstake: %v", util.ChangeToCoin(balance), util.ChangeToCoin(stake)) } } @@ -137,7 +137,7 @@ func buildPrivateKeyCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintWarnMsg("Private Key: %v", prv) + cmd.PrintWarnMsgf("Private Key: %v", prv) } } @@ -157,14 +157,14 @@ func buildPublicKeyCmd(parentCmd *cobra.Command) { info := wallet.AddressInfo(*addrArg) if info == nil { - cmd.PrintErrorMsg("Address not found") + cmd.PrintErrorMsgf("Address not found") return } cmd.PrintLine() - cmd.PrintInfoMsg("Public Key: %v", info.Pub.String()) + cmd.PrintInfoMsgf("Public Key: %v", info.Pub.String()) if !info.Imported { - cmd.PrintInfoMsg("Path: %v", info.Path.String()) + cmd.PrintInfoMsgf("Path: %v", info.Path.String()) } } } @@ -196,7 +196,7 @@ func buildImportPrivateKeyCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintSuccessMsg("Private Key imported. Address: %v", + cmd.PrintSuccessMsgf("Private Key imported. Address: %v", prv.PublicKey().Address()) } } @@ -225,7 +225,7 @@ func buildSetLabelCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintSuccessMsg("Label set successfully") + cmd.PrintSuccessMsgf("Label set successfully") } } diff --git a/cmd/wallet/create.go b/cmd/wallet/create.go index 891d11445..a88d3be1f 100644 --- a/cmd/wallet/create.go +++ b/cmd/wallet/create.go @@ -33,9 +33,9 @@ func buildGenerateCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintSuccessMsg("Wallet created successfully at: %s", wallet.Path()) - cmd.PrintInfoMsg("Seed: \"%v\"", mnemonic) - cmd.PrintWarnMsg("Please keep your seed in a safe place; " + + cmd.PrintSuccessMsgf("Wallet created successfully at: %s", wallet.Path()) + cmd.PrintInfoMsgf("Seed: \"%v\"", mnemonic) + cmd.PrintWarnMsgf("Please keep your seed in a safe place; " + "if you lose it, you will not be able to restore your wallet.") } } @@ -63,6 +63,6 @@ func buildChangePasswordCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintWarnMsg("Wallet password updated") + cmd.PrintWarnMsgf("Wallet password updated") } } diff --git a/cmd/wallet/history.go b/cmd/wallet/history.go index 5b23f9cb1..3dbd5acb1 100644 --- a/cmd/wallet/history.go +++ b/cmd/wallet/history.go @@ -41,7 +41,7 @@ func buildAddToHistoryCmd(parentCmd *cobra.Command) { err = wallet.Save() cmd.FatalErrorCheck(err) - cmd.PrintInfoMsg("Transaction added to wallet") + cmd.PrintInfoMsgf("Transaction added to wallet") } } @@ -60,10 +60,10 @@ func buildShowHistoryCmd(parentCmd *cobra.Command) { history := wallet.GetHistory(*addrArg) for i, h := range history { if h.Time != nil { - cmd.PrintInfoMsg("%d %v %v %v %s\t%v", + cmd.PrintInfoMsgf("%d %v %v %v %s\t%v", i+1, h.Time.Format(time.RFC822), h.TxID, h.PayloadType, h.Desc, h.Amount) } else { - cmd.PrintInfoMsg("%d %v %s\t%v", + cmd.PrintInfoMsgf("%d %v %s\t%v", i+1, h.TxID, h.Desc, h.Amount) } } diff --git a/cmd/wallet/recover.go b/cmd/wallet/recover.go index 76bcfe99a..5a8c56963 100644 --- a/cmd/wallet/recover.go +++ b/cmd/wallet/recover.go @@ -32,7 +32,7 @@ func buildRecoverCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("Wallet recovered successfully at: %s", wallet.Path()) + cmd.PrintInfoMsgf("Wallet recovered successfully at: %s", wallet.Path()) } } @@ -55,6 +55,6 @@ func buildGetSeedCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("Seed: \"%v\"", mnemonic) + cmd.PrintInfoMsgf("Seed: \"%v\"", mnemonic) } } diff --git a/cmd/wallet/tx.go b/cmd/wallet/tx.go index e071f7bf7..cf0c53a76 100644 --- a/cmd/wallet/tx.go +++ b/cmd/wallet/tx.go @@ -51,11 +51,11 @@ func buildTransferTxCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("You are going to sign this \033[1mSend\033[0m transition:") - cmd.PrintInfoMsg("From : %s", *fromArg) - cmd.PrintInfoMsg("To : %s", *toArg) - cmd.PrintInfoMsg("Amount: %.9f", *amtArg) - cmd.PrintInfoMsg("Fee : %.9f", util.ChangeToCoin(trx.Fee())) + cmd.PrintInfoMsgf("You are going to sign this \033[1mSend\033[0m transition:") + cmd.PrintInfoMsgf("From : %s", *fromArg) + cmd.PrintInfoMsgf("To : %s", *toArg) + cmd.PrintInfoMsgf("Amount: %.9f", *amtArg) + cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee())) signAndPublishTx(w, trx, *noConfirmOpt, *passOpt) } @@ -92,11 +92,11 @@ func buildBondTxCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("You are going to sign this \033[1mBond\033[0m transition:") - cmd.PrintInfoMsg("Account : %s", *fromArg) - cmd.PrintInfoMsg("Validator: %s", *toArg) - cmd.PrintInfoMsg("Amount : %.9f", *amtArg) - cmd.PrintInfoMsg("Fee : %.9f", util.ChangeToCoin(trx.Fee())) + cmd.PrintInfoMsgf("You are going to sign this \033[1mBond\033[0m transition:") + cmd.PrintInfoMsgf("Account : %s", *fromArg) + cmd.PrintInfoMsgf("Validator: %s", *toArg) + cmd.PrintInfoMsgf("Amount : %.9f", *amtArg) + cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee())) signAndPublishTx(w, trx, *noConfirmOpt, *passOpt) } @@ -130,9 +130,9 @@ func buildUnbondTxCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("You are going to sign this \033[1mUnbond\033[0m transition:") - cmd.PrintInfoMsg("Validator: %s", *fromArg) - cmd.PrintInfoMsg("Fee : %.9f", util.ChangeToCoin(trx.Fee())) + cmd.PrintInfoMsgf("You are going to sign this \033[1mUnbond\033[0m transition:") + cmd.PrintInfoMsgf("Validator: %s", *fromArg) + cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee())) signAndPublishTx(w, trx, *noConfirmOpt, *passOpt) } @@ -173,11 +173,11 @@ func buildWithdrawTxCmd(parentCmd *cobra.Command) { cmd.FatalErrorCheck(err) cmd.PrintLine() - cmd.PrintInfoMsg("You are going to sign this \033[1mWithdraw\033[0m transition:") - cmd.PrintInfoMsg("Validator: %s", *fromArg) - cmd.PrintInfoMsg("Account : %s", *toArg) - cmd.PrintInfoMsg("Amount : %.9f", *amtArg) - cmd.PrintInfoMsg("Fee : %.9f", util.ChangeToCoin(trx.Fee())) + cmd.PrintInfoMsgf("You are going to sign this \033[1mWithdraw\033[0m transition:") + cmd.PrintInfoMsgf("Validator: %s", *fromArg) + cmd.PrintInfoMsgf("Account : %s", *toArg) + cmd.PrintInfoMsgf("Amount : %.9f", *amtArg) + cmd.PrintInfoMsgf("Fee : %.9f", util.ChangeToCoin(trx.Fee())) signAndPublishTx(w, trx, *noConfirmOpt, *passOpt) } @@ -209,13 +209,13 @@ func signAndPublishTx(w *wallet.Wallet, trx *tx.Tx, noConfirm bool, pass string) cmd.FatalErrorCheck(err) bs, _ := trx.Bytes() - cmd.PrintInfoMsg("Signed transaction data: %x", bs) + cmd.PrintInfoMsgf("Signed transaction data: %x", bs) cmd.PrintLine() if !w.IsOffline() { if !noConfirm { - cmd.PrintInfoMsg("You are going to broadcast the signed transition:") - cmd.PrintWarnMsg("THIS ACTION IS NOT REVERSIBLE") + cmd.PrintInfoMsgf("You are going to broadcast the signed transition:") + cmd.PrintWarnMsgf("THIS ACTION IS NOT REVERSIBLE") confirmed := cmd.PromptConfirm("Do you want to continue") if !confirmed { return @@ -227,7 +227,7 @@ func signAndPublishTx(w *wallet.Wallet, trx *tx.Tx, noConfirm bool, pass string) err = w.Save() cmd.FatalErrorCheck(err) - cmd.PrintInfoMsg("Transaction hash: %s", res) + cmd.PrintInfoMsgf("Transaction hash: %s", res) } } diff --git a/util/bech32m/bech32m_test.go b/util/bech32m/bech32m_test.go index e0521a135..edff90941 100644 --- a/util/bech32m/bech32m_test.go +++ b/util/bech32m/bech32m_test.go @@ -35,7 +35,8 @@ func TestBech32M(t *testing.T) { {"\x201xj0phk", InvalidCharacterError('\x20')}, {"\x7f1g6xzxy", InvalidCharacterError('\x7f')}, {"\x801vctc34", InvalidCharacterError('\x80')}, - {"an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4", InvalidLengthError(91)}, + {"an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4", + InvalidLengthError(91)}, {"qyrz8wqd2c9m", InvalidSeparatorIndexError(-1)}, {"1qyrz8wqd2c9m", InvalidSeparatorIndexError(0)}, {"y1b0jsk6g", NonCharsetCharError(98)},