Skip to content

Commit

Permalink
use zerolog for logging similar to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Dec 18, 2023
1 parent 3e7bd5d commit 5036108
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 44 deletions.
17 changes: 10 additions & 7 deletions cmd/deposit.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package cmd

import (
"log"

"github.com/MalteHerrmann/evmos-utils/gov"
"github.com/MalteHerrmann/evmos-utils/utils"
"github.com/spf13/cobra"
)

//nolint:gochecknoglobals // required by cobra
var depositCmd = &cobra.Command{
Use: "deposit",
Use: "deposit [PROPOSAL_ID]",
Short: "Deposit for a governance proposal",
Long: `Deposit the minimum needed deposit for a given governance proposal.
If no proposal ID is given by the user, the latest proposal is queried and deposited for.`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
bin, err := utils.NewEvmosTestingBinary()
if err != nil {
log.Fatalf("error creating binary: %v", err)
bin.Logger.Error().Msgf("error creating binary: %v", err)
return

Check failure on line 20 in cmd/deposit.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

if err = bin.GetAccounts(); err != nil {
log.Fatalf("error getting accounts: %v", err)
bin.Logger.Error().Msgf("error getting accounts: %v", err)
return

Check failure on line 25 in cmd/deposit.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
}

err = gov.Deposit(bin, args)
proposalID, err := gov.Deposit(bin, args)
if err != nil {
log.Fatalf("error depositing: %v", err)
bin.Logger.Error().Msgf("error depositing: %v", err)
return

Check failure on line 31 in cmd/deposit.go

View workflow job for this annotation

GitHub Actions / lint

return with no blank line before (nlreturn)
} else {

Check warning on line 32 in cmd/deposit.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
bin.Logger.Info().Msgf("successfully deposited for proposal %d", proposalID)
}
},
}
Expand Down
21 changes: 13 additions & 8 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"log"
"regexp"

"github.com/MalteHerrmann/evmos-utils/gov"
Expand All @@ -12,28 +11,34 @@ import (

//nolint:gochecknoglobals // required by cobra
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Use: "upgrade TARGET_VERSION",
Short: "Prepare an upgrade of a node",
Long: `Prepare an upgrade of a node by submitting a governance proposal,
voting for it using all keys of in the keyring and having it pass.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
bin, err := utils.NewEvmosTestingBinary()
if err != nil {
log.Fatalf("error creating binary: %v", err)
bin.Logger.Error().Msgf("error creating binary: %v", err)
return
}

if err = bin.GetAccounts(); err != nil {
log.Fatalf("error getting accounts: %v", err)
bin.Logger.Error().Msgf("error getting accounts: %v", err)
return
}

targetVersion := args[0]
if matched, _ := regexp.MatchString(`v\d+\.\d+\.\d(-rc\d+)?`, targetVersion); !matched {
log.Fatalf("invalid target version: %s; please use the format vX.Y.Z(-rc*).\n", targetVersion)
bin.Logger.Error().Msgf("invalid target version: %s; please use the format vX.Y.Z(-rc*).", targetVersion)
return
}

if err := upgradeLocalNode(bin, targetVersion); err != nil {
log.Fatalf("error upgrading local node: %v", err)
bin.Logger.Error().Msgf("error upgrading local node: %v", err)
return
} else {

Check warning on line 40 in cmd/upgrade.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
bin.Logger.Info().Msgf("successfully prepared upgrade to %s", targetVersion)
}
},
}
Expand All @@ -53,14 +58,14 @@ func upgradeLocalNode(bin *utils.Binary, targetVersion string) error {

upgradeHeight := currentHeight + utils.DeltaHeight

log.Println("Submitting upgrade proposal...")
bin.Logger.Error().Msg("Submitting upgrade proposal...")

proposalID, err := gov.SubmitUpgradeProposal(bin, targetVersion, upgradeHeight)
if err != nil {
return errors.Wrap(err, "error executing upgrade proposal")
}

log.Printf("Scheduled upgrade to %s at height %d.\n", targetVersion, upgradeHeight)
bin.Logger.Info().Msgf("Scheduled upgrade to %s at height %d.\n", targetVersion, upgradeHeight)

if err = gov.SubmitAllVotesForProposal(bin, proposalID); err != nil {
return errors.Wrapf(err, "error submitting votes for proposal %d", proposalID)
Expand Down
18 changes: 11 additions & 7 deletions cmd/vote.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package cmd

import (
"log"

"github.com/MalteHerrmann/evmos-utils/gov"
"github.com/MalteHerrmann/evmos-utils/utils"
"github.com/spf13/cobra"
)

//nolint:gochecknoglobals // required by cobra
var voteCmd = &cobra.Command{
Use: "vote",
Use: "vote [PROPOSAL_ID]",
Short: "Vote for a governance proposal",
Long: `Vote for a governance proposal with all keys in the keyring.
If no proposal ID is passed, the latest proposal on chain is queried and used.`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
bin, err := utils.NewEvmosTestingBinary()
if err != nil {
log.Fatalf("error creating binary: %v", err)
bin.Logger.Error().Msgf("error creating binary: %v", err)
return
}

if err = bin.GetAccounts(); err != nil {
log.Fatalf("error getting accounts: %v", err)
bin.Logger.Error().Msgf("error getting accounts: %v", err)
return
}

if err = gov.SubmitAllVotes(bin, args); err != nil {
log.Fatal(err)
proposalID, err := gov.SubmitAllVotes(bin, args)
if err != nil {
bin.Logger.Error().Msgf("error submitting votes: %v", err)
return
} else {

Check warning on line 32 in cmd/vote.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
bin.Logger.Info().Msgf("successfully submitted votes for proposal %d", proposalID)
}
},
}
Expand Down
9 changes: 4 additions & 5 deletions gov/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gov

import (
"fmt"
"log"
"regexp"
"strconv"

Expand All @@ -12,18 +11,18 @@ import (
)

// Deposit deposits the minimum needed deposit for a given governance proposal.
func Deposit(bin *utils.Binary, args []string) error {
func Deposit(bin *utils.Binary, args []string) (int, error) {
deposit, err := GetMinDeposit(bin)
if err != nil {
log.Fatalf("error getting minimum deposit: %v", err)
return 0, errors.Wrap(err, "failed to get minimum deposit")
}

proposalID, err := GetProposalIDFromInput(bin, args)
if err != nil {
log.Fatalf("error getting proposal ID: %v", err)
return 0, errors.Wrap(err, "failed to get proposal ID")
}

return DepositForProposal(
return proposalID, DepositForProposal(
bin, proposalID, bin.Accounts[0].Name, deposit.String(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions gov/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func GetProposalIDFromInput(bin *utils.Binary, args []string) (int, error) {
return 0, errors.Wrap(err, "error querying latest proposal ID")
}
case 1:
proposalID, err = strconv.Atoi(args[2])
proposalID, err = strconv.Atoi(args[0])
if err != nil {
return 0, errors.Wrapf(err, "error converting proposal ID %s to integer", args[2])
return 0, errors.Wrapf(err, "error converting proposal ID %s to integer", args[0])
}
default:
return 0, fmt.Errorf("invalid number of arguments; expected 0 or 1; got %d", len(args))
Expand Down
13 changes: 6 additions & 7 deletions gov/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gov

import (
"fmt"
"log"
"strconv"
"strings"

Expand All @@ -11,13 +10,13 @@ import (
)

// SubmitAllVotes submits a vote for the given proposal ID using all testing accounts.
func SubmitAllVotes(bin *utils.Binary, args []string) error {
func SubmitAllVotes(bin *utils.Binary, args []string) (int, error) {
proposalID, err := GetProposalIDFromInput(bin, args)
if err != nil {
return err
return 0, err
}

return SubmitAllVotesForProposal(bin, proposalID)
return proposalID, SubmitAllVotesForProposal(bin, proposalID)
}

// SubmitAllVotesForProposal submits a vote for the given proposal ID using all testing accounts.
Expand All @@ -32,7 +31,7 @@ func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error {
}

utils.Wait(1)
log.Printf("Voting for proposal %d...\n", proposalID)
bin.Logger.Info().Msgf("Voting for proposal %d", proposalID)

var out string

Expand All @@ -47,9 +46,9 @@ func SubmitAllVotesForProposal(bin *utils.Binary, proposalID int) error {
return fmt.Errorf("proposal with ID %d is inactive", proposalID)
}

log.Printf(" - could NOT vote using key: %s\n", acc.Name)
bin.Logger.Error().Msgf("could not vote using key %s: %v", acc.Name, err)
} else {
log.Printf(" - voted using key: %s\n", acc.Name)
bin.Logger.Info().Msgf("voted using key %s", acc.Name)
}
}

Expand Down
19 changes: 13 additions & 6 deletions utils/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"fmt"
"github.com/rs/zerolog"

Check failure on line 5 in utils/binary.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"github.com/rs/zerolog/log"
"os"
"os/exec"
"path"

Check failure on line 9 in utils/binary.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
Expand All @@ -23,10 +25,12 @@ type Binary struct {
Appd string
// Accounts are the accounts stored in the local keyring.
Accounts []Account
// Logger is a logger to be used within all commands.
Logger zerolog.Logger
}

// NewBinary returns a new Binary instance.
func NewBinary(home, appd string) (*Binary, error) {
func NewBinary(home, appd string, logger zerolog.Logger) (*Binary, error) {
// check if home directory exists
if _, err := os.Stat(home); os.IsNotExist(err) {
return nil, errors.Wrap(err, fmt.Sprintf("home directory does not exist: %s", home))
Expand All @@ -44,23 +48,26 @@ func NewBinary(home, appd string) (*Binary, error) {
}

return &Binary{
Cdc: cdc,
Home: home,
Appd: appd,
Cdc: cdc,
Home: home,
Appd: appd,
Logger: logger,
}, nil
}

// NewEvmosTestingBinary returns a new Binary instance with the default home and appd
// setup for the Evmos local node testing setup.
func NewEvmosTestingBinary() (*Binary, error) {
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout})

userHome, err := os.UserHomeDir()
if err != nil {
return nil, errors.Wrap(err, "failed to get user home dir")
return &Binary{Logger: logger}, errors.Wrap(err, "failed to get user home dir")
}

defaultEvmosdHome := path.Join(userHome, ".tmp-evmosd")

return NewBinary(defaultEvmosdHome, "evmosd")
return NewBinary(defaultEvmosdHome, "evmosd", logger)
}

// GetCodec returns the codec to be used for the client.
Expand Down
3 changes: 1 addition & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
Expand Down Expand Up @@ -46,7 +45,7 @@ func ExecuteBinaryCmd(bin *Binary, args BinaryCmdArgs) (string, error) {

output, err := cmd.CombinedOutput()
if err != nil && !args.Quiet {
log.Println(string(output))
bin.Logger.Error().Msg(string(output))
}

return string(output), err
Expand Down

0 comments on commit 5036108

Please sign in to comment.