erigon-pulse/cmd/erigon/main.go
ledgerwatch e75ea786c0
[devnet tool] separate logging (#7526)
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro-2.local>
2023-05-17 07:36:06 +01:00

129 lines
3.2 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/log/v3"
"github.com/pelletier/go-toml"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
"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/debug"
"github.com/ledgerwatch/erigon/turbo/node"
)
func main() {
defer func() {
panicResult := recover()
if panicResult == nil {
return
}
log.Error("catch panic", "err", panicResult, "stack", dbg.Stack())
os.Exit(1)
}()
app := erigonapp.MakeApp("erigon", runErigon, erigoncli.DefaultFlags)
if err := app.Run(os.Args); err != nil {
_, printErr := fmt.Fprintln(os.Stderr, err)
if printErr != nil {
log.Warn("Fprintln error", "err", printErr)
}
os.Exit(1)
}
}
func runErigon(cliCtx *cli.Context) error {
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)
}
}
var logger log.Logger
var err error
if logger, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}
// 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)
nodeCfg := node.NewNodConfigUrfave(cliCtx, logger)
ethCfg := node.NewEthConfigUrfave(cliCtx, nodeCfg, logger)
ethNode, err := node.New(nodeCfg, ethCfg, logger)
if err != nil {
log.Error("Erigon startup", "err", err)
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 {
fileExtension := filepath.Ext(filePath)
fileConfig := make(map[string]interface{})
if fileExtension == ".yaml" {
yamlFile, err := os.ReadFile(filePath)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, fileConfig)
if err != nil {
return err
}
} else if fileExtension == ".toml" {
tomlFile, err := os.ReadFile(filePath)
if err != nil {
return err
}
err = toml.Unmarshal(tomlFile, &fileConfig)
if err != nil {
return err
}
} else {
return errors.New("config files only accepted are .yaml and .toml")
}
// sets global flags to value in yaml/toml file
for key, value := range fileConfig {
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.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.Set(key, fmt.Sprintf("%v", value))
if err != nil {
return fmt.Errorf("failed setting %s flag with value=%v error=%s", key, value, err)
}
}
}
}
return nil
}