Skip to content

Commit

Permalink
server export command fixes
Browse files Browse the repository at this point in the history
* server export command moved to dedicated package
* added success message to the output
* added support for exporting config file to json format
  • Loading branch information
ZeljkoBenovic committed May 6, 2022
1 parent 26d6042 commit 9350d7d
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 58 deletions.
8 changes: 4 additions & 4 deletions command/server/config.go → command/server/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package config

import (
"encoding/json"
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions command/server/export/export.go
Original file line number Diff line number Diff line change
@@ -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(
&paramFlagValues.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
}
13 changes: 13 additions & 0 deletions command/server/export/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package export

const (
fileTypeFlag = "type"
)

type exportParams struct {
FileType string
}

var (
paramFlagValues = &exportParams{}
)
16 changes: 16 additions & 0 deletions command/server/export/result.go
Original file line number Diff line number Diff line change
@@ -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()
}
45 changes: 0 additions & 45 deletions command/server/generate_config.go

This file was deleted.

3 changes: 2 additions & 1 deletion command/server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"github.com/0xPolygon/polygon-edge/command/server/config"
"math"
"net"

Expand All @@ -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
}

Expand Down
11 changes: 6 additions & 5 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"errors"
"github.com/0xPolygon/polygon-edge/command/server/config"
"net"

"github.com/0xPolygon/polygon-edge/chain"
Expand Down Expand Up @@ -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{},
},
}
)
Expand All @@ -56,7 +57,7 @@ var (
)

type serverParams struct {
rawConfig *Config
rawConfig *config.Config
configPath string

libp2pAddress *net.TCPAddr
Expand Down
14 changes: 11 additions & 3 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
&params.rawConfig.LogLevel,
Expand Down

0 comments on commit 9350d7d

Please sign in to comment.