Skip to content

Commit

Permalink
move network config (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof authored Jan 11, 2024
1 parent 9746ad5 commit e6b206f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/massastation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
os.Exit(0)
}

networkManager, err := config.NewNetworkManager(configDir)
networkManager, err := config.NewNetworkManager()
if err != nil {
logger.Fatalf("Failed to create NetworkManager: %s", err.Error())
}
Expand Down
35 changes: 31 additions & 4 deletions int/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"sync"

Expand All @@ -20,6 +21,7 @@ const (
buildnetRPC = "https://buildnet.massa.net/api/v2"
buildnetChainID = 77658366
permissionUrwGrOr = 0o644
configDirName = "massa-station"
)

type NetworkInfos struct {
Expand Down Expand Up @@ -48,15 +50,15 @@ type NetworkManager struct {
// NewNetworkManager creates a new instance of NetworkManager.
// It loads the initial network configurations from the specified file and sets the default network configuration.
// Returns the initialized NetworkManager and any error encountered during initialization.
func NewNetworkManager(configDir string) (*NetworkManager, error) {
func NewNetworkManager() (*NetworkManager, error) {
//nolint: exhaustruct
networkManager := &NetworkManager{
networkInfos: NetworkInfos{},
knownNetworks: make(map[string]NetworkConfig),
}

// Load network configuration
initNetworksData, err := LoadConfig(configDir)
initNetworksData, err := LoadConfig()
if err != nil {
return nil, fmt.Errorf("failed to load configuration: %w", err)
}
Expand Down Expand Up @@ -176,10 +178,15 @@ func (n *NetworkManager) SwitchNetwork(selectedNetworkStr string) error {

// LoadConfig loads network configurations from YAML file.
// Returns the loaded network configurations and any error encountered during loading.
func LoadConfig(configDir string) (map[string]NetworkConfig, error) {
func LoadConfig() (map[string]NetworkConfig, error) {
configDir, err := configDirPath()
if err != nil {
return nil, fmt.Errorf("failed to get user config dir: %w", err)
}

networkConfigPath := path.Join(configDir, networkConfigFile)

_, err := os.Stat(networkConfigPath)
_, err = os.Stat(networkConfigPath)
if os.IsNotExist(err) {
createDefaultConfig(networkConfigPath)
} else if err != nil {
Expand Down Expand Up @@ -226,3 +233,23 @@ func createDefaultConfig(networkConfigPath string) {
logger.Fatalf("failed to write default networks to file: %v", err)
}
}

// configDirPath returns the path where the network config yaml file is stored.
func configDirPath() (string, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("getting user config directory: %w", err)
}

path := filepath.Join(configDir, configDirName)

// create the directory if it doesn't exist
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return "", fmt.Errorf("creating account directory '%s': %w", path, err)
}
}

return path, nil
}

0 comments on commit e6b206f

Please sign in to comment.