Skip to content

Commit

Permalink
Remove unused fields and use the default const if no config set
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed Apr 3, 2024
1 parent e472e5f commit 3e7ecbc
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 34 deletions.
6 changes: 5 additions & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
6 changes: 5 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
}
Expand All @@ -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 != "" {
Expand All @@ -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)
}
}
Expand Down
13 changes: 1 addition & 12 deletions cmd/config_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
18 changes: 3 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -159,7 +158,6 @@ func initConfig() { //nolint:gocyclo
if apiKeyFromEnv != "" && apiSecretFromEnv != "" {
account.CurrentAccount.Name = "<environment variables>"
gConfigFilePath = "<environment variables>"
account.CurrentAccount.Account = "unknown"
account.CurrentAccount.Key = apiKeyFromEnv
account.CurrentAccount.Secret = apiSecretFromEnv

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3e7ecbc

Please sign in to comment.