2021-05-26 10:35:39 +00:00
// Package cli contains framework for building a command-line based Erigon node.
2020-09-05 16:07:27 +00:00
package cli
import (
2021-09-07 06:44:40 +00:00
"github.com/ledgerwatch/erigon/cmd/utils"
2021-05-20 18:25:53 +00:00
"github.com/ledgerwatch/erigon/internal/debug"
"github.com/ledgerwatch/erigon/internal/flags"
2021-09-07 06:44:40 +00:00
"github.com/ledgerwatch/erigon/node"
"github.com/ledgerwatch/erigon/params"
2020-09-05 16:07:27 +00:00
"github.com/urfave/cli"
)
2020-09-21 14:10:25 +00:00
// MakeApp creates a cli application (based on `github.com/urlfave/cli` package).
// The application exits when `action` returns.
// Parameters:
// * action: the main function for the application. receives `*cli.Context` with parsed command-line flags. Returns no error, if some error could not be recovered from write to the log or panic.
// * cliFlags: the list of flags `cli.Flag` that the app should set and parse. By default, use `DefaultFlags()`. If you want to specify your own flag, use `append(DefaultFlags(), myFlag)` for this parameter.
2020-09-05 16:07:27 +00:00
func MakeApp ( action func ( * cli . Context ) , cliFlags [ ] cli . Flag ) * cli . App {
2021-05-26 10:35:39 +00:00
app := flags . NewApp ( "" , "" , "erigon experimental cli" )
2020-09-05 16:07:27 +00:00
app . Action = action
app . Flags = append ( cliFlags , debug . Flags ... ) // debug flags are required
app . Before = func ( ctx * cli . Context ) error {
return debug . Setup ( ctx )
}
app . After = func ( ctx * cli . Context ) error {
debug . Exit ( )
return nil
}
2021-12-27 07:59:29 +00:00
app . Commands = [ ] cli . Command { initCommand , snapshotCommand }
2020-09-05 16:07:27 +00:00
return app
}
2021-09-07 06:44:40 +00:00
func MigrateFlags ( action func ( ctx * cli . Context ) error ) func ( * cli . Context ) error {
return func ( ctx * cli . Context ) error {
for _ , name := range ctx . FlagNames ( ) {
if ctx . IsSet ( name ) {
ctx . GlobalSet ( name , ctx . String ( name ) )
}
}
return action ( ctx )
}
}
func NewNodeConfig ( ctx * cli . Context ) * node . Config {
nodeConfig := node . DefaultConfig
// see simiar changes in `cmd/geth/config.go#defaultNodeConfig`
if commit := params . GitCommit ; commit != "" {
nodeConfig . Version = params . VersionWithCommit ( commit , "" )
} else {
nodeConfig . Version = params . Version
}
2021-09-08 08:25:10 +00:00
nodeConfig . IPCPath = "" // force-disable IPC endpoint
2021-09-07 06:44:40 +00:00
nodeConfig . Name = "erigon"
if ctx . GlobalIsSet ( utils . DataDirFlag . Name ) {
nodeConfig . DataDir = ctx . GlobalString ( utils . DataDirFlag . Name )
}
return & nodeConfig
}
func MakeConfigNodeDefault ( ctx * cli . Context ) * node . Node {
return makeConfigNode ( NewNodeConfig ( ctx ) )
}
func makeConfigNode ( config * node . Config ) * node . Node {
stack , err := node . New ( config )
if err != nil {
utils . Fatalf ( "Failed to create Erigon node: %v" , err )
}
return stack
}