github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/singletons/dreamland/client.go (about)

     1  package dreamland
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"time"
     8  
     9  	dreamland "github.com/taubyte/dreamland/service"
    10  	"github.com/taubyte/dreamland/service/api"
    11  	"github.com/taubyte/tau-cli/env"
    12  )
    13  
    14  var dream_client *dreamland.Client
    15  
    16  func Client(ctx context.Context) (*dreamland.Client, error) {
    17  	if dream_client == nil {
    18  		var err error
    19  		dream_client, err = dreamland.New(ctx, dreamland.URL("http://127.0.0.1:1421"), dreamland.Timeout(15*time.Second))
    20  		if err != nil {
    21  			return nil, err
    22  		}
    23  	}
    24  
    25  	return dream_client, nil
    26  }
    27  
    28  func Status(ctx context.Context) (echart api.Echart, err error) {
    29  	var dreamClient *dreamland.Client
    30  	dreamClient, err = Client(ctx)
    31  	if err != nil {
    32  		return
    33  	}
    34  
    35  	selectedUniverse, _ := env.GetCustomNetworkUrl()
    36  	universe := dreamClient.Universe(selectedUniverse)
    37  	echart, err = universe.Status()
    38  	return
    39  }
    40  
    41  func HTTPPort(ctx context.Context, name string) (int, error) {
    42  	echart, err := Status(ctx)
    43  	if err != nil {
    44  		return 0, err
    45  	}
    46  
    47  	for _, node := range echart.Nodes {
    48  		if strings.Contains(node.Name, name) {
    49  			httpPort, ok := node.Value["http"]
    50  			if !ok {
    51  				return 0, fmt.Errorf("http port for `%s` not set", name)
    52  			}
    53  
    54  			return httpPort, nil
    55  		}
    56  	}
    57  
    58  	return 0, fmt.Errorf("node `%s` not found", name)
    59  }