Skip to content

Commit

Permalink
Change config loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianyi Wang committed Dec 6, 2023
1 parent 616db36 commit 70bf410
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
48 changes: 26 additions & 22 deletions config/env_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
smithyrequestcompression "github.com/aws/smithy-go/private/requestcompression"
)

// CredentialsSourceName provides a name of the provider when config is
Expand Down Expand Up @@ -334,7 +335,7 @@ func NewEnvConfig() (EnvConfig, error) {
if err := setBoolPtrFromEnvVal(&cfg.DisableRequestCompression, []string{awsDisableRequestCompression}); err != nil {
return cfg, err
}
if err := setRequestMinCompressSizeBytes(&cfg.RequestMinCompressSizeBytes); err != nil {
if err := setInt64PtrFromEnvVal(&cfg.RequestMinCompressSizeBytes, []string{awsRequestMinCompressionSizeBytes}, smithyrequestcompression.MaxRequestMinCompressSizeBytes); err != nil {
return cfg, err
}

Expand Down Expand Up @@ -402,27 +403,6 @@ func (c EnvConfig) getAppID(context.Context) (string, bool, error) {
return c.AppID, len(c.AppID) > 0, nil
}

func setRequestMinCompressSizeBytes(bytes **int64) error {
b := os.Getenv(awsRequestMinCompressionSizeBytes)
if b == "" {
return nil
}

byte, err := strconv.ParseInt(b, 10, 64)
if err != nil {
return fmt.Errorf("invalid value for env var, %s=%s, need int64",
awsRequestMinCompressionSizeBytes, b)
} else if byte < 0 || byte > 10485760 {
return fmt.Errorf("invalid range for env var min request compression size bytes %q, must be within 0 and 10485760 inclusively", byte)
}
if *bytes == nil {
*bytes = new(int64)
}
**bytes = byte

return nil
}

func (c EnvConfig) getDisableRequestCompression(context.Context) (bool, bool, error) {
if c.DisableRequestCompression == nil {
return false, false, nil
Expand Down Expand Up @@ -693,6 +673,30 @@ func setBoolPtrFromEnvVal(dst **bool, keys []string) error {
return nil
}

func setInt64PtrFromEnvVal(dst **int64, keys []string, max int64) error {
for _, k := range keys {
value := os.Getenv(k)
if len(value) == 0 {
continue
}

v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return fmt.Errorf("invalid value for env var, %s=%s, need int64", k, value)
} else if v < 0 || v > max {
return fmt.Errorf("invalid range for env var min request compression size bytes %q, must be within 0 and 10485760 inclusively", v)
}
if *dst == nil {
*dst = new(int64)
}

**dst = v
break
}

return nil
}

func setEndpointDiscoveryTypeFromEnvVal(dst *aws.EndpointDiscoveryEnableState, keys []string) error {
for _, k := range keys {
value := os.Getenv(k)
Expand Down
3 changes: 2 additions & 1 deletion config/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aws/aws-sdk-go-v2/internal/ini"
"github.com/aws/aws-sdk-go-v2/internal/shareddefaults"
"github.com/aws/smithy-go/logging"
smithyrequestcompression "github.com/aws/smithy-go/private/requestcompression"
)

const (
Expand Down Expand Up @@ -1147,7 +1148,7 @@ func updateRequestMinCompressSizeBytes(bytes **int64, sec ini.Section, key strin
if !ok {
return fmt.Errorf("invalid value for min request compression size bytes %s, need int64", sec.String(key))
}
if v < 0 || v > 10485760 {
if v < 0 || v > smithyrequestcompression.MaxRequestMinCompressSizeBytes {
return fmt.Errorf("invalid range for min request compression size bytes %d, must be within 0 and 10485760 inclusively", v)
}
*bytes = new(int64)
Expand Down

0 comments on commit 70bf410

Please sign in to comment.