Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade urfave/cli to v2 #6047

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions cmd/abigen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ import (
"regexp"
"strings"

"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"

"github.com/ledgerwatch/erigon/accounts/abi"
"github.com/ledgerwatch/erigon/accounts/abi/bind"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/common/compiler"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/params"
cli2 "github.com/ledgerwatch/erigon/turbo/cli"
"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli"
)

var (
Expand Down Expand Up @@ -100,30 +101,30 @@ var (
func init() {
app = cli2.NewApp(params.GitCommit, "", "ethereum checkpoint helper tool")
app.Flags = []cli.Flag{
abiFlag,
binFlag,
typeFlag,
jsonFlag,
solFlag,
solcFlag,
vyFlag,
vyperFlag,
excFlag,
pkgFlag,
outFlag,
langFlag,
aliasFlag,
&abiFlag,
&binFlag,
&typeFlag,
&jsonFlag,
&solFlag,
&solcFlag,
&vyFlag,
&vyperFlag,
&excFlag,
&pkgFlag,
&outFlag,
&langFlag,
&aliasFlag,
}
app.Action = abigen
}

func abigen(c *cli.Context) error {
utils.CheckExclusive(c, abiFlag, jsonFlag, solFlag, vyFlag) // Only one source can be selected.
if c.GlobalString(pkgFlag.Name) == "" {
if c.String(pkgFlag.Name) == "" {
utils.Fatalf("No destination package specified (--pkg)")
}
var lang bind.Lang
switch c.GlobalString(langFlag.Name) {
switch c.String(langFlag.Name) {
case "go":
lang = bind.LangGo
case "java":
Expand All @@ -132,7 +133,7 @@ func abigen(c *cli.Context) error {
lang = bind.LangObjC
utils.Fatalf("Objc binding generation is uncompleted")
default:
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.GlobalString(langFlag.Name))
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.String(langFlag.Name))
}
// If the entire solidity code was specified, build and bind based on that
var (
Expand All @@ -143,13 +144,13 @@ func abigen(c *cli.Context) error {
libs = make(map[string]string)
aliases = make(map[string]string)
)
if c.GlobalString(abiFlag.Name) != "" {
if c.String(abiFlag.Name) != "" {
// Load up the ABI, optional bytecode and type name from the parameters
var (
abiBytes []byte
err error
)
input := c.GlobalString(abiFlag.Name)
input := c.String(abiFlag.Name)
if input == "-" {
abiBytes, err = io.ReadAll(os.Stdin)
} else {
Expand All @@ -161,7 +162,7 @@ func abigen(c *cli.Context) error {
abis = append(abis, string(abiBytes))

var bin []byte
if binFile := c.GlobalString(binFlag.Name); binFile != "" {
if binFile := c.String(binFlag.Name); binFile != "" {
if bin, err = os.ReadFile(binFile); err != nil {
utils.Fatalf("Failed to read input bytecode: %v", err)
}
Expand All @@ -171,28 +172,28 @@ func abigen(c *cli.Context) error {
}
bins = append(bins, string(bin))

kind := c.GlobalString(typeFlag.Name)
kind := c.String(typeFlag.Name)
if kind == "" {
kind = c.GlobalString(pkgFlag.Name)
kind = c.String(pkgFlag.Name)
}
types = append(types, kind)
} else {
// Generate the list of types to exclude from binding
exclude := make(map[string]bool)
for _, kind := range strings.Split(c.GlobalString(excFlag.Name), ",") {
for _, kind := range strings.Split(c.String(excFlag.Name), ",") {
exclude[strings.ToLower(kind)] = true
}
var err error
var contracts map[string]*compiler.Contract

switch {
case c.GlobalIsSet(solFlag.Name):
contracts, err = compiler.CompileSolidity(c.GlobalString(solcFlag.Name), c.GlobalString(solFlag.Name))
case c.IsSet(solFlag.Name):
contracts, err = compiler.CompileSolidity(c.String(solcFlag.Name), c.String(solFlag.Name))
if err != nil {
utils.Fatalf("Failed to build Solidity contract: %v", err)
}
case c.GlobalIsSet(vyFlag.Name):
output, err := compiler.CompileVyper(c.GlobalString(vyperFlag.Name), c.GlobalString(vyFlag.Name))
case c.IsSet(vyFlag.Name):
output, err := compiler.CompileVyper(c.String(vyperFlag.Name), c.String(vyFlag.Name))
if err != nil {
utils.Fatalf("Failed to build Vyper contract: %v", err)
}
Expand All @@ -208,8 +209,8 @@ func abigen(c *cli.Context) error {
contracts[name] = contract
}

case c.GlobalIsSet(jsonFlag.Name):
jsonOutput, err := os.ReadFile(c.GlobalString(jsonFlag.Name))
case c.IsSet(jsonFlag.Name):
jsonOutput, err := os.ReadFile(c.String(jsonFlag.Name))
if err != nil {
utils.Fatalf("Failed to read combined-json from compiler: %v", err)
}
Expand Down Expand Up @@ -238,28 +239,28 @@ func abigen(c *cli.Context) error {
}
}
// Extract all aliases from the flags
if c.GlobalIsSet(aliasFlag.Name) {
if c.IsSet(aliasFlag.Name) {
// We support multi-versions for aliasing
// e.g.
// foo=bar,foo2=bar2
// foo:bar,foo2:bar2
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
submatches := re.FindAllStringSubmatch(c.GlobalString(aliasFlag.Name), -1)
submatches := re.FindAllStringSubmatch(c.String(aliasFlag.Name), -1)
for _, match := range submatches {
aliases[match[1]] = match[2]
}
}
// Generate the contract binding
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases)
code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases)
if err != nil {
utils.Fatalf("Failed to generate ABI binding: %v", err)
}
// Either flush it out to a file or display on the standard output
if !c.GlobalIsSet(outFlag.Name) {
if !c.IsSet(outFlag.Name) {
fmt.Printf("%s\n", code)
return nil
}
if err := os.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
if err := os.WriteFile(c.String(outFlag.Name), []byte(code), 0600); err != nil {
utils.Fatalf("Failed to write ABI binding: %v", err)
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions cmd/devnet/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon/cmd/devnet/devnetutils"
Expand Down Expand Up @@ -77,7 +77,7 @@ func StartNode(wg *sync.WaitGroup, args []string) {
}

// runNode configures, creates and serves an erigon node
func runNode(ctx *cli.Context) {
func runNode(ctx *cli.Context) error {
logger := log.New()

// Initializing the node and providing the current git commit there
Expand All @@ -89,13 +89,14 @@ func runNode(ctx *cli.Context) {
ethNode, err := node.New(nodeCfg, ethCfg, logger)
if err != nil {
log.Error("Devnet startup", "err", err)
return
return err
}

err = ethNode.Serve()
if err != nil {
log.Error("error while serving Devnet node", "err", err)
}
return err
}

// miningNodeArgs returns custom args for starting a mining node
Expand Down
21 changes: 11 additions & 10 deletions cmd/erigon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"reflect"
"strings"

"github.com/ledgerwatch/erigon/turbo/logging"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/log/v3"
"github.com/pelletier/go-toml"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"

"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon/cmd/utils"
"github.com/ledgerwatch/erigon/params"
erigonapp "github.com/ledgerwatch/erigon/turbo/app"
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"
"github.com/ledgerwatch/erigon/turbo/logging"
"github.com/ledgerwatch/erigon/turbo/node"
"github.com/ledgerwatch/log/v3"
)

func main() {
Expand All @@ -43,13 +43,13 @@ func main() {
}
}

func runErigon(cliCtx *cli.Context) {
func runErigon(cliCtx *cli.Context) error {
logger := logging.GetLoggerCtx("erigon", cliCtx)

// initializing the node and providing the current git commit there
logger.Info("Build info", "git_branch", params.GitBranch, "git_tag", params.GitTag, "git_commit", params.GitCommit)

configFilePath := cliCtx.GlobalString(utils.ConfigFlag.Name)
configFilePath := cliCtx.String(utils.ConfigFlag.Name)
if configFilePath != "" {
if err := setFlagsFromConfigFile(cliCtx, configFilePath); err != nil {
log.Warn("failed setting config flags from yaml/toml file", "err", err)
Expand All @@ -62,12 +62,13 @@ func runErigon(cliCtx *cli.Context) {
ethNode, err := node.New(nodeCfg, ethCfg, logger)
if err != nil {
log.Error("Erigon startup", "err", err)
return
return err
}
err = ethNode.Serve()
if err != nil {
log.Error("error while serving an Erigon node", "err", err)
}
return err
}

func setFlagsFromConfigFile(ctx *cli.Context, filePath string) error {
Expand Down Expand Up @@ -98,19 +99,19 @@ func setFlagsFromConfigFile(ctx *cli.Context, filePath string) error {
}
// sets global flags to value in yaml/toml file
for key, value := range fileConfig {
if !ctx.GlobalIsSet(key) {
if !ctx.IsSet(key) {
if reflect.ValueOf(value).Kind() == reflect.Slice {
sliceInterface := value.([]interface{})
s := make([]string, len(sliceInterface))
for i, v := range sliceInterface {
s[i] = fmt.Sprintf("%v", v)
}
err := ctx.GlobalSet(key, strings.Join(s, ","))
err := ctx.Set(key, strings.Join(s, ","))
if err != nil {
return fmt.Errorf("failed setting %s flag with values=%s error=%s", key, s, err)
}
} else {
err := ctx.GlobalSet(key, fmt.Sprintf("%v", value))
err := ctx.Set(key, fmt.Sprintf("%v", value))
if err != nil {
return fmt.Errorf("failed setting %s flag with value=%v error=%s", key, value, err)

Expand Down
10 changes: 6 additions & 4 deletions cmd/erigoncustom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"os"

"github.com/urfave/cli/v2"

erigonapp "github.com/ledgerwatch/erigon/turbo/app"
erigoncli "github.com/ledgerwatch/erigon/turbo/cli"

"github.com/urfave/cli"
)

// defining a custom command-line flag, a string
Expand All @@ -25,7 +25,7 @@ const (
func main() {
// initializing Erigon application here and providing our custom flag
app := erigonapp.MakeApp(runErigon,
append(erigoncli.DefaultFlags, flag), // always use DefaultFlags, but add a new one in the end.
append(erigoncli.DefaultFlags, &flag), // always use DefaultFlags, but add a new one in the end.
)
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -34,7 +34,7 @@ func main() {
}

// Erigon main function
func runErigon(ctx *cli.Context) {
func runErigon(ctx *cli.Context) error {
// running a node and initializing a custom bucket with all default settings
//eri := node.New(ctx, node.Params{
// CustomBuckets: map[string]dbutils.BucketConfigItem{
Expand All @@ -46,5 +46,7 @@ func runErigon(ctx *cli.Context) {

//if err != nil {
// log.Error("error while serving a Erigon node", "err", err)
// return err
//}
return nil
}
6 changes: 3 additions & 3 deletions cmd/evm/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"fmt"
"os"

"github.com/ledgerwatch/erigon/cmd/evm/internal/compiler"
"github.com/urfave/cli/v2"

"github.com/urfave/cli"
"github.com/ledgerwatch/erigon/cmd/evm/internal/compiler"
)

var compileCommand = cli.Command{
Expand All @@ -34,7 +34,7 @@ var compileCommand = cli.Command{
}

func compileCmd(ctx *cli.Context) error {
debug := ctx.GlobalBool(DebugFlag.Name)
debug := ctx.Bool(DebugFlag.Name)

if len(ctx.Args().First()) == 0 {
return errors.New("filename required")
Expand Down
7 changes: 4 additions & 3 deletions cmd/evm/disasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"os"
"strings"

"github.com/urfave/cli/v2"

"github.com/ledgerwatch/erigon/core/asm"
"github.com/urfave/cli"
)

var disasmCommand = cli.Command{
Expand All @@ -43,8 +44,8 @@ func disasmCmd(ctx *cli.Context) error {
return err
}
in = string(input)
case ctx.GlobalIsSet(InputFlag.Name):
in = ctx.GlobalString(InputFlag.Name)
case ctx.IsSet(InputFlag.Name):
in = ctx.String(InputFlag.Name)
default:
return errors.New("missing filename or --input value")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"fmt"
"strings"

"github.com/urfave/cli/v2"

"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/tests"
"github.com/urfave/cli"
)

var (
Expand Down
Loading