-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/vechain/networkhub/hub" | ||
"github.com/vechain/networkhub/network" | ||
"github.com/vechain/networkhub/preset" | ||
) | ||
|
||
type Client struct { | ||
networkHub *hub.NetworkHub | ||
presets *preset.Networks | ||
storage *Storage | ||
} | ||
|
||
func New(networkHub *hub.NetworkHub, presets *preset.Networks) *Client { | ||
return &Client{ | ||
networkHub: networkHub, | ||
presets: presets, | ||
storage: NewInMemStorage(), | ||
} | ||
} | ||
|
||
func (c *Client) Stop(id string) error { | ||
return c.networkHub.StopNetwork(id) | ||
} | ||
|
||
func (c *Client) Start(id string) error { | ||
return c.networkHub.StartNetwork(id) | ||
} | ||
|
||
func (c *Client) Config(config string) (string, error) { | ||
var netCfg network.Network | ||
|
||
if err := json.Unmarshal([]byte(config), &netCfg); err != nil { | ||
return "", err | ||
} | ||
return c.config(&netCfg) | ||
} | ||
|
||
func (c *Client) LoadExistingNetworks() error { | ||
nets, err := c.storage.LoadExistingNetworks() | ||
if err != nil { | ||
return fmt.Errorf("unable to load existing networks: %w", err) | ||
} | ||
|
||
for networkID, net := range nets { | ||
loadedID, err := c.networkHub.LoadNetworkConfig(net) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if networkID != loadedID { | ||
return fmt.Errorf("unexpected networkID loaded: storedID:%s configuredID:%s", networkID, loadedID) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) Preset(presetNetwork string, environment, artifactPath string) (string, error) { | ||
netCfg, err := c.presets.Load(presetNetwork, &preset.APIConfigPayload{Environment: environment, ArtifactPath: artifactPath}) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to load network preset: %w", err) | ||
} | ||
return c.config(netCfg) | ||
} | ||
|
||
func (c *Client) config(netCfg *network.Network) (string, error) { | ||
networkID, err := c.networkHub.LoadNetworkConfig(netCfg) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to load config: %w", err) | ||
} | ||
|
||
networkInst, err := c.networkHub.GetNetwork(networkID) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to retrieve network: %w", err) | ||
} | ||
|
||
err = c.storage.Store(networkID, networkInst) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to store network: %w", err) | ||
} | ||
|
||
return networkID, 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,27 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/vechain/networkhub/network" | ||
"log/slog" | ||
) | ||
|
||
type Storage struct { | ||
path string | ||
storage map[string]*network.Network | ||
} | ||
|
||
func NewInMemStorage() *Storage { | ||
return &Storage{} | ||
} | ||
|
||
func (s *Storage) Store(networkID string, net *network.Network) error { | ||
// Add/Update the network entry | ||
s.storage[networkID] = net | ||
|
||
slog.Info("Network saved to memory") | ||
return nil | ||
} | ||
|
||
func (s *Storage) LoadExistingNetworks() (map[string]*network.Network, error) { | ||
return s.storage, nil | ||
} |