This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
26d6042
commit 9350d7d
Showing
8 changed files
with
133 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
¶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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters