From 9350d7d3c963f0b39070691337bd8610bbf3da8a Mon Sep 17 00:00:00 2001 From: ZeljkoBenovic Date: Sat, 7 May 2022 01:55:14 +0200 Subject: [PATCH] server export command fixes * server export command moved to dedicated package * added success message to the output * added support for exporting config file to json format --- command/server/{ => config}/config.go | 8 +-- command/server/export/export.go | 81 +++++++++++++++++++++++++++ command/server/export/params.go | 13 +++++ command/server/export/result.go | 16 ++++++ command/server/generate_config.go | 45 --------------- command/server/init.go | 3 +- command/server/params.go | 11 ++-- command/server/server.go | 14 ++++- 8 files changed, 133 insertions(+), 58 deletions(-) rename command/server/{ => config}/config.go (96%) create mode 100644 command/server/export/export.go create mode 100644 command/server/export/params.go create mode 100644 command/server/export/result.go delete mode 100644 command/server/generate_config.go diff --git a/command/server/config.go b/command/server/config/config.go similarity index 96% rename from command/server/config.go rename to command/server/config/config.go index 69ffe1133f..3179cff0e7 100644 --- a/command/server/config.go +++ b/command/server/config/config.go @@ -1,4 +1,4 @@ -package server +package config import ( "encoding/json" @@ -15,7 +15,7 @@ import ( // Config defines the server configuration params type Config struct { GenesisPath string `json:"chain_config" yaml:"chain_config"` - SecretsConfigPath string `json:"secrets_config" yaml:"secrets_config,omitempty"` + SecretsConfigPath string `json:"secrets_config" yaml:"secrets_config"` DataDir string `json:"data_dir" yaml:"data_dir"` BlockGasTarget string `json:"block_gas_target" yaml:"block_gas_target"` GRPCAddr string `json:"grpc_addr" yaml:"grpc_addr"` @@ -95,11 +95,11 @@ func DefaultConfig() *Config { } } -// readConfigFile reads the config file from the specified path, builds a Config object +// ReadConfigFile reads the config file from the specified path, builds a Config object // and returns it. // //Supported file types: .json, .hcl, .yaml, .yml -func readConfigFile(path string) (*Config, error) { +func ReadConfigFile(path string) (*Config, error) { data, err := ioutil.ReadFile(path) if err != nil { return nil, err diff --git a/command/server/export/export.go b/command/server/export/export.go new file mode 100644 index 0000000000..d5c9d25b4c --- /dev/null +++ b/command/server/export/export.go @@ -0,0 +1,81 @@ +package export + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/0xPolygon/polygon-edge/command" + "github.com/0xPolygon/polygon-edge/command/server/config" + "github.com/spf13/cobra" + "gopkg.in/yaml.v3" + "os" +) + +func GetCommand() *cobra.Command { + configCmd := &cobra.Command{ + Use: "export", + Short: "export default-config.yaml file with default parameters that can be used to run the server", + Run: runGenerateConfigCommand, + } + + setFlags(configCmd) + + return configCmd +} + +func setFlags(cmd *cobra.Command) { + cmd.Flags().StringVar( + ¶mFlagValues.FileType, + fileTypeFlag, + "yaml", + "file type of exported config file (yaml or json)", + ) +} + +func runGenerateConfigCommand(cmd *cobra.Command, _ []string) { + outputter := command.InitializeOutputter(cmd) + defer outputter.WriteOutput() + + if err := generateConfig(*config.DefaultConfig()); err != nil { + outputter.SetError(err) + + return + } + + outputter.SetCommandResult(&cmdResult{ + CommandOutput: "Configuration file successfully exported", + }) +} + +func generateConfig(config config.Config) error { + config.Network.MaxPeers = -1 + config.Network.MaxInboundPeers = -1 + config.Network.MaxOutboundPeers = -1 + + var ( + data []byte + err error + ) + + switch paramFlagValues.FileType { + case "yaml", "yml": + data, err = yaml.Marshal(config) + case "json": + data, err = json.MarshalIndent(config, "", " ") + default: + return errors.New("invalid file type, only yaml and json are supported") + } + + if err != nil { + return fmt.Errorf("could not marshal config struct, %w", err) + } + + if err := os.WriteFile( + fmt.Sprintf("default-config.%s", paramFlagValues.FileType), + data, + os.ModePerm); err != nil { + return errors.New("could not create and write config file") + } + + return nil +} diff --git a/command/server/export/params.go b/command/server/export/params.go new file mode 100644 index 0000000000..847dc22ed0 --- /dev/null +++ b/command/server/export/params.go @@ -0,0 +1,13 @@ +package export + +const ( + fileTypeFlag = "type" +) + +type exportParams struct { + FileType string +} + +var ( + paramFlagValues = &exportParams{} +) diff --git a/command/server/export/result.go b/command/server/export/result.go new file mode 100644 index 0000000000..ad124b370f --- /dev/null +++ b/command/server/export/result.go @@ -0,0 +1,16 @@ +package export + +import "bytes" + +type cmdResult struct { + CommandOutput string `json:"export_result"` +} + +func (c *cmdResult) GetOutput() string { + var buffer bytes.Buffer + + buffer.WriteString("\n[EXPORT SUCCESS]\n") + buffer.WriteString(c.CommandOutput + "\n") + + return buffer.String() +} diff --git a/command/server/generate_config.go b/command/server/generate_config.go deleted file mode 100644 index d60c58e39f..0000000000 --- a/command/server/generate_config.go +++ /dev/null @@ -1,45 +0,0 @@ -package server - -import ( - "fmt" - "github.com/0xPolygon/polygon-edge/command" - "github.com/spf13/cobra" - "gopkg.in/yaml.v3" - "os" -) - -func getGenerateConfigCmd() *cobra.Command { - configCmd := &cobra.Command{ - Use: "export", - Short: "export default-config.yaml file with default parameters that can be used to run the server", - Run: runGenerateConfigCommand, - } - - return configCmd -} - -func runGenerateConfigCommand(cmd *cobra.Command, _ []string) { - outputter := command.InitializeOutputter(cmd) - - if err := generateConfig(*DefaultConfig()); err != nil { - outputter.SetError(err) - outputter.WriteOutput() - } -} - -func generateConfig(config Config) error { - config.Network.MaxPeers = -1 - config.Network.MaxInboundPeers = -1 - config.Network.MaxOutboundPeers = -1 - - data, err := yaml.Marshal(config) - if err != nil { - return fmt.Errorf("could not marshal Config struct") - } - - if err := os.WriteFile("default-config.yaml", data, os.ModePerm); err != nil { - return fmt.Errorf("could not create and write default-config.yaml file") - } - - return nil -} diff --git a/command/server/init.go b/command/server/init.go index 0a3e1abb03..40535acc98 100644 --- a/command/server/init.go +++ b/command/server/init.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "github.com/0xPolygon/polygon-edge/command/server/config" "math" "net" @@ -18,7 +19,7 @@ import ( func (p *serverParams) initConfigFromFile() error { var parseErr error - if p.rawConfig, parseErr = readConfigFile(p.configPath); parseErr != nil { + if p.rawConfig, parseErr = config.ReadConfigFile(p.configPath); parseErr != nil { return parseErr } diff --git a/command/server/params.go b/command/server/params.go index 318f2386b5..5e645d7ece 100644 --- a/command/server/params.go +++ b/command/server/params.go @@ -2,6 +2,7 @@ package server import ( "errors" + "github.com/0xPolygon/polygon-edge/command/server/config" "net" "github.com/0xPolygon/polygon-edge/chain" @@ -42,10 +43,10 @@ const ( var ( params = &serverParams{ - rawConfig: &Config{ - Telemetry: &Telemetry{}, - Network: &Network{}, - TxPool: &TxPool{}, + rawConfig: &config.Config{ + Telemetry: &config.Telemetry{}, + Network: &config.Network{}, + TxPool: &config.TxPool{}, }, } ) @@ -56,7 +57,7 @@ var ( ) type serverParams struct { - rawConfig *Config + rawConfig *config.Config configPath string libp2pAddress *net.TCPAddr diff --git a/command/server/server.go b/command/server/server.go index 163dba1000..8d7f45a2d0 100644 --- a/command/server/server.go +++ b/command/server/server.go @@ -3,6 +3,8 @@ package server import ( "fmt" "github.com/0xPolygon/polygon-edge/command" + "github.com/0xPolygon/polygon-edge/command/server/config" + "github.com/0xPolygon/polygon-edge/command/server/export" "github.com/spf13/cobra" "github.com/0xPolygon/polygon-edge/command/helper" @@ -21,15 +23,21 @@ func GetCommand() *cobra.Command { helper.RegisterLegacyGRPCAddressFlag(serverCmd) helper.RegisterJSONRPCFlag(serverCmd) - // register subcommand used to generate config file on the fly - serverCmd.AddCommand(getGenerateConfigCmd()) + registerSubcommands(serverCmd) setFlags(serverCmd) return serverCmd } +func registerSubcommands(baseCmd *cobra.Command) { + baseCmd.AddCommand( + // server export + export.GetCommand(), + ) +} + func setFlags(cmd *cobra.Command) { - defaultConfig := DefaultConfig() + defaultConfig := config.DefaultConfig() cmd.Flags().StringVar( ¶ms.rawConfig.LogLevel,