github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/singletons/patrick_client/client.go (about) 1 package patrickClient 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/taubyte/tau-cli/common" 8 "github.com/taubyte/tau-cli/env" 9 "github.com/taubyte/tau-cli/i18n" 10 networkI18n "github.com/taubyte/tau-cli/i18n/network" 11 singletonsI18n "github.com/taubyte/tau-cli/i18n/singletons" 12 loginLib "github.com/taubyte/tau-cli/lib/login" 13 "github.com/taubyte/tau-cli/singletons/config" 14 "github.com/taubyte/tau-cli/singletons/dreamland" 15 "github.com/taubyte/tau-cli/singletons/session" 16 "github.com/taubyte/tau-cli/states" 17 "github.com/taubyte/tau/clients/http" 18 client "github.com/taubyte/tau/clients/http/patrick" 19 ) 20 21 var _client *client.Client 22 23 func Clear() { 24 _client = nil 25 } 26 27 func getClientUrl() (url string, err error) { 28 profile, err := loginLib.GetSelectedProfile() 29 if err != nil { 30 return "", err 31 } 32 33 switch profile.NetworkType { 34 case common.DreamlandNetwork: 35 port, err := dreamland.HTTPPort(context.TODO(), "patrick") 36 if err != nil { 37 return "", err 38 } 39 40 url = fmt.Sprintf("http://127.0.0.1:%d", port) 41 case common.RemoteNetwork: 42 url = fmt.Sprintf("https://patrick.tau.%s", profile.Network) 43 default: 44 err = networkI18n.ErrorUnknownNetwork(profile.NetworkType) 45 } 46 47 return 48 } 49 50 func loadClient() (config.Profile, *client.Client, error) { 51 profileName, exist := session.Get().ProfileName() 52 if !exist { 53 // Check for a default if no profiles are selected 54 profileName, _, _ = loginLib.GetProfiles() 55 if len(profileName) == 0 { 56 i18n.Help().HaveYouLoggedIn() 57 return config.Profile{}, nil, singletonsI18n.ProfileDoesNotExist() 58 } 59 } 60 61 profile, err := config.Profiles().Get(profileName) 62 if err != nil { 63 return config.Profile{}, nil, err 64 } 65 66 selectedNetwork, _ := env.GetSelectedNetwork() 67 if selectedNetwork == "" { 68 i18n.Help().HaveYouSelectedANetwork() 69 return config.Profile{}, nil, singletonsI18n.NoNetworkSelected() 70 } 71 72 url, err := getClientUrl() 73 if err != nil { 74 return config.Profile{}, nil, err 75 } 76 77 ops := []http.Option{http.URL(url), http.Auth(profile.Token)} 78 client, err := client.New(states.Context, ops...) 79 if err != nil { 80 return profile, nil, singletonsI18n.CreatingPatrickClientFailed(err) 81 } 82 83 return profile, client, nil 84 }