github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/commands/resources/network/select.go (about)

     1  package network
     2  
     3  import (
     4  	"fmt"
     5  
     6  	cliCommon "github.com/taubyte/tau-cli/cli/common"
     7  	"github.com/taubyte/tau-cli/common"
     8  	"github.com/taubyte/tau-cli/env"
     9  	loginLib "github.com/taubyte/tau-cli/lib/login"
    10  	"github.com/taubyte/tau-cli/prompts"
    11  	"github.com/taubyte/tau-cli/singletons/config"
    12  	"github.com/taubyte/tau-cli/singletons/dreamland"
    13  	"github.com/taubyte/tau-cli/validate"
    14  	slices "github.com/taubyte/utils/slices/string"
    15  
    16  	networkFlags "github.com/taubyte/tau-cli/flags/network"
    17  	networkI18n "github.com/taubyte/tau-cli/i18n/network"
    18  	"github.com/urfave/cli/v2"
    19  )
    20  
    21  func (link) Select() cliCommon.Command {
    22  	return cliCommon.Create(
    23  		&cli.Command{
    24  			Action: _select,
    25  			Flags:  []cli.Flag{networkFlags.FQDN, networkFlags.Universe},
    26  		},
    27  	)
    28  }
    29  
    30  func _select(ctx *cli.Context) error {
    31  	// Setting string flag with value counts as two
    32  	if ctx.NumFlags() > 2 {
    33  		return networkI18n.FlagError()
    34  	}
    35  
    36  	profile, err := loginLib.GetSelectedProfile()
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	switch {
    42  	case ctx.IsSet(networkFlags.FQDN.Name):
    43  		profile.NetworkType = common.RemoteNetwork
    44  		profile.Network = ctx.String(networkFlags.FQDN.Name)
    45  
    46  		if err := validate.SeerFQDN(ctx.Context, profile.Network); err != nil {
    47  			return err
    48  		}
    49  
    50  		if !slices.Contains(profile.History, profile.Network) {
    51  			profile.History = append(profile.History, profile.Network)
    52  		}
    53  
    54  	case ctx.IsSet(networkFlags.Universe.Name):
    55  		dreamClient, err := dreamland.Client(ctx.Context)
    56  		if err != nil {
    57  			return fmt.Errorf("creating dreamland client failed with: %w", err)
    58  		}
    59  
    60  		universes, err := dreamClient.Status()
    61  		if err != nil {
    62  			return fmt.Errorf("calling dreamland status failed with: %w", err)
    63  		}
    64  
    65  		universeName := ctx.String(networkFlags.Universe.Name)
    66  		_, ok := universes[universeName]
    67  		if !ok {
    68  			return fmt.Errorf("universe `%s` was not found", universeName)
    69  		}
    70  
    71  		profile.NetworkType = common.DreamlandNetwork
    72  		profile.Network = universeName
    73  	default:
    74  		dreamClient, err := dreamland.Client(ctx.Context)
    75  		if err != nil {
    76  			return fmt.Errorf("creating dreamland client failed with: %w", err)
    77  		}
    78  
    79  		networkSelections := []string{common.RemoteNetwork}
    80  		if _, err := dreamClient.Status(); err == nil {
    81  			networkSelections = append(networkSelections, common.DreamlandNetwork)
    82  		}
    83  
    84  		networkSelections = append(networkSelections, profile.History...)
    85  
    86  		prev := []string{}
    87  		if len(profile.NetworkType) > 0 {
    88  			prev = append(prev, profile.NetworkType)
    89  		}
    90  
    91  		network := prompts.GetOrAskForSelection(ctx, "Network", prompts.NetworkPrompts, networkSelections, prev...)
    92  		if network == common.RemoteNetwork {
    93  			profile.NetworkType = common.RemoteNetwork
    94  			profile.Network = prompts.GetOrRequireAString(ctx, "", prompts.FQDN, validate.FQDNValidator, profile.Network)
    95  			if err := validate.SeerFQDN(ctx.Context, profile.Network); err != nil {
    96  				return err
    97  			}
    98  
    99  			if !slices.Contains(profile.History, profile.Network) {
   100  				profile.History = append(profile.History, profile.Network)
   101  			}
   102  
   103  		} else if network == common.DreamlandNetwork {
   104  			universes, err := dreamClient.Status()
   105  			if err != nil {
   106  				return fmt.Errorf("calling dreamland status failed with: %w", err)
   107  			}
   108  
   109  			universeNames := make([]string, 0, len(universes))
   110  			for name := range universes {
   111  				universeNames = append(universeNames, name)
   112  			}
   113  
   114  			profile.Network, err = prompts.SelectInterface(universeNames, prompts.Universe, "")
   115  			if err != nil {
   116  				return fmt.Errorf("universe selection failed with: %w", err)
   117  			}
   118  		} else {
   119  			profile.NetworkType = common.RemoteNetwork
   120  			profile.Network = network
   121  		}
   122  	}
   123  
   124  	config.Profiles().Set(profile.Name(), profile)
   125  	if err := env.SetSelectedNetwork(ctx, profile.NetworkType); err != nil {
   126  		return err
   127  	}
   128  
   129  	if err := env.SetNetworkUrl(ctx, profile.Network); err != nil {
   130  		return err
   131  	}
   132  
   133  	networkI18n.Success(profile.Network)
   134  
   135  	return nil
   136  }