go-ethereum/internal/cli/server/command.go
Pratik Patil e62beee1b2 internal/cli: use config file for flags in new-cli, builder/files: update defaults (#457)
* updated simple.json and simple.hcl

* added annotations for developer and grpc block

* added toml tags and simple.toml file

* added support for toml config files

* updated simple files toml, hcl, json

* added config.toml in builder/files and updated bor.service

* add dumpconfig command in cli for exporting configs

* update docs

* updated .goreleaser.yml (POS-651)

* changed --config to -config

* added test config for tests and fixed lint errors

* made fields of type big.int and time.Duration private, removed merge from dumpconfig, setting up default values to the Raw fields in dumpconfig, and fixed one lint error

* fixed lint errors (strange)

* lint fix

* private no-more, using '-' in name tags to ignore

* updated name tags, made c.configFile as a stringFlag (only one config file supported) and updated the merge logic in command.go -> Run()

* fix: set method for big.Int flags, added a TODO

* handeled bigInt and timeDuration type, bug fix in config_legacy, lint fix

* updated flags, consistent across flags.go and config.go

* fixed config test and updated test hcl, json config files

* updated config legacy test

* added test to check values of cmd flags, restructured Run in command.go, linter fix

* fix linters

* lint again

* changed 2 flags and assert -> require

* changed the 2 flags back

* updated correct congig.toml path and made mainnet default

* updated config.toml with new flags

* added sample config (toml) file

* removed sample-config.toml and added it in docs/config.md

Co-authored-by: Manav Darji <manavdarji.india@gmail.com>
2022-07-29 17:12:30 -07:00

143 lines
2.6 KiB
Go

package server
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/mitchellh/cli"
"github.com/ethereum/go-ethereum/log"
)
// Command is the command to start the sever
type Command struct {
UI cli.Ui
// cli configuration
cliConfig *Config
// final configuration
config *Config
configFile string
srv *Server
}
// MarkDown implements cli.MarkDown interface
func (c *Command) MarkDown() string {
items := []string{
"# Server",
"The ```bor server``` command runs the Bor client.",
c.Flags().MarkDown(),
}
return strings.Join(items, "\n\n")
}
// Help implements the cli.Command interface
func (c *Command) Help() string {
return `Usage: bor [options]
Run the Bor server.
` + c.Flags().Help()
}
// Synopsis implements the cli.Command interface
func (c *Command) Synopsis() string {
return "Run the Bor server"
}
func (c *Command) extractFlags(args []string) error {
config := *DefaultConfig()
flags := c.Flags()
if err := flags.Parse(args); err != nil {
c.UI.Error(err.Error())
c.config = &config
return err
}
// TODO: Check if this can be removed or not
// read cli flags
if err := config.Merge(c.cliConfig); err != nil {
c.UI.Error(err.Error())
c.config = &config
return err
}
// read if config file is provided, this will overwrite the cli flags, if provided
if c.configFile != "" {
log.Warn("Config File provided, this will overwrite the cli flags.", "configFile:", c.configFile)
cfg, err := readConfigFile(c.configFile)
if err != nil {
c.UI.Error(err.Error())
c.config = &config
return err
}
if err := config.Merge(cfg); err != nil {
c.UI.Error(err.Error())
c.config = &config
return err
}
}
c.config = &config
return nil
}
// Run implements the cli.Command interface
func (c *Command) Run(args []string) int {
err := c.extractFlags(args)
if err != nil {
c.UI.Error(err.Error())
return 1
}
srv, err := NewServer(c.config)
if err != nil {
c.UI.Error(err.Error())
return 1
}
c.srv = srv
return c.handleSignals()
}
func (c *Command) handleSignals() int {
signalCh := make(chan os.Signal, 4)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
sig := <-signalCh
c.UI.Output(fmt.Sprintf("Caught signal: %v", sig))
c.UI.Output("Gracefully shutting down agent...")
gracefulCh := make(chan struct{})
go func() {
c.srv.Stop()
close(gracefulCh)
}()
for i := 10; i > 0; i-- {
select {
case <-signalCh:
log.Warn("Already shutting down, interrupt more force stop.", "times", i-1)
case <-gracefulCh:
return 0
}
}
return 1
}
// GetConfig returns the user specified config
func (c *Command) GetConfig() *Config {
return c.cliConfig
}