Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 7d526c0

Browse files
committed
adding error code for unable to read files
1 parent c7514f9 commit 7d526c0

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

aws/session/internal/ini/ini.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package ini
33
import (
44
"io"
55
"os"
6+
7+
"github.com/aws/aws-sdk-go/aws/awserr"
68
)
79

810
// OpenFile takes a path to a given file, and will open and parse
911
// that file.
1012
func OpenFile(path string) (Sections, error) {
1113
f, err := os.Open(path)
1214
if err != nil {
13-
return nil, err
15+
return nil, awserr.New(ErrCodeUnableToReadFile, "unable to open file", err)
1416
}
1517
defer f.Close()
1618

aws/session/internal/ini/ini_lexer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ package ini
33
import (
44
"io"
55
"io/ioutil"
6+
7+
"github.com/aws/aws-sdk-go/aws/awserr"
8+
)
9+
10+
const (
11+
// ErrCodeUnableToReadFile is used when a file is failed to be
12+
// opened or read from.
13+
ErrCodeUnableToReadFile = "FailedRead"
614
)
715

816
// TokenType represents the various different tokens types
@@ -61,7 +69,7 @@ type Token interface {
6169
func (l *iniLexer) Tokenize(r io.Reader) ([]Token, error) {
6270
b, err := ioutil.ReadAll(r)
6371
if err != nil {
64-
return nil, err
72+
return nil, awserr.New(ErrCodeUnableToReadFile, "unable to read file", err)
6573
}
6674

6775
var tok Token

aws/session/internal/ini/newline_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func isNewline(b []byte) bool {
2525

2626
func newNewlineToken(b []byte) (newlineToken, int, error) {
2727
value := string(b[0])
28-
if isNewline(b[1:]) {
28+
if value[0] != '\n' && isNewline(b[1:]) {
2929
value += string(b[1])
3030
}
3131

aws/session/shared_config.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package session
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/aws/aws-sdk-go/aws/awserr"
87
"github.com/aws/aws-sdk-go/aws/credentials"
@@ -106,18 +105,16 @@ func loadSharedConfigIniFiles(filenames []string) ([]sharedConfigFile, error) {
106105
files := make([]sharedConfigFile, 0, len(filenames))
107106

108107
for _, filename := range filenames {
109-
f, err := os.Open(filename)
110-
if err != nil {
108+
sections, err := ini.OpenFile(filename)
109+
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == ini.ErrCodeUnableToReadFile {
110+
// Skip files which can't be opened and read for whatever reason
111111
continue
112-
}
113-
114-
tables, err := ini.Parse(f)
115-
if err != nil {
112+
} else if err != nil {
116113
return nil, SharedConfigLoadError{Filename: filename, Err: err}
117114
}
118115

119116
files = append(files, sharedConfigFile{
120-
Filename: filename, IniData: tables,
117+
Filename: filename, IniData: sections,
121118
})
122119
}
123120

0 commit comments

Comments
 (0)