Skip to content

Commit

Permalink
adding client
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Jul 24, 2024
1 parent acb1dc5 commit 1bd0937
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
87 changes: 87 additions & 0 deletions entrypoint/client/client.go
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
}
27 changes: 27 additions & 0 deletions entrypoint/client/storage_inmem.go
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
}

0 comments on commit 1bd0937

Please sign in to comment.