Skip to content

Commit d46a59b

Browse files
committed
removing dependency of ini parse
1 parent 6a8514b commit d46a59b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3605
-40
lines changed

aws/external/shared_config.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package external
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"runtime"
98

109
"github.com/aws/aws-sdk-go-v2/aws"
11-
"github.com/go-ini/ini"
10+
"github.com/aws/aws-sdk-go-v2/aws/awserr"
11+
"github.com/aws/aws-sdk-go-v2/internal/ini"
1212
)
1313

1414
const (
@@ -199,32 +199,27 @@ func NewSharedConfig(profile string, filenames []string) (SharedConfig, error) {
199199

200200
type sharedConfigFile struct {
201201
Filename string
202-
IniData *ini.File
202+
IniData ini.Sections
203203
}
204204

205205
func loadSharedConfigIniFiles(filenames []string) ([]sharedConfigFile, error) {
206206
files := make([]sharedConfigFile, 0, len(filenames))
207207

208208
errs := SharedConfigNotExistErrors{}
209209
for _, filename := range filenames {
210-
b, err := ioutil.ReadFile(filename)
211-
if err != nil {
212-
if os.IsNotExist(err) {
213-
errs = append(errs,
214-
SharedConfigFileNotExistError{Filename: filename, Err: err},
215-
)
216-
continue
217-
}
218-
return nil, SharedConfigLoadError{Filename: filename, Err: err}
219-
}
220-
221-
f, err := ini.Load(b)
222-
if err != nil {
210+
sections, err := ini.OpenFile(filename)
211+
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == ini.ErrCodeUnableToReadFile {
212+
errs = append(errs,
213+
SharedConfigFileNotExistError{Filename: filename, Err: err},
214+
)
215+
// Skip files which can't be opened and read for whatever reason
216+
continue
217+
} else if err != nil {
223218
return nil, SharedConfigLoadError{Filename: filename, Err: err}
224219
}
225220

226221
files = append(files, sharedConfigFile{
227-
Filename: filename, IniData: f,
222+
Filename: filename, IniData: sections,
228223
})
229224
}
230225

@@ -298,47 +293,47 @@ func (c *SharedConfig) setFromIniFiles(profile string, files []sharedConfigFile)
298293
// if a config file only includes aws_access_key_id but no aws_secret_access_key
299294
// the aws_access_key_id will be ignored.
300295
func (c *SharedConfig) setFromIniFile(profile string, file sharedConfigFile) error {
301-
section, err := file.IniData.GetSection(profile)
302-
if err != nil {
296+
section, ok := file.IniData.GetSection(profile)
297+
if !ok {
303298
// Fallback to to alternate profile name: profile <name>
304-
section, err = file.IniData.GetSection(fmt.Sprintf("profile %s", profile))
305-
if err != nil {
299+
section, ok = file.IniData.GetSection(fmt.Sprintf("profile %s", profile))
300+
if !ok {
306301
return SharedConfigProfileNotExistError{
307302
Filename: file.Filename,
308303
Profile: profile,
309-
Err: err,
304+
Err: nil,
310305
}
311306
}
312307
}
313308

314309
// Shared Credentials
315-
akid := section.Key(accessKeyIDKey).String()
316-
secret := section.Key(secretAccessKey).String()
310+
akid := section.String(accessKeyIDKey)
311+
secret := section.String(secretAccessKey)
317312
if len(akid) > 0 && len(secret) > 0 {
318313
c.Credentials = aws.Credentials{
319314
AccessKeyID: akid,
320315
SecretAccessKey: secret,
321-
SessionToken: section.Key(sessionTokenKey).String(),
316+
SessionToken: section.String(sessionTokenKey),
322317
Source: fmt.Sprintf("SharedConfigCredentials: %s", file.Filename),
323318
}
324319
}
325320

326321
// Assume Role
327-
roleArn := section.Key(roleArnKey).String()
328-
srcProfile := section.Key(sourceProfileKey).String()
322+
roleArn := section.String(roleArnKey)
323+
srcProfile := section.String(sourceProfileKey)
329324
if len(roleArn) > 0 && len(srcProfile) > 0 {
330325
c.AssumeRole = AssumeRoleConfig{
331326
RoleARN: roleArn,
332-
ExternalID: section.Key(externalIDKey).String(),
333-
MFASerial: section.Key(mfaSerialKey).String(),
334-
RoleSessionName: section.Key(roleSessionNameKey).String(),
327+
ExternalID: section.String(externalIDKey),
328+
MFASerial: section.String(mfaSerialKey),
329+
RoleSessionName: section.String(roleSessionNameKey),
335330

336331
sourceProfile: srcProfile,
337332
}
338333
}
339334

340335
// Region
341-
if v := section.Key(regionKey).String(); len(v) > 0 {
336+
if v := section.String(regionKey); len(v) > 0 {
342337
c.Region = v
343338
}
344339

aws/external/shared_config_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/aws/aws-sdk-go-v2/aws"
12-
"github.com/go-ini/ini"
12+
"github.com/aws/aws-sdk-go-v2/internal/ini"
1313
)
1414

1515
var _ RegionProvider = (*SharedConfig)(nil)
@@ -96,12 +96,12 @@ func TestNewSharedConfig(t *testing.T) {
9696
SharedConfigProfileNotExistError{
9797
Profile: "profile_not_exists",
9898
Filename: testConfigOtherFilename,
99-
Err: fmt.Errorf("section 'profile profile_not_exists' does not exist"),
99+
Err: nil,
100100
},
101101
SharedConfigProfileNotExistError{
102102
Profile: "profile_not_exists",
103103
Filename: testConfigFilename,
104-
Err: fmt.Errorf("section 'profile profile_not_exists' does not exist"),
104+
Err: nil,
105105
},
106106
},
107107
},
@@ -153,7 +153,7 @@ func TestNewSharedConfig(t *testing.T) {
153153
Profile: "profile_name",
154154
Err: SharedConfigLoadError{
155155
Filename: filepath.Join("testdata", "shared_config_invalid_ini"),
156-
Err: fmt.Errorf("unclosed section: [profile_nam"),
156+
Err: fmt.Errorf("INIParseError:"),
157157
},
158158
},
159159
}
@@ -180,11 +180,11 @@ func TestNewSharedConfig(t *testing.T) {
180180

181181
func TestLoadSharedConfigFromFile(t *testing.T) {
182182
filename := testConfigFilename
183-
f, err := ini.Load(filename)
183+
sections, err := ini.OpenFile(filename)
184184
if err != nil {
185185
t.Fatalf("failed to load test config file, %s, %v", filename, err)
186186
}
187-
iniFile := sharedConfigFile{IniData: f, Filename: filename}
187+
iniFile := sharedConfigFile{IniData: sections, Filename: filename}
188188

189189
cases := []struct {
190190
Profile string
@@ -267,7 +267,7 @@ func TestLoadSharedConfigFromFile(t *testing.T) {
267267
Err: SharedConfigProfileNotExistError{
268268
Filename: "testdata/shared_config",
269269
Profile: "does_not_exist",
270-
Err: fmt.Errorf("section 'profile does_not_exist' does not exist"),
270+
Err: nil,
271271
},
272272
},
273273
}
@@ -419,7 +419,7 @@ func TestLoadSharedConfig(t *testing.T) {
419419
testConfigOtherFilename, testConfigFilename,
420420
},
421421
LoadFn: LoadSharedConfig,
422-
Err: "section 'profile profile_not_exists' does not exist",
422+
Err: "failed to get shared config profile",
423423
},
424424
{
425425
Configs: Configs{
@@ -429,7 +429,7 @@ func TestLoadSharedConfig(t *testing.T) {
429429
testConfigOtherFilename, testConfigFilename,
430430
},
431431
LoadFn: LoadSharedConfigIgnoreNotExist,
432-
Err: "section 'profile profile_not_exists' does not exist",
432+
Err: "failed to get shared config profile",
433433
},
434434
}
435435

internal/ini/.DS_Store

10 KB
Binary file not shown.

internal/ini/ast.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package ini
2+
3+
// ASTKind represents different states in the parse table
4+
// and the type of AST that is being constructed
5+
type ASTKind int
6+
7+
// ASTKind* is used in the parse table to transition between
8+
// the different states
9+
const (
10+
ASTKindNone = ASTKind(iota)
11+
ASTKindStart
12+
ASTKindExpr
13+
ASTKindEqualExpr
14+
ASTKindStatement
15+
ASTKindSkipStatement
16+
ASTKindExprStatement
17+
ASTKindSectionStatement
18+
ASTKindNestedSectionStatement
19+
ASTKindCompletedNestedSectionStatement
20+
ASTKindCommentStatement
21+
ASTKindCompletedSectionStatement
22+
)
23+
24+
func (k ASTKind) String() string {
25+
switch k {
26+
case ASTKindNone:
27+
return "none"
28+
case ASTKindStart:
29+
return "start"
30+
case ASTKindExpr:
31+
return "expr"
32+
case ASTKindStatement:
33+
return "stmt"
34+
case ASTKindSectionStatement:
35+
return "section_stmt"
36+
case ASTKindExprStatement:
37+
return "expr_stmt"
38+
case ASTKindCommentStatement:
39+
return "comment"
40+
case ASTKindNestedSectionStatement:
41+
return "nested_section_stmt"
42+
case ASTKindCompletedSectionStatement:
43+
return "completed_stmt"
44+
case ASTKindSkipStatement:
45+
return "skip"
46+
default:
47+
return ""
48+
}
49+
}
50+
51+
// AST interface allows us to determine what kind of node we
52+
// are on and casting may not need to be necessary.
53+
//
54+
// The root is always the first node in Children
55+
type AST struct {
56+
Kind ASTKind
57+
Root Token
58+
RootToken bool
59+
Children []AST
60+
}
61+
62+
func newAST(kind ASTKind, root AST, children ...AST) AST {
63+
return AST{
64+
Kind: kind,
65+
Children: append([]AST{root}, children...),
66+
}
67+
}
68+
69+
func newASTWithRootToken(kind ASTKind, root Token, children ...AST) AST {
70+
return AST{
71+
Kind: kind,
72+
Root: root,
73+
RootToken: true,
74+
Children: children,
75+
}
76+
}
77+
78+
// AppendChild will append to the list of children an AST has.
79+
func (a *AST) AppendChild(child AST) {
80+
a.Children = append(a.Children, child)
81+
}
82+
83+
// GetRoot will return the root AST which can be the first entry
84+
// in the children list or a token.
85+
func (a *AST) GetRoot() AST {
86+
if a.RootToken {
87+
return *a
88+
}
89+
90+
if len(a.Children) == 0 {
91+
return AST{}
92+
}
93+
94+
return a.Children[0]
95+
}
96+
97+
// GetChildren will return the current AST's list of children
98+
func (a *AST) GetChildren() []AST {
99+
if len(a.Children) == 0 {
100+
return []AST{}
101+
}
102+
103+
if a.RootToken {
104+
return a.Children
105+
}
106+
107+
return a.Children[1:]
108+
}
109+
110+
// SetChildren will set and override all children of the AST.
111+
func (a *AST) SetChildren(children []AST) {
112+
if a.RootToken {
113+
a.Children = children
114+
} else {
115+
a.Children = append(a.Children[:1], children...)
116+
}
117+
}
118+
119+
// Start is used to indicate the starting state of the parse table.
120+
var Start = newAST(ASTKindStart, AST{})

internal/ini/bench_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ini
2+
3+
import (
4+
oldini "github.com/go-ini/ini"
5+
"testing"
6+
)
7+
8+
const (
9+
section = `[default]
10+
region = us-west-2
11+
credential_source = Ec2InstanceMetadata
12+
s3 =
13+
foo=bar
14+
bar=baz
15+
output = json
16+
17+
[assumerole]
18+
output = json
19+
region = us-west-2
20+
`
21+
)
22+
23+
func BenchmarkINIParser(b *testing.B) {
24+
for i := 0; i < b.N; i++ {
25+
ParseBytes([]byte(section))
26+
}
27+
}
28+
29+
func BenchmarkGoINIParser(b *testing.B) {
30+
for i := 0; i < b.N; i++ {
31+
oldini.Load([]byte(section))
32+
}
33+
}
34+
35+
func BenchmarkTokenize(b *testing.B) {
36+
lexer := iniLexer{}
37+
for i := 0; i < b.N; i++ {
38+
lexer.tokenize([]byte(section))
39+
}
40+
}

internal/ini/comma_token.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ini
2+
3+
var commaRunes = []rune(",")
4+
5+
func isComma(b rune) bool {
6+
return b == ','
7+
}
8+
9+
func newCommaToken() Token {
10+
return newToken(TokenComma, commaRunes, NoneType)
11+
}

0 commit comments

Comments
 (0)