Skip to content

Commit

Permalink
feat: add beekeeper logger (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gacevicljubisa authored Dec 21, 2023
1 parent 904df4c commit 97b77a9
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 211 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51
version: v1.54.2
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ linters:
enable:
- asciicheck
- bidichk
- depguard
# - depguard
- dogsled
- durationcheck
- errcheck
Expand Down Expand Up @@ -56,4 +56,4 @@ issues:
- linters:
- forbidigo
path: cmd/
text: "use of `fmt.Print" ## allow fmt.Print in cmd directory
text: "use of `fmt.Print" ## allow fmt.Print in cmd directory
66 changes: 53 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ package main

import (
"context"
"fmt"
"io"
"log"
"strings"

"github.com/ethersphere/beekeeper/pkg/logging"
"github.com/ethersphere/node-funder/pkg/funder"
"github.com/spf13/cobra"
)

const (
optionLogVerbosity string = "log-verbosity"
)

func main() {
cfg := funder.Config{}

var logLevel string

rootCmd := &cobra.Command{
Use: "funder",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -24,13 +34,21 @@ func main() {
},
}

rootCmd.PersistentFlags().StringVar(&logLevel, optionLogVerbosity, "info", "log verbosity level 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=trace")

logger, err := newLogger(rootCmd, logLevel)
if err != nil {
log.Fatal(err)
}

fundCmd := &cobra.Command{
Use: "fund",
Short: "fund (top up) bee node wallets",
Run: func(cmd *cobra.Command, args []string) {
doFund(cfg)
doFund(cfg, logger)
},
}

fundCmd.PersistentFlags().StringVar(&cfg.Namespace, "namespace", "", "kubernetes namespace")
fundCmd.PersistentFlags().StringSliceVar(&cfg.Addresses, "addresses", nil, "wallet addresses")
fundCmd.PersistentFlags().StringVar(&cfg.ChainNodeEndpoint, "chainNodeEndpoint", "", "endpoint to chain node")
Expand All @@ -42,52 +60,74 @@ func main() {
Use: "stake",
Short: "stake (top up) bee nodes",
Run: func(cmd *cobra.Command, args []string) {
doStake(cfg)
doStake(cfg, logger)
},
}
stakeCmd.PersistentFlags().StringVar(&cfg.Namespace, "namespace", "", "kubernetes namespace")
stakeCmd.PersistentFlags().Float64Var(&cfg.MinAmounts.SwarmToken, "minSwarm", 0, "specifies min amount of swarm tokens (BZZ) nodes should have staked")

rootCmd.AddCommand(fundCmd)
rootCmd.AddCommand(stakeCmd)
rootCmd.AddCommand(fundCmd, stakeCmd)

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
logger.Fatal(err)
}
}

func doFund(cfg funder.Config) {
func doFund(cfg funder.Config, logger logging.Logger) {
ctx := context.Background()

if cfg.Namespace == "" && len(cfg.Addresses) == 0 {
log.Fatalf("--namespace or --addresses must be set")
logger.Fatalf("--namespace or --addresses must be set")
return
}

if cfg.ChainNodeEndpoint == "" {
log.Fatalf("--chainNodeEndpoint must be set")
logger.Fatalf("--chainNodeEndpoint must be set")
return
}

if cfg.WalletKey == "" {
log.Fatalf("--walletKey must be set")
logger.Fatalf("--walletKey must be set")
return
}

if err := funder.Fund(ctx, cfg, nil, nil); err != nil {
log.Fatalf("error while funding: %v", err)
logger.Fatalf("error while funding: %v", err)
}
}

func doStake(cfg funder.Config) {
func doStake(cfg funder.Config, logger logging.Logger) {
ctx := context.Background()

if cfg.Namespace == "" {
log.Fatalf("--namespace must be set")
logger.Fatalf("--namespace must be set")
return
}

if err := funder.Stake(ctx, cfg, nil); err != nil {
log.Fatalf("error while funding: %v", err)
logger.Fatalf("error while funding: %v", err)
}
}

func newLogger(cmd *cobra.Command, verbosity string) (logging.Logger, error) {
var logger logging.Logger

switch strings.ToLower(verbosity) {
case "0", "silent":
logger = logging.New(io.Discard, 0)
case "1", "error":
logger = logging.New(cmd.OutOrStdout(), 2)
case "2", "warn":
logger = logging.New(cmd.OutOrStdout(), 3)
case "3", "info":
logger = logging.New(cmd.OutOrStdout(), 4)
case "4", "debug":
logger = logging.New(cmd.OutOrStdout(), 5)
case "5", "trace":
logger = logging.New(cmd.OutOrStdout(), 6)
default:
return nil, fmt.Errorf("unknown %s level %q, use help to check flag usage options", optionLogVerbosity, verbosity)
}

return logger, nil
}
66 changes: 44 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,58 +1,80 @@
module github.com/ethersphere/node-funder

go 1.20
go 1.21

require (
github.com/btcsuite/btcd v0.22.1
github.com/ethereum/go-ethereum v1.10.26
github.com/ethersphere/bee v1.16.1
github.com/btcsuite/btcd v0.22.3
github.com/ethereum/go-ethereum v1.13.4
github.com/ethersphere/bee v1.17.6
github.com/ethersphere/beekeeper v0.15.0
github.com/ethersphere/go-sw3-abi v0.4.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.4
k8s.io/apimachinery v0.22.16
k8s.io/client-go v0.22.16
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
)

require (
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.7.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/shirou/gopsutil v3.21.5+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.6 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.22.16 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
Loading

0 comments on commit 97b77a9

Please sign in to comment.