diff --git a/cmd/client.go b/cmd/client.go index bc89f2df..9298c4ea 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -62,10 +62,14 @@ func buildClient() { httpClient := &http.Client{Transport: newCLIRoundTripper(http.DefaultTransport, account.CurrentAccount.CustomHeaders)} + clientTimeout := account.CurrentAccount.ClientTimeout + if clientTimeout == 0 { + clientTimeout = defaultClientTimeout + } clientExoV2, err := exov2.NewClient( account.CurrentAccount.Key, account.CurrentAccount.APISecret(), - exov2.ClientOptWithTimeout(time.Minute*time.Duration(account.CurrentAccount.ClientTimeout)), + exov2.ClientOptWithTimeout(time.Minute*time.Duration(clientTimeout)), exov2.ClientOptWithHTTPClient(httpClient), exov2.ClientOptCond(func() bool { if v := os.Getenv("EXOSCALE_TRACE"); v != "" { diff --git a/cmd/cmd.go b/cmd/cmd.go index 67544894..89c6f7d3 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -81,7 +81,11 @@ func cmdSetZoneFlagFromDefault(cmd *cobra.Command) { // set once this function returns. func cmdSetTemplateFlagFromDefault(cmd *cobra.Command) { if cmd.Flag("template").Value.String() == "" { - cmd.Flag("template").Value.Set(account.CurrentAccount.DefaultTemplate) // nolint:errcheck + if account.CurrentAccount.DefaultTemplate != "" { + cmd.Flag("template").Value.Set(account.CurrentAccount.DefaultTemplate) // nolint:errcheck + } else { + cmd.Flag("template").Value.Set(defaultTemplate) // nolint:errcheck + } } } diff --git a/cmd/config.go b/cmd/config.go index 2cda2969..b94df963 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -120,7 +120,7 @@ func saveConfig(filePath string, newAccounts *account.Config) error { if acc.DefaultOutputFormat != "" { accounts[i]["defaultOutputFormat"] = acc.DefaultOutputFormat } - if acc.Environment != "" { + if acc.Environment != "" && acc.Environment != "api" { accounts[i]["environment"] = acc.Environment } if acc.Endpoint != "" { @@ -132,12 +132,14 @@ func saveConfig(filePath string, newAccounts *account.Config) error { if acc.DefaultTemplate != "" { accounts[i]["defaultTemplate"] = acc.DefaultTemplate } + if acc.Account != "" { + accounts[i]["account"] = acc.Account + } if len(acc.SecretCommand) != 0 { accounts[i]["secretCommand"] = acc.SecretCommand } else { accounts[i]["secret"] = acc.Secret } - accounts[i]["account"] = acc.Account conf.Accounts = append(conf.Accounts, acc) } @@ -150,7 +152,7 @@ func saveConfig(filePath string, newAccounts *account.Config) error { accounts[accountsSize+i]["key"] = acc.Key accounts[accountsSize+i]["secret"] = acc.Secret accounts[accountsSize+i]["defaultZone"] = acc.DefaultZone - if acc.Environment != "" { + if acc.Environment != "" && acc.Environment != "api" { accounts[i]["environment"] = acc.Environment } if acc.Endpoint != "" { @@ -159,7 +161,6 @@ func saveConfig(filePath string, newAccounts *account.Config) error { if acc.DefaultSSHKey != "" { accounts[accountsSize+i]["defaultSSHKey"] = acc.DefaultSSHKey } - accounts[accountsSize+i]["account"] = acc.Account conf.Accounts = append(conf.Accounts, acc) } } diff --git a/cmd/config_add.go b/cmd/config_add.go index 4d9422df..79337e51 100644 --- a/cmd/config_add.go +++ b/cmd/config_add.go @@ -68,10 +68,7 @@ func promptAccountInformation() (*account.Account, error) { var client *exo.Client reader := bufio.NewReader(os.Stdin) - account := &account.Account{ - Key: "", - Secret: "", - } + account := &account.Account{} apiKey, err := readInput(reader, "API Key", account.Key) if err != nil { @@ -94,14 +91,6 @@ func promptAccountInformation() (*account.Account, error) { account.Secret = secretKey } - acc, err := readInput(reader, "Account name", account.Account) - if err != nil { - return nil, err - } - if acc != "" { - account.Account = acc - } - name, err := readInput(reader, "Name", account.Name) if err != nil { return nil, err diff --git a/cmd/root.go b/cmd/root.go index 40fd803c..2660e758 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -87,10 +87,9 @@ func Execute(version, commit string) { func init() { account.CurrentAccount = &account.Account{ - DefaultZone: defaultZone, - DefaultTemplate: defaultTemplate, - Environment: defaultEnvironment, - SosEndpoint: defaultSosEndpoint, + DefaultZone: defaultZone, + Environment: defaultEnvironment, + SosEndpoint: defaultSosEndpoint, } gConfig = viper.New() @@ -159,7 +158,6 @@ func initConfig() { //nolint:gocyclo if apiKeyFromEnv != "" && apiSecretFromEnv != "" { account.CurrentAccount.Name = "" gConfigFilePath = "" - account.CurrentAccount.Account = "unknown" account.CurrentAccount.Key = apiKeyFromEnv account.CurrentAccount.Secret = apiSecretFromEnv @@ -173,9 +171,6 @@ func initConfig() { //nolint:gocyclo if sosEndpointFromEnv != "" { account.CurrentAccount.SosEndpoint = sosEndpointFromEnv } - if account.CurrentAccount.ClientTimeout == 0 { - account.CurrentAccount.ClientTimeout = defaultClientTimeout - } account.GAllAccount = &account.Config{ DefaultAccount: account.CurrentAccount.Name, @@ -307,17 +302,10 @@ func initConfig() { //nolint:gocyclo } } - if account.CurrentAccount.DefaultTemplate == "" { - account.CurrentAccount.DefaultTemplate = defaultTemplate - } - if account.CurrentAccount.SosEndpoint == "" { account.CurrentAccount.SosEndpoint = defaultSosEndpoint } - if account.CurrentAccount.ClientTimeout == 0 { - account.CurrentAccount.ClientTimeout = defaultClientTimeout - } clientTimeoutFromEnv := readFromEnv("EXOSCALE_API_TIMEOUT") if clientTimeoutFromEnv != "" { if t, err := strconv.Atoi(clientTimeoutFromEnv); err == nil { diff --git a/pkg/account/account.go b/pkg/account/account.go index 3cc0909b..d2118dd1 100644 --- a/pkg/account/account.go +++ b/pkg/account/account.go @@ -13,7 +13,8 @@ var ( ) type Account struct { - Name string + Name string + // TODO: deprecated field, will be deleted. Account string // TODO: remove it to replace it with the new API listZones. SosEndpoint string