github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/lib/login/select.go (about)

     1  package loginLib
     2  
     3  import (
     4  	"github.com/taubyte/tau-cli/env"
     5  	loginI18n "github.com/taubyte/tau-cli/i18n/login"
     6  	"github.com/taubyte/tau-cli/singletons/config"
     7  	"github.com/urfave/cli/v2"
     8  )
     9  
    10  /*
    11  if setDefault is true it will remove current default and set the
    12  newly selected profile as the default
    13  */
    14  func Select(ctx *cli.Context, name string, setDefault bool) error {
    15  	if setDefault {
    16  		configProfiles := config.Profiles()
    17  		profiles := configProfiles.List(true)
    18  		for profileName, profile := range profiles {
    19  			if profileName == name {
    20  				profile.Default = true
    21  				err := configProfiles.Set(profileName, profile)
    22  				if err != nil {
    23  					return loginI18n.SettingDefaultFailed(err)
    24  				}
    25  				continue
    26  			}
    27  
    28  			if profile.Default {
    29  				profile.Default = false
    30  
    31  				err := configProfiles.Set(profileName, profile)
    32  				if err != nil {
    33  					return loginI18n.RemovingDefaultFailed(err)
    34  				}
    35  			}
    36  		}
    37  	}
    38  
    39  	profile, err := config.Profiles().Get(name)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	env.SetSelectedNetwork(ctx, profile.NetworkType)
    45  	env.SetNetworkUrl(ctx, profile.Network)
    46  	return env.SetSelectedUser(ctx, name)
    47  }