Skip to content

Commit

Permalink
feat: Migrate networks to yml file
Browse files Browse the repository at this point in the history
  • Loading branch information
activescott committed Mar 11, 2024
1 parent b5f9168 commit 21785c0
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 349 deletions.
3 changes: 2 additions & 1 deletion rocketpool-cli/rocketpool-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ ______ _ _ ______ _
if cfg.Smartnode.GetRplFaucetAddress() != "" {
faucet.RegisterCommands(app, "faucet", []string{"f"})
}

service.RegisterCommands(app, "service", []string{"s"}, cfg)
}

minipool.RegisterCommands(app, "minipool", []string{"m"})
network.RegisterCommands(app, "network", []string{"e"})
node.RegisterCommands(app, "node", []string{"n"})
odao.RegisterCommands(app, "odao", []string{"o"})
queue.RegisterCommands(app, "queue", []string{"q"})
service.RegisterCommands(app, "service", []string{"s"})
wallet.RegisterCommands(app, "wallet", []string{"w"})

app.Before = func(c *cli.Context) error {
Expand Down
9 changes: 4 additions & 5 deletions rocketpool-cli/service/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,16 @@ func createFlagsFromConfigParams(sectionName string, params []*cfgtypes.Paramete
}

// Register commands
func RegisterCommands(app *cli.App, name string, aliases []string) {
func RegisterCommands(app *cli.App, name string, aliases []string, cfg *config.RocketPoolConfig) {

configFlags := []cli.Flag{}
cfgTemplate := config.NewRocketPoolConfig("", false)
network := cfgTemplate.Smartnode.Network.Value.(cfgtypes.Network)
network := cfg.Smartnode.Network.Value.(cfgtypes.Network)

// Root params
configFlags = createFlagsFromConfigParams("", cfgTemplate.GetParameters(), configFlags, network)
configFlags = createFlagsFromConfigParams("", cfg.GetParameters(), configFlags, network)

// Subconfigs
for sectionName, subconfig := range cfgTemplate.GetSubconfigs() {
for sectionName, subconfig := range cfg.GetSubconfigs() {
configFlags = createFlagsFromConfigParams(sectionName, subconfig.GetParameters(), configFlags, network)
}

Expand Down
2 changes: 1 addition & 1 deletion rocketpool-cli/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ func resyncEth2(c *cli.Context) error {

// Generate a YAML file that shows the current configuration schema, including all of the parameters and their descriptions
func getConfigYaml(c *cli.Context) error {
cfg := config.NewRocketPoolConfig("", false)
cfg := config.NewRocketPoolConfig("", false, config.NewNetworksConfig())
bytes, err := yaml.Marshal(cfg)
if err != nil {
return fmt.Errorf("error serializing configuration schema: %w", err)
Expand Down
65 changes: 65 additions & 0 deletions shared/services/config/networks-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package config

import (
"fmt"
"os"
"path"

"github.com/rocket-pool/smartnode/shared/types/config"
"gopkg.in/yaml.v2"
)

const defaultNetworksConfigPath = "networks-default.yml"

type NetworksConfig struct {
Networks []*config.NetworkInfo `yaml:"networks"`
}

func NewNetworksConfig() *NetworksConfig {
return &NetworksConfig{
Networks: make([]*config.NetworkInfo, 0),
}
}

func LoadNetworksFromFile(configPath string) (*NetworksConfig, error) {
_, err := os.Stat(configPath)
if os.IsNotExist(err) {
return nil, fmt.Errorf("config path does not exist: %s", configPath)
}

filePath := path.Join(configPath, defaultNetworksConfigPath)
if os.IsNotExist(err) {
return nil, fmt.Errorf("default networks file does not exist: %s", filePath)
}

bytes, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("could not read default networks file: %w", err)
}

networks := NetworksConfig{}
err = yaml.Unmarshal(bytes, &networks)
if err != nil {
return nil, fmt.Errorf("could not unmarshal default networks file: %w", err)
}

return &networks, nil
}

func (nc *NetworksConfig) GetNetworks() []*config.NetworkInfo {
return nc.Networks
}

func (nc *NetworksConfig) SaveToFile(filePath string) error {
// TODO: this signature doesn't support saving to default/extras files
d, err := yaml.Marshal(nc)
if err != nil {
return fmt.Errorf("could not marshal networks config: %w", err)
}

err = os.WriteFile(filePath, d, 0644)
if err != nil {
return fmt.Errorf("could not write networks config to file to %s: %w", filePath, err)
}
return nil
}
15 changes: 11 additions & 4 deletions shared/services/config/rocket-pool-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type RocketPoolConfig struct {

IsNativeMode bool `yaml:"-"`

networks *NetworksConfig `yaml:"-"`

// Execution client settings
ExecutionClientMode config.Parameter `yaml:"executionClientMode,omitempty"`
ExecutionClient config.Parameter `yaml:"executionClient,omitempty"`
Expand Down Expand Up @@ -165,9 +167,13 @@ func LoadFromFile(path string) (*RocketPoolConfig, error) {
if err := yaml.Unmarshal(configBytes, &settings); err != nil {
return nil, fmt.Errorf("could not parse settings file: %w", err)
}

// now load the networks:
networks, err := LoadNetworksFromFile(filepath.Dir(path))
if err != nil {
return nil, fmt.Errorf("could not load networks file: %w", err)
}
// Deserialize it into a config object
cfg := NewRocketPoolConfig(filepath.Dir(path), false)
cfg := NewRocketPoolConfig(filepath.Dir(path), false, networks)
err = cfg.Deserialize(settings)
if err != nil {
return nil, fmt.Errorf("could not deserialize settings file: %w", err)
Expand All @@ -178,7 +184,7 @@ func LoadFromFile(path string) (*RocketPoolConfig, error) {
}

// Creates a new Rocket Pool configuration instance
func NewRocketPoolConfig(rpDir string, isNativeMode bool) *RocketPoolConfig {
func NewRocketPoolConfig(rpDir string, isNativeMode bool, networks *NetworksConfig) *RocketPoolConfig {

clientModes := []config.ParameterOption{{
Name: "Locally Managed",
Expand All @@ -194,6 +200,7 @@ func NewRocketPoolConfig(rpDir string, isNativeMode bool) *RocketPoolConfig {
Title: "Top-level Settings",
RocketPoolDirectory: rpDir,
IsNativeMode: isNativeMode,
networks: networks,

ExecutionClientMode: config.Parameter{
ID: "executionClientMode",
Expand Down Expand Up @@ -503,7 +510,7 @@ func getAugmentedEcDescription(client config.ExecutionClient, originalDescriptio

// Create a copy of this configuration.
func (cfg *RocketPoolConfig) CreateCopy() *RocketPoolConfig {
newConfig := NewRocketPoolConfig(cfg.RocketPoolDirectory, cfg.IsNativeMode)
newConfig := NewRocketPoolConfig(cfg.RocketPoolDirectory, cfg.IsNativeMode, cfg.networks)

// Set the network
network := cfg.Smartnode.Network.Value.(config.Network)
Expand Down
Loading

0 comments on commit 21785c0

Please sign in to comment.