Skip to content

Commit

Permalink
fix: linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Aug 21, 2023
1 parent 2d8e86f commit b7bc834
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 70 deletions.
22 changes: 11 additions & 11 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -155,35 +155,35 @@ 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)
}
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)
}
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)
}
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)
}
Expand All @@ -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{}) {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
32 changes: 16 additions & 16 deletions cmd/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions cmd/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func buildAllAddressesCmd(parentCmd *cobra.Command) {
line += " (Imported)"
}

cmd.PrintInfoMsg(line)
cmd.PrintInfoMsgf(line)
}
}
}
Expand All @@ -87,7 +87,7 @@ func buildNewAddressCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintInfoMsg("%s", addr)
cmd.PrintInfoMsgf("%s", addr)
}
}

Expand All @@ -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))
}
}
Expand All @@ -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)
}
}

Expand All @@ -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())
}
}
}
Expand Down Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func buildSetLabelCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintSuccessMsg("Label set successfully")
cmd.PrintSuccessMsgf("Label set successfully")
}
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/wallet/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
Expand Down Expand Up @@ -63,6 +63,6 @@ func buildChangePasswordCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintWarnMsg("Wallet password updated")
cmd.PrintWarnMsgf("Wallet password updated")
}
}
6 changes: 3 additions & 3 deletions cmd/wallet/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand All @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/wallet/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -55,6 +55,6 @@ func buildGetSeedCmd(parentCmd *cobra.Command) {
cmd.FatalErrorCheck(err)

cmd.PrintLine()
cmd.PrintInfoMsg("Seed: \"%v\"", mnemonic)
cmd.PrintInfoMsgf("Seed: \"%v\"", mnemonic)
}
}
44 changes: 22 additions & 22 deletions cmd/wallet/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
Loading

0 comments on commit b7bc834

Please sign in to comment.