Skip to content

Commit

Permalink
feat: ini2 parser (#2365)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws committed Nov 13, 2023
1 parent 5a1ec8a commit 5c1b48d
Show file tree
Hide file tree
Showing 51 changed files with 703 additions and 2,912 deletions.
9 changes: 9 additions & 0 deletions .changelog/7d0bbc1477cd4cefa6958c9762ace056.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "7d0bbc14-77cd-4cef-a695-8c9762ace056",
"type": "feature",
"description": "Replace the legacy config parser with a modern, less-strict implementation. Parsing failures within a section will now simply ignore the invalid line rather than silently drop the entire section.",
"modules": [
"config",
"internal/ini"
]
}
64 changes: 64 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"fmt"
"os"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -146,3 +147,66 @@ func TestLoadDefaultConfig(t *testing.T) {
t.Fatal("expect error when optFn returns error, got nil")
}
}

func BenchmarkLoadProfile1(b *testing.B) {
benchConfigLoad(b, 1)
}

func BenchmarkLoadProfile10(b *testing.B) {
benchConfigLoad(b, 10)
}

func BenchmarkLoadProfile100(b *testing.B) {
benchConfigLoad(b, 100)
}

func BenchmarkLoadProfile1000(b *testing.B) {
benchConfigLoad(b, 1000)
}

func benchConfigLoad(b *testing.B, n int) {
f, err := generateProfiles(n)
if err != nil {
b.Fatal(err)
}

defer os.Remove(f)
opt := WithSharedConfigFiles([]string{f})

b.ResetTimer()
for n := 0; n < b.N; n++ {
LoadDefaultConfig(context.Background(), opt)
}
}

const profileTemplate = `
[profile role%d]
tool_sso_start_url = https://example.awsapps.com/start
tool_sso_region = us-west-2
tool_sso_account_id = 12345678901234
tool_sso_role_name = some_role_name
tool_generated_from = some_tool
credential_process = some_tool credential-process
`

func generateProfiles(n int) (string, error) {
f, err := os.CreateTemp("", fmt.Sprintf("aws-bench-config-%d-*", n))
if err != nil {
return "", err
}

for i := 0; i < n; i++ {
if _, err := fmt.Fprintf(f, profileTemplate, n); err != nil {
f.Close()
os.Remove(f.Name())
return "", err
}
}

if err := f.Close(); err != nil {
os.Remove(f.Name())
return "", err
}

return f.Name(), nil
}
25 changes: 7 additions & 18 deletions config/shared_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import (
var _ regionProvider = (*SharedConfig)(nil)

var (
testConfigFilename = filepath.Join("testdata", "shared_config")
testConfigOtherFilename = filepath.Join("testdata", "shared_config_other")
testCredentialsFilename = filepath.Join("testdata", "shared_credentials")
testConfigLeadingWSFilename1 = filepath.Join("testdata", "leading_ws")
testConfigLeadingWSFilename2 = filepath.Join("testdata", "leading_ws_trailing_nl")
testConfigFilename = filepath.Join("testdata", "shared_config")
testConfigOtherFilename = filepath.Join("testdata", "shared_config_other")
testCredentialsFilename = filepath.Join("testdata", "shared_credentials")
)

func TestNewSharedConfig(t *testing.T) {
Expand Down Expand Up @@ -140,9 +138,10 @@ func TestNewSharedConfig(t *testing.T) {
"Invalid INI file": {
ConfigFilenames: []string{filepath.Join("testdata", "shared_config_invalid_ini")},
Profile: "profile_name",
Err: SharedConfigLoadError{
Filename: filepath.Join("testdata", "shared_config_invalid_ini"),
Err: fmt.Errorf("invalid state"),
Err: SharedConfigProfileNotExistError{
Filename: []string{filepath.Join("testdata", "shared_config_invalid_ini")},
Profile: "profile_name",
Err: nil,
},
},
"S3UseARNRegion property on profile": {
Expand Down Expand Up @@ -687,16 +686,6 @@ func TestNewSharedConfig(t *testing.T) {
EC2IMDSv1Disabled: aws.Bool(false),
},
},
"leading whitespace error 1": {
ConfigFilenames: []string{testConfigLeadingWSFilename1},
Profile: "leading-whitespace-error",
Err: fmt.Errorf("Invalid token, remove leading whitespace"),
},
"leading whitespace error 2": {
ConfigFilenames: []string{testConfigLeadingWSFilename2},
Profile: "leading-whitespace-error",
Err: fmt.Errorf("Invalid token, remove leading whitespace"),
},
}

for name, c := range cases {
Expand Down
120 changes: 0 additions & 120 deletions internal/ini/ast.go

This file was deleted.

33 changes: 0 additions & 33 deletions internal/ini/bench_test.go

This file was deleted.

11 changes: 0 additions & 11 deletions internal/ini/comma_token.go

This file was deleted.

35 changes: 0 additions & 35 deletions internal/ini/comment_token.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/ini/dependency.go

This file was deleted.

43 changes: 0 additions & 43 deletions internal/ini/doc.go

This file was deleted.

4 changes: 0 additions & 4 deletions internal/ini/empty_token.go

This file was deleted.

Loading

0 comments on commit 5c1b48d

Please sign in to comment.