Skip to content

Commit 422bb5c

Browse files
authored
Merge pull request #287 from compose-spec/add-nakedret-linter
Add "nakedret" to the linters to put dotenv in conformity
2 parents cc56ae5 + ca8f7fd commit 422bb5c

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

dotenv/godotenv.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func ParseWithLookup(r io.Reader, lookupFn LookupFn) (map[string]string, error)
6262
// godotenv.Load("fileone", "filetwo")
6363
//
6464
// It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults
65-
func Load(filenames ...string) (err error) {
65+
func Load(filenames ...string) error {
6666
return load(false, filenames...)
6767
}
6868

@@ -77,20 +77,19 @@ func Load(filenames ...string) (err error) {
7777
// godotenv.Overload("fileone", "filetwo")
7878
//
7979
// It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars.
80-
func Overload(filenames ...string) (err error) {
80+
func Overload(filenames ...string) error {
8181
return load(true, filenames...)
8282
}
8383

84-
func load(overload bool, filenames ...string) (err error) {
84+
func load(overload bool, filenames ...string) error {
8585
filenames = filenamesOrDefault(filenames)
86-
8786
for _, filename := range filenames {
88-
err = loadFile(filename, overload)
87+
err := loadFile(filename, overload)
8988
if err != nil {
90-
return // return early on a spazout
89+
return err
9190
}
9291
}
93-
return
92+
return nil
9493
}
9594

9695
var startsWithDigitRegex = regexp.MustCompile(`^\s*\d.*`) // Keys starting with numbers are ignored
@@ -121,12 +120,12 @@ func ReadWithLookup(lookupFn LookupFn, filenames ...string) (map[string]string,
121120

122121
// Read all env (with same file loading semantics as Load) but return values as
123122
// a map rather than automatically writing values into env
124-
func Read(filenames ...string) (envMap map[string]string, err error) {
123+
func Read(filenames ...string) (map[string]string, error) {
125124
return ReadWithLookup(nil, filenames...)
126125
}
127126

128127
// Unmarshal reads an env file from a string, returning a map of keys and values.
129-
func Unmarshal(str string) (envMap map[string]string, err error) {
128+
func Unmarshal(str string) (map[string]string, error) {
130129
return UnmarshalBytes([]byte(str))
131130
}
132131

@@ -223,10 +222,10 @@ func loadFile(filename string, overload bool) error {
223222
return nil
224223
}
225224

226-
func readFile(filename string, lookupFn LookupFn) (envMap map[string]string, err error) {
225+
func readFile(filename string, lookupFn LookupFn) (map[string]string, error) {
227226
file, err := os.Open(filename)
228227
if err != nil {
229-
return
228+
return nil, err
230229
}
231230
defer file.Close()
232231

@@ -235,7 +234,7 @@ func readFile(filename string, lookupFn LookupFn) (envMap map[string]string, err
235234

236235
var exportRegex = regexp.MustCompile(`^\s*(?:export\s+)?(.*?)\s*$`)
237236

238-
func parseLine(line string, envMap map[string]string) (key string, value string, err error) {
237+
func parseLine(line string, envMap map[string]string) (string, string, error) {
239238
return parseLineWithLookup(line, envMap, nil)
240239
}
241240
func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupFn) (string, string, error) {

dotenv/parser.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ func getStatementStart(src []byte) []byte {
8181
}
8282

8383
// locateKeyName locates and parses key name and returns rest of slice
84-
func locateKeyName(src []byte) (key string, cutset []byte, inherited bool, err error) {
84+
func locateKeyName(src []byte) (string, []byte, bool, error) {
85+
var key string
86+
var inherited bool
8587
// trim "export" and space at beginning
8688
src = bytes.TrimLeftFunc(bytes.TrimPrefix(src, []byte(exportPrefix)), isSpace)
8789

@@ -120,7 +122,7 @@ loop:
120122

121123
// trim whitespace
122124
key = strings.TrimRightFunc(key, unicode.IsSpace)
123-
cutset = bytes.TrimLeftFunc(src[offset:], isSpace)
125+
cutset := bytes.TrimLeftFunc(src[offset:], isSpace)
124126
return key, cutset, inherited, nil
125127
}
126128

@@ -208,14 +210,14 @@ func indexOfNonSpaceChar(src []byte) int {
208210
}
209211

210212
// hasQuotePrefix reports whether charset starts with single or double quote and returns quote character
211-
func hasQuotePrefix(src []byte) (quote byte, isQuoted bool) {
213+
func hasQuotePrefix(src []byte) (byte, bool) {
212214
if len(src) == 0 {
213215
return 0, false
214216
}
215217

216-
switch prefix := src[0]; prefix {
218+
switch quote := src[0]; quote {
217219
case prefixDoubleQuote, prefixSingleQuote:
218-
return prefix, true
220+
return quote, true // isQuoted
219221
default:
220222
return 0, false
221223
}

golangci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ linters:
1111
- gosimple
1212
- ineffassign
1313
- misspell
14+
- nakedret
1415
- govet
1516
linters-settings:
1617
gocritic:
@@ -23,4 +24,4 @@ linters-settings:
2324
disabled-checks:
2425
- paramTypeCombine
2526
- unnamedResult
26-
- whyNoLint
27+
- whyNoLint

0 commit comments

Comments
 (0)